|
-
July 18th, 2012, 11:55 AM
#1
How to get text input ?
Hello everybody .
I have read this reference
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
http://msdn.microsoft.com/en-us/libr...=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
Code:
<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
Code:
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 by headshot; July 18th, 2012 at 09:50 PM.
-
July 18th, 2012, 01:23 PM
#2
Re: How to get text input ?
 Originally Posted by headshot
You can tell me why it does not check the contents into input text ?
Code:
LPTSTR value_text = new TCHAR[SysStringLen(type_text)];
//...
if(strcmp("http://google.com/",(const char *)value_text) == 0)
There are several things wrong with this code.
1) Memory leak. You do not delete[] the memory you allocated with new[]. Instead, use CArray, CString, std::vector<TCHAR>, or some sort of an RAII type where the new[]/delete[] is not done by you, but by the class itself. The reason why you should do this is to ensure the memory is cleaned up, even if an exception is thrown somewhere in the function. Even something crude like this would work:
Code:
struct StringRAII
{
LPTSTR m_p;
StringRAII( unsigned int nLen ) : m_p(new TCHAR[nLen]) { }
LPTSTR get() { return m_p; }
~StringRAII( ) { delete [] m_p; }
};
//...
StringRAII str(SysStringLen(type_text));
LPTSTR value_text = str.get();
//...
All I did was to create a very simple class. I create an instance and allocate the memory. When str goes out of scope for any reason whatsoever, the memory is deleted. You need not worry when, where, or how to delete the memory, it will just happen all due to the destructor of StringRAII. Caution -- only use this is your goal is to delete the memory -- if for some reason you want that string to exist outside the function, then don't use that struct, instead use a string class.
2) The value_text is a pointer to a TCHAR. This means that TCHAR will either be an ANSI character or Unicode character type, depending on the build settings. However, the strcmp() function only works for ANSI types.
3) Inside the strcmp() call, you are erroneously casting the type to a const char*. If you removed that cast, what does the compiler say to you when you compile the code? If the value_text is actually a Unicode string, you cannot change a Unicode string into an ANSI string by casting. To change a Unicode string into an ANSI string requires conversion. A C-style cast is not smart enough to do conversions -- you have to call functions to do this (WideCharToMultiByte, MultiByteToWideChar, the various ATL macros, etc.).
So you have at least 3 things wrong with the code that has nothing to do with HTML.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 18th, 2012 at 01:38 PM.
-
July 18th, 2012, 09:49 PM
#3
Re: How to get text input ?
hi .
About problem memory leak . I must exist string to compare with line in file text ,  
Second . I think process ANSI/UNICODE
Code:
// Convert the text from Unicode to ANSI
LPTSTR value_text = new TCHAR[SysStringLen(type_text)];
lstrcpy(value_text, OLE2T(type_text));
I think it`s wrong in here
Code:
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;
-
July 18th, 2012, 10:56 PM
#4
Re: How to get text input ?
I think it`s recieved text the user input
Code:
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;
}
-
July 21st, 2012, 04:26 PM
#5
Re: How to get text input ?
 Originally Posted by headshot
hi .
About problem memory leak . I must exist string to compare with line in file text ,  
It is a memory leak. You did not deallocate the memory when you were finished with it.
Second . I think process ANSI/UNICODE
Which one is it, ANSI or UNICODE?
Code:
if(strcmp("http://google.com/",(const char *)value_text) == 0)
This is still wrong, but you made no attempt to change it. If value_text is a UNICODE (16-bit) character string, that code will not work.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 23rd, 2012 at 01:15 AM.
-
July 22nd, 2012, 11:17 PM
#6
Re: How to get text input ?
 Originally Posted by Paul McKenzie
It is a memory leak. You did not deallocate the memory when you were finished with it.
Which one is it, ANSI or UNICODE?
Code:
if(strcmp("http://google.com/",(const char *)value_text) == 0)
This is still wrong, but you made no attempt to change it. If value_text is a UNICODE (16-bit) character string, that code will not work.
Regards,
Paul McKenzie
Convert the text from Unicode to ANSI
Code:
LPTSTR value_text= new TCHAR[SysStringLen(bstrValue)];
lstrcpy(value_text, OLE2T(bstrValue));
The main problems is get value of textbox in HTML page ?
Do you understand anything i say .
-
July 23rd, 2012, 01:21 AM
#7
Re: How to get text input ?
 Originally Posted by headshot
Convert the text from Unicode to ANSI
Code:
LPTSTR value_text= new TCHAR[SysStringLen(bstrValue)];
lstrcpy(value_text, OLE2T(bstrValue));
The main problems is get value of textbox in HTML page ?
Do you understand anything i say .
Why are you still doing this?
Code:
if(strcmp("http://google.com/",value_text) == 0)
I already stated to you that strcmp() is an ANSI function. Here is my quote again:
2) The value_text is a pointer to a TCHAR. This means that TCHAR will either be an ANSI character or Unicode character type, depending on the build settings. However, the strcmp() function only works for ANSI types.
The "http://google.com/" is an ANSI string literal. If you changed value_text to a Unicode string, you are now comparing ANSI to Unicode strings using an ANSI function, which is still wrong.
Here is a fixed version:
Code:
if(_tcscmp(_T("http://google.com/"),value_text) == 0)
That is the proper way to compare a string literal to a TCHAR. Please see here:
http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
Look at the TCHAR version of the function and what the _T() macro does.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 23rd, 2012 at 01:28 AM.
-
July 23rd, 2012, 06:07 AM
#8
Re: How to get text input ?
yes
So what can you guide me to get value of textbox on the HTML page
-
July 23rd, 2012, 11:08 AM
#9
Re: How to get text input ?
I found some link from msdn
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx : input element | HTMLInputElement object
http://msdn.microsoft.com/en-us/libr...spx#properties : input type=text element | input type=text object
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx : value attribute | value property
Do you think so ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|