IHTMLDocument access violation

TADICAN 2009-11-18 10:25:18
先贴代码

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

请问这个是怎么回事?

...全文
108 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
有没有谁能用IE6试一下?
谢谢啦先
skyxie 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 tadican 的回复:]
引用 11 楼 skyxie 的回复:
在UI线程中调用,肯定没有问题!

试了,有问题。报同样的错。
[/Quote]
没有道理,我前端时间才对qq web游戏中的一个flash进行操作过,就是用的这个方法。
和你上面的代码,差别就是
1)我是在UI线程中操作的。
2)我的是IE6
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 skyxie 的回复:]
在UI线程中调用,肯定没有问题!
[/Quote]
试了,有问题。报同样的错。
skyxie 2009-11-19
  • 打赏
  • 举报
回复
在UI线程中调用,肯定没有问题!
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
我用的是IE8,这个应该没有影响对吧?
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tr0j4n 的回复:]
代码发我。biweilun#live.com
[/Quote]
我把代码整理了一下,已经发送到你邮箱。顺便在这里再贴一遍。

// 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;
}

MoXiaoRab 2009-11-19
  • 打赏
  • 举报
回复
代码发我。biweilun#live.com
MoXiaoRab 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tadican 的回复:]
引用 4 楼 tr0j4n 的回复:
C/C++ codeVARIANT color;       
color.vt= VT_BSTR;       
color.bstrVal= SysAllocString(L"red");     

spDoc->put_bgColor(CComVariant redColor(color));
Try it

问题依旧,报错也和前面一样。
[/Quote]
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tr0j4n 的回复:]
C/C++ codeVARIANT color;
color.vt= VT_BSTR;
color.bstrVal= SysAllocString(L"red");

spDoc->put_bgColor(CComVariant redColor(color));
Try it
[/Quote]
问题依旧,报错也和前面一样。
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangjia184 的回复:]
CComVariant("red")  -> CComBSTR(_T("red"))
[/Quote]
cannot convert parameter 1 from 'ATL::CComBSTR' to 'VARIANT'
MoXiaoRab 2009-11-19
  • 打赏
  • 举报
回复
VARIANT color;         
color.vt = VT_BSTR;
color.bstrVal = SysAllocString(L"red");

spDoc->put_bgColor(CComVariant redColor(color));

Try it
CodeProject-Jerry 2009-11-19
  • 打赏
  • 举报
回复
CComVariant("red") -> CComBSTR(_T("red"))
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 skyxie 的回复:]
SUCCEEDED(hr) 并不保证 spDoc 非空,你先看看 spDoc 是否为NULL
[/Quote]
我忘记说了,spDoc非NULL,这个调试过。
skyxie 2009-11-19
  • 打赏
  • 举报
回复
SUCCEEDED(hr) 并不保证 spDoc 非空,你先看看 spDoc 是否为NULL
TADICAN 2009-11-19
  • 打赏
  • 举报
回复

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;
TADICAN 2009-11-19
  • 打赏
  • 举报
回复
调试的时候报权限不够。
我怀疑权限问题是不是直接原因,是不是有其它因素导致这个问题?

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧