Thread: IHTMLDocument2 get_body error

  1. #1
    Registered User
    Join Date
    Aug 2008

    IHTMLDocument2 get_body error

    Hi all, I have this code:

    void OnGetDocInterface(HWND hWnd) 
    	CoInitialize( NULL );
    	// Explicitly load MSAA so we know if it's installed
    	HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
    	if ( hInst != NULL )
    		if ( hWnd != NULL )
    			HWND hWndChild=NULL;
    			// Get 1st document window
    			hWndChild = FindIEWindow()
    			if ( hWndChild )
    //hWndChild contains a valid "Internet Explorer_Server" handle
    				CComPtr<IHTMLDocument2> spDoc;
    				LRESULT lRes;
    				UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
    				::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
    				LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, (char *)("ObjectFromLresult") );
    				if ( pfObjectFromLresult != NULL )
    					HRESULT hr;
    					hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
    					if ( SUCCEEDED(hr) )
    						// Change background color to red
    						spDoc->put_bgColor( CComVariant(L"red") );//this is were the program crashes*
    			} // else document not ready
    		} // else Internet Explorer is not running
    		::FreeLibrary( hInst );
    	} // else Active Accessibility is not installed
    When execution gets to:
    spDoc->put_bgColor( CComVariant(L"red") );
    I get error:
    "Unhandled exception at 0xabababab in TestApp.exe: 0xC0000005: Access violation reading location 0xabababab."
    and my program crashes.
    The handle is corect and there is no error until that point.
    The code is from so it is probably corect.
    I use VC++ 2008 Express Edition, I have included:
    #include <Windows.h>
    #include <Mshtml.h>
    #include <OleAcc.h>
    #include <atlbase.h>
    Thx in advance .

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    I am going to pull a Salem move on this one.

    Humor aside, I am of the impression you should have

    spDoc->put_bgColor(CComBSTR(L"red") );
    instead of:

    spDoc->put_bgColor( CComVariant(L"red") );
    But since I never use ATL, I have no freaking clue. I just used google and copied and pasted from pages without reading them.

  3. #3
    Registered User
    Join Date
    Dec 2008

    What's wrong with that?

    I do have the same problem, any one would help me please?

    VS2008 C++, ATL

    Unhandled exception at 0xabababab in GetIHtmlElementSample1.exe: 0xC0000005: Access violation reading location 0xabababab.
    BSTR ur;
    // if( spDoc->get_URL( &ur ) )
    //	 ShowMessage( ur );
    // Change background color to red
    spDoc->put_bgColor( CComVariant("red") );
    // spDoc->put_bgColor( CComBSTR(L"red") );
    ShowMessage( _T("Congraturations to you, you have change the background color to red."));
    	// Change background color to red
    							spDoc->put_bgColor( CComVariant("red") );
    00415B40  push        offset string "red" (4296F0h) 
    00415B45  lea         ecx,[ebp-15Ch] 
    00415B4B  call        ATL::CComVariant::CComVariant (4112BCh) 
    00415B50  mov         dword ptr [ebp-17Ch],eax 
    00415B56  mov         eax,dword ptr [ebp-17Ch] 
    00415B5C  mov         dword ptr [ebp-180h],eax 
    00415B62  mov         byte ptr [ebp-4],1 
    00415B66  mov         ecx,dword ptr [ebp-180h] 
    00415B6C  mov         edx,dword ptr [ecx] 
    00415B6E  mov         dword ptr [ebp-174h],edx 
    00415B74  mov         eax,dword ptr [ecx+4] 
    00415B77  mov         dword ptr [ebp-170h],eax 
    00415B7D  mov         edx,dword ptr [ecx+8] 
    00415B80  mov         dword ptr [ebp-16Ch],edx 
    00415B86  mov         eax,dword ptr [ecx+0Ch] 
    00415B89  mov         dword ptr [ebp-168h],eax 
    00415B8F  lea         ecx,[spDoc] 
    00415B92  call        ATL::CComPtrBase<IHTMLDocument2>::operator-> (411663h) 
    00415B97  mov         dword ptr [ebp-184h],eax 
    00415B9D  mov         esi,esp 
    00415B9F  sub         esp,10h 
    00415BA2  mov         ecx,esp 
    00415BA4  mov         edx,dword ptr [ebp-174h] 
    00415BAA  mov         dword ptr [ecx],edx 
    00415BAC  mov         eax,dword ptr [ebp-170h] 
    00415BB2  mov         dword ptr [ecx+4],eax 
    00415BB5  mov         edx,dword ptr [ebp-16Ch] 
    00415BBB  mov         dword ptr [ecx+8],edx 
    00415BBE  mov         eax,dword ptr [ebp-168h] 
    00415BC4  mov         dword ptr [ecx+0Ch],eax 
    00415BC7  mov         ecx,dword ptr [ebp-184h] 
    00415BCD  push        ecx  
    00415BCE  mov         edx,dword ptr [ebp-184h] 
    00415BD4  mov         eax,dword ptr [edx] 
    00415BD6  mov         ecx,dword ptr [eax+70h] 
    00415BD9  call        ecx  
    00415BDB  cmp         esi,esp 
    00415BDD  call        @ILT+1880(__RTC_CheckEsp) (41175Dh) 
    00415BE2  mov         byte ptr [ebp-4],0 
    00415BE6  lea         ecx,[ebp-15Ch] 
    00415BEC  call        ATL::CComVariant::~CComVariant (411177h) 
    							// spDoc->put_bgColor( CComBSTR(L"red") );
    							ShowMessage( _T("Congraturations to you, you have change the background color to red."));
    00415BF1  push        offset string L"Congraturations to y"... (429648h) 
    00415BF6  call        ShowMessage (411348h) 
    00415BFB  add         esp,4

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    The code in the article has a typo

    hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
    should be

    hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument2, 0, (void**)&spDoc );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM