Re: CHtmlView document.returnValue
On Mon, 21 May 2007 17:24:28 GMT, MrAsm <mrasm@usa.com> wrote:
On 21 May 2007 08:46:34 -0700, Francesco Vitale
<francesco.vitale@gmail.com> wrote:
Here follows my HTML code:
<HTML>
<BODY oncontextmenu="return false">
<script>
window.returnValue = "I_have_to_get_this_value"
and yes... I'm trying to get the "I_have_to_get_this_value".
I think that the easiest thing to do is to add a function in your
JavaScript HTML file, like this:
function getTheWindowReturnValue() {
return window.returnValue;
}
And then you can invoke this function from C++, with the code I showed
in my previous post. This function will just return the
'window.returnValue' that you want.
I did the test and it works fine.
I tried to do some "navigation" of the DOM using VB6 (which makes
COM/OLE Automation much easier than pure C++ :), and I found also a
different approach.
You might just get the "returnValue" property from the JavaScript
"window" object (IHTMLWindow2).
i.e. we can modify my previous code, so we can get the IHTMLWindow2
interface ("window" object) from IHTMLDocument2 interface, via
IHTMLDocument2::get_parentWindow, then we can read the
"window.returnValue" property in a similar way we used to invoke the
JavaScript function (using the COleDispatchDriver helper class and its
GetProperty method).
I'd like to know also different solutions, if you have any.
The code follows:
(BTW: Note that in this version I used just
ATLASSERT( SUCCEEDED(hr) );
to do the check for HRESULTs, so the source code gets not very
"bloated" by error checks and messages.)
<CODE language="C++">
void TestGetReturnValue()
{
...
// pHtmlView is a pointer to CHtmlView derived class
//
// Hungarian Notation here:
//
// sp : smart-pointer (CComPtr)
//
// COM operations return code
HRESULT hr;
//
// Access the IHTMLDocument2 interface from the HTML view
//
// Get IDispatch for HTML Document from HTML view
CComPtr< IDispatch > spDispatch = pHtmlView->GetHtmlDocument();
// Get IHTMLDocument2 interface from the above IDispatch
CComPtr< IHTMLDocument2 > spDocument;
hr = spDispatch->QueryInterface( IID_IHTMLDocument2,
(void **) &spDocument );
ATLASSERT( SUCCEEDED(hr) );
//
// Get parentWindow from HTML document
//
CComPtr< IHTMLWindow2 > spWindow;
hr = spDocument->get_parentWindow( &spWindow );
ATLASSERT( SUCCEEDED(hr) );
// *** NEW ***
// *** GET THE window.returnValue PROPERTY ***
// *** NEW ***
// Name of the property to get
const CString strProperty = _T("returnValue");
// Convert it to COM BSTR string
CComBSTR bstrProperty( strProperty );
// ID associated to property
DISPID dispid = NULL;
// Map the property name to its numerical ID
hr = spWindow->GetIDsOfNames(
IID_NULL, // (reserved)
&bstrProperty, // name to get ID of
1, // only 1 name
LOCALE_SYSTEM_DEFAULT, // default locale context
&dispid // will return the ID associated
// to given name
);
ATLASSERT( SUCCEEDED(hr) );
// Return value of the property (stored in OLE variant)
COleVariant result;
// Use the helper class to get the property
COleDispatchDriver dispatchDriver(
spWindow, // Script Engine
FALSE // Don't automatically release it
);
dispatchDriver.GetProperty( dispid, VT_VARIANT,
(void *) &result );
// We know it's a string (BSTR).
// Display it
CString msg( result.bstrVal );
MessageBox( msg );
}
</CODE>
MrAsm