Re: How to get text input ?
Quote:
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
Re: How to get text input ?
hi .
About problem memory leak . I must exist string to compare with line in file text , :D:eek:
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;
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;
}
Re: How to get text input ?
Quote:
Originally Posted by
headshot
hi .
About problem memory leak . I must exist string to compare with line in file text , :D:eek:
It is a memory leak. You did not deallocate the memory when you were finished with it.
Quote:
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
Re: How to get text input ?
Quote:
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 .
Re: How to get text input ?
Quote:
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:
Quote:
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
Re: How to get text input ?
yes
So what can you guide me to get value of textbox on the HTML page
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 ?