Discussion:
Need help with KB249232: How To Get IHTMLDocument2 from a HWND
(too old to reply)
Charles Chandler
2005-05-24 19:31:04 UTC
Permalink
When running a little test app I created based on the code in KB249232
(http://support.microsoft.com/default.aspx?scid=kb;en-us;249232), I keep
encountering an error getting a pointer to the IHTMLDocument2 interface using
the IHTMLWindow2::get_document() function.

On one computer that I've tested this with, the error is consistently
"0x800703e6 Invalid access to memory location."

On another computer, the error is something like "Null reference pointer
<yadda,yadda,yadda>"

Does anybody have any clues they can give me about what's going on here?

Thanks,

CVC

Code snippet follows:

CoInitialize( NULL );

HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
if ( hInst != NULL )
{
if ( HWND != NULL )
{
BSTR ClassName;
this->get_ClassName(&ClassName);
CComBSTR atlbstrClassName(ClassName);
if ( atlbstrClassName == _T("Internet Explorer_Server"))
{
CComPtr<IHTMLDocument2> spDoc;
LRESULT lRes;

UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
::SendMessageTimeout( this->hwndGUIObject, nMsg, 0L, 0L,
SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

LPFNOBJECTFROMLRESULT pfObjectFromLresult =
(LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc
);
if ( SUCCEEDED(hr) )
{
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;

hr = spWin->get_document( &spDoc.p );
// Change background color to red
spDoc->put_bgColor( CComVariant("red") );

}
}
} // else document not ready
} // else Internet Explorer is not running
::FreeLibrary( hInst );
} // else Active Accessibility is not installed
CoUninitialize();
--
I wish the toaster to be happy, too.
Wei-Dong XU [MSFT]
2005-05-25 07:11:21 UTC
Permalink
Hi,

From my review to your code, I found the line:
"if ( HWND != NULL )"

Should the HWND be hWnd or hwnd?

Furthermore, could you be so kind to send me one repro project? I will test
this for you then. My email address is: v-***@nospam.microsoft.com (please
remove the nospam used to avoid email).

Look forward to your update!

Best Regards,
Wei-Dong XU
Microsoft Product Support Services
This posting is provided "AS IS" with no warranties, and confers no rights.
Charles Chandler
2005-05-26 13:42:04 UTC
Permalink
Hey,

Thanks for responding.

That "if ( HWND != NULL )" line was an error I injected when sanitizing
that code snippet for publication. Where the HWND is now was originally the
name of a member variable for the class where this code is part of a method
definition.

I realized that should have been hWnd rather than HWND shortly after I
posted this, but could not figure out how to correc the mistake.
Post by Wei-Dong XU [MSFT]
Furthermore, could you be so kind to send me one repro project?
I have done so, and appreciate your efforts on my behalf.

Thanks!

CVC
Wei-Dong XU [MSFT]
2005-05-30 08:34:50 UTC
Permalink
Hi,

I have reviewed your project. Based on my research, this code works very
well at my scenario without any problem. I modified the code below:
::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );
to:
HWND hGlobal = GetDesktopWindow();
::EnumChildWindows( hGlobal, EnumChildProc, (LPARAM)&hWndChild );

This way, the EnumChildWindows api will return one IE window handle to me.
Then the code afterwords works as expected to set the background to red.

So I think the code from kb article 249232 contains no bug. For your
scenario, it is really very hard for me to help you now. One debugging on
your project is required. For the debugging, I think the best and quickest
way for you is to raise one incident to Microsoft local PSS. One support
professional will focus on your issue and perform the debugging/remote
debugging to locate the root cause. Your Microsoft local PSS can be
contacted from:
http://support.microsoft.com/common/international.aspx

Please feel free to let me know if any question. I am more than happy to be
of any assistance.

Best Regards,
Wei-Dong XU
Microsoft Product Support Services
This posting is provided "AS IS" with no warranties, and confers no rights.
Loading...