先贴代码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 请问这个是怎么回事?
if ( SUCCEEDED(hr) )
{
// Change background color to red
spDoc->put_bgColor( CComVariant("red") );//这里报acces violation
}
这个是msdn的一个例子http://support.microsoft.com/kb/249232 请问这个是怎么回事?
color.vt = VT_BSTR;
color.bstrVal = SysAllocString(L"red"); spDoc->put_bgColor(CComVariant redColor(color));
Try it
//
#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;
}
和你上面的代码,差别就是
1)我是在UI线程中操作的。
2)我的是IE6
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;