先贴代码hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
if ( SUCCEEDED(hr) )
{
// Change background color to red
spDoc->put_bgColor( CComVariant("red") );//这里报acces violation
}
这个是msdn的一个例子http://support.microsoft.com/kb/249232 请问这个是怎么回事?

解决方案 »

  1.   

    SUCCEEDED(hr) 并不保证 spDoc 非空,你先看看 spDoc 是否为NULL
      

  2.   

    我忘记说了,spDoc非NULL,这个调试过。
      

  3.   

    CComVariant("red")  -> CComBSTR(_T("red"))
      

  4.   

    VARIANT color;         
    color.vt = VT_BSTR;         
    color.bstrVal = SysAllocString(L"red");       spDoc->put_bgColor(CComVariant redColor(color)); 
    Try it
      

  5.   

    cannot convert parameter 1 from 'ATL::CComBSTR' to 'VARIANT'
      

  6.   

    我把代码整理了一下,已经发送到你邮箱。顺便在这里再贴一遍。// IHTMLDocumentTest.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <mshtml.h>
    #include <atlbase.h>
    #include <oleacc.h>
    BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
    {
    TCHAR buf[100];
    ::GetClassName( hwnd, (LPTSTR)&buf, 100 );
    if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 )
    {
    *(HWND*)lParam = hwnd;
    return FALSE;
    }
    else
    return TRUE;
    };
    void GetDocInterface(void)
    {
    HWND hWnd = FindWindow(NULL,_TEXT("Mortal Coil - Windows Internet Explorer"));//coil is a flash game
    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,
    //EnumChildWindows continues until the last child window is enumerated or the callback function returns FALSE.
    //If a child window has created child windows of its own, EnumChildWindows enumerates those windows as well. 
    ::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );
    if ( hWndChild )
    {
    CComPtr<IHTMLDocument2> spDoc;
    LRESULT lRes;
    //The RegisterWindowMessage function defines a new window message that is guaranteed to be unique throughout the system.
    //If two different applications register the same message string, the applications return the same message value. The message remains registered until the session ends. 
    UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
    ::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
    LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, "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(_T("red"))/*CComBSTR(_T("red"))*/ );// access violation here
    //VARIANT color; 
    //color.vt = VT_BSTR; 
    //color.bstrVal = SysAllocString(_T("red")); 
    //CComVariant red(color);
    //spDoc->put_bgColor(red); 
    }
    }
    } // else document not ready
    else
    {
    MessageBox(NULL,_T("document not ready"),_T("NULL"),MB_OK);
    }
    } // else Internet Explorer is not running
    else
    {
    MessageBox(NULL,_T("Internet Explorer is not running"),_T("NULL"),MB_OK);
    }
    ::FreeLibrary( hInst );
    } // else Active Accessibility is not installed
    else
    {
    MessageBox(NULL,_T("Active Accessibility is not installed"),_T("NULL"),MB_OK);
    }
    CoUninitialize();
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    GetDocInterface();
    system("pause");
    return 0;
    }
      

  7.   

    没有道理,我前端时间才对qq web游戏中的一个flash进行操作过,就是用的这个方法。
    和你上面的代码,差别就是
    1)我是在UI线程中操作的。
    2)我的是IE6
      

  8.   


    if ( SUCCEEDED(hr) )
    {
    VARIANT v;
    v.vt=VT_BSTR;
    v.bstrVal=L"Red";
    IDispatch* spDisp;
    IHTMLWindow2* spWin;
    spDoc->get_Script(&spDisp );
    spDisp->QueryInterface(IID_IHTMLWindow2,(void**)&spWin);                                                                           
    spWin->get_document(&spDoc);
    spDoc->put_bgColor(v); }
    问题解决了。
    参考 http://book.77169.org/3932/3932067.htm
    http://www.vckbase.com/document/viewdoc/?id=288;