How to get text input ?

Hello everybody .
I have read this reference
http://msdn.microsoft.com/en-us/library/aa752127%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa768277%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/aa752574%28v=vs.85%29.aspx
Now , I want to check user input text . If the user enters "http://google.com". After "DISPID_BEFORENAVIGATE2" of Internet Explorer Web Browser loaded successfully. it checks if the same "http://google.com" then return "about: blank"
This is my form HTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>

<form method="POST" action="--WEBBOT-SELF--">
	<label>Website ABC</label>
	<br>
	<input type="text" name="txt_url" value="http://google.com" size="50">
	<br>
	<textarea name="txt_url2" cols="50" rows="2">http://msdn.com</textarea>
	<br>
	<input type="checkbox" name="chk_male" value="Male" checked>
	<br>
	<input type="text" name="T1" size="20">
	<input type="submit" value="Submit" name="B1">
	<input type="reset" value="Reset" name="B2">
	<br>
	<input type="submit" name="Ok" value="ok_click">
	<input type="reset" value="cancel_click" name="Cancel">
</form>

</body>

</html>

Note : In this HTML have to three text input
And this is event code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
STDMETHODIMP ABCtest::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pvarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)   
{   
    USES_CONVERSION; 
    if(dispidMember == DISPID_BEFORENAVIGATE2)   
    {   
        BSTR type_text;   
        HRESULT result = m_spWebBrowser2->get_Type(&type_text);   
        if(FAILED(result ))   
            return result ;   

        LPTSTR value_text = new TCHAR[SysStringLen(type_text)];   
        lstrcpy(value_text, OLE2T(type_text));   

        if(strcmp("http://google.com/",(const char *)value_text) == 0)
        {                                   
            VARIANT vFlags = {0},vTargetFrameName = {0};   
            m_spWebBrowser2->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);   
            m_spWebBrowser2->put_Visible(VARIANT_TRUE);   
            return S_FALSE;   
        }   
        return S_OK;   
    }   
    else if(dispidMember == DISPID_NAVIGATECOMPLETE2) 
    {                                           
        BSTR type_text;   
        HRESULT result = m_spWebBrowser2->get_Type(&type_text);   
        if(FAILED(result))   
            return result;   

        LPTSTR value_text = new TCHAR[SysStringLen(type_text)];   
        lstrcpy(value_text, OLE2T(type_text));   

        if(strcmp("http://google.com/",(const char *)value_text) == 0)   
        {   
            VARIANT vFlags = {0},vTargetFrameName = {0};   
            m_spWebBrowser2->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);   
            m_spWebBrowser2->put_Visible(VARIANT_TRUE);   
        }   
        return S_OK;   
    }   
    return S_FALSE;   
}   

You can tell me why it does not check the contents into input text ?
Last edited on
Well, first and foremost, this should be posted in the Windows Programming forum. You can move this topic to that forum if you like.

Second: You have a GLORIOUS mess of ANSI/UNICODE strings that can lead to no good. You have got to learn how to properly code agnostically, or just plain stick to one of them (UNICODE being the preferred option).

Third: You for some reason believe the that the type name is the address being navigated to. Incorrect. get_Type() will return "HTML Document" if the document is an HTML page. It will return other names for other types of documents. This has no relation whatsoever to the URL being navigated to.

According to http://msdn.microsoft.com/en-us/library/aa768280(VS.85).aspx , the BeforeNavigate2 event passes the URL being navigated to as the second argument. Remember that you access the parameters from right to left in IDispatch::Invoke.

Fourth: You are leaking the allocated memory in line 11.
Last edited on
You'll have a better chance getting the attention of our .Net guys if you post this in the Windows Forum. It also might help us if you let us know what data type m_spWebBrowser2 is as well (if this is a built in member or some kind of functor I wouldn't know, I'm not a .Net guy).

All that being said. I have to ask, are you making a Proxy?!?! That would hands down be the coolest .Net app idea I've ever seen on this site!
Hi webJose and Computergeek01. I had move it to Window programming .In here I hope assistant from someone .
Well , First , I think process ANSI/UNICODE
1
2
3
// Convert the text from Unicode to ANSI
LPTSTR value_text = new TCHAR[SysStringLen(type_text)];   
        lstrcpy(value_text, OLE2T(type_text));   

oh ,i have HTML page as same above , I must get text input if the user typing in input . I think get_type() do it .and i think i wrong at here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
BSTR type_text;   
        HRESULT result = m_spWebBrowser2->get_Type(&type_text);   
//pointer to receive data from spWebBrowser2
        if(FAILED(result ))   
            return result ;   
///////////////////////////////////////////////////////////////////////////

// in here this wrong
// 
        LPTSTR value_text = new TCHAR[SysStringLen(type_text)];   
        lstrcpy(value_text, OLE2T(type_text));   
/////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////
// text input HTML will save in value_text parameter the user input
// then compare with http://google.com
        if(strcmp("http://google.com/",(const char *)value_text) == 0)
        {                                   
            VARIANT vFlags = {0},vTargetFrameName = {0};   
            m_spWebBrowser2->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);   
            m_spWebBrowser2->put_Visible(VARIANT_TRUE);   
            return S_FALSE;   
        }   
        return S_OK; 

I think it`s recieve text the user input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
if(dispidMember == DISPID_BEFORENAVIGATE2)
{
	CComBSTR bstrName;
	HRESULT hRes=m_spWebBrowser2->get_tagName(&bstrName);
	bstrName.ToUpper();
		
	if(SUCCEEDED(hRes) && bstrName == "INPUT")   
    {  
		CComPtr<IHTMLInputElement> pInputElement;   
		HRESULT hRes=QueryInterface(IID_IHTMLInputElement, &pInputElement);  

		if(SUCCEEDED(hRes) && pInputElement)   
		{ 
			CComBSTR bstrType;   
            HRESULT hRes = pInputElement->get_type(&bstrType);   
            bstrType.ToLower();   
            if(SUCCEEDED(hRes) && bstrType == "text")   
            {   
                CComBSTR bstrValue;   
                HRESULT hRes = pInputElement->get_value(&bstrValue);   
				
                if(SUCCEEDED(hRes))  
				{   
                    if(bstrValue.Length()>0)   
                    {   
                        string value_text += CString(bstrValue);   
					}
                }  				
			}
		}
	}
	
	if(strcmp("http://google.com/",value_text) == 0) 
	{		
			VARIANT vFlags = {0},vTargetFrameName = {0};
			// Instead of about:blank, you can redirect http://yahoo.com
			m_spWebBrowser2->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);
			m_spWebBrowser2->put_Visible(VARIANT_TRUE);
			return S_FALSE;
	}
	return S_OK;
}
Last edited on
You declare the variable 'value_text' as LPTSTR and you are then forced to cast it as a char*. That is a HOLY MESS. Just plain HORRIBLE. Stick to UNICODE and forget about TCHAR, LPTSTR and all that and simply work with WCHAR, LPWSTR and related functions. For example, it is as simple as using wcscmp() to compare wide strings instead of the ANSI version strcmp(). There's a wide option to every function. If you don't know how to properly use the TCHAR, then don't use it. You are inflicting pain on yourself. Besides, BSTR is a wide character string so you don't have to convert it. It is actually simpler.

get_Type() doesn't return the URL the browser is navigating to. Check the documentation. You will never ever get a successful comparison to http://www.google.com/ using the value returned by get_Type().

As for your other piece of code using get_tagName(). You are now trying to read HTML elements on a browser that is about to destroy whatever current page it has. The address bar is NOT an HTML element in a page. You will NOT get the URL like this. I already told you: The URL is the second parameter in the BeforeNavigate2 list of parameters. I even gave you the URL to the documentation.

Ah, I think I understood better what you are looking for after re-reading the original post.

This is very strange indeed. In any case, I think you are doing it wrong: You must first obtain the document object. Then see if it supports the IHTMLDocument or IHTMLDocumentX interfaces, where X is a number between 2 and 7. If you get a valid interface pointer then you can be sure the document is HTML or can be represented as one. Only then you can attempt to access HTML elements by tag name or ID. I recommend ID: Use GetElementById().
Last edited on
I think we just need Get_tagname (INPUT) and get_type (TEXT). Then we turn get_value and save as above. As you say tring BSTR is a large type string, so we store the user to enter values ​​in this variable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
if(dispidMember == DISPID_BEFORENAVIGATE2)
{
	CComBSTR bstrName;
	HRESULT hRes=m_spWebBrowser2->get_tagName(&bstrName);
	bstrName.ToUpper();
		
	if(SUCCEEDED(hRes) && bstrName == "INPUT")   
        {  
		CComPtr<IHTMLInputElement> pInputElement;   
		HRESULT hRes=QueryInterface(IID_IHTMLInputElement, &pInputElement);  

		if(SUCCEEDED(hRes) && pInputElement)   
		{ 
			CComBSTR bstrType;   
                       HRESULT hRes = pInputElement->get_type(&bstrType);   
                       bstrType.ToLower();   
                       if(SUCCEEDED(hRes) && bstrType == "text")   
                      {   
                            CComBSTR bstrValue;   
                            HRESULT hRes = pInputElement->get_value(&bstrValue);   
				
                             if(SUCCEEDED(hRes))  
			    {   
                                    if(bstrValue.Length()>0)   
                                   {   
                                            BSTR value_text=bstrValue;
                                            //  string value_text += CString(bstrValue);   
				   }
                            }  				
		       }
		}
	}
	
	if(strcmp("http://google.com/",value_text) == 0) 
	{		
			VARIANT vFlags = {0},vTargetFrameName = {0};
			// Instead of about:blank, you can redirect http://yahoo.com
			 m_spWebBrowser2->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);
			m_spWebBrowser2->put_Visible(VARIANT_TRUE);
			return S_FALSE;
	}
	return S_OK;
}

And we can not use getElementById (). Because the INPUT and HTML pages have no ID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<form method="POST" action="--WEBBOT-SELF--">
	<label>Website ABC</label>
	<br>
	<input type="text" name="txt_url" value="http://google.com" size="50">
	<br>
	<textarea name="txt_url2" cols="50" rows="2">http://msdn.com</textarea>
	<br>
	<input type="checkbox" name="chk_male" value="Male" checked>
	<br>
	<input type="text" name="T1" size="20">
	<input type="submit" value="Submit" name="B1">
	<input type="reset" value="Reset" name="B2">
	<br>
	<input type="submit" name="Ok" value="ok_click">
	<input type="reset" value="cancel_click" name="Cancel">
</form>
</body>
</html>
Last edited on
Topic archived. No new replies allowed.