全局HOOK与IE接口 0x8001010D错误
最近在做一个自动测试工具,在做到web部分的时候,需要从浏览器窗体中去获取IHTMLDocument2接口以获得所操作的控件的信息,但是遇到一个0x8001010D错误,错误信息为 First-chance exception at 0x75a29617 in Hook.exe: 0x8001010D: 因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫。
我的在全局鼠标钩子的回调函数中使用以下代码:
IHTMLDocument2* GetDocInterface(HWND hWnd)
{
// 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA
CoInitialize( NULL );
HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
IHTMLDocument2* pDoc2=NULL;
if ( hInst != NULL ){
if ( hWnd != NULL ){
CComPtr<IHTMLDocument> spDoc=NULL;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
/*clock_t time = clock();
CString strTime;
strTime.Format(_T("%d"),time);
OutputDebugString(strTime);*/
OutputDebugString(_T("Before SendMessage \n"));
int re = ::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
CString result;
result.Format(_T("SendMessage re:%d \n"),re);
OutputDebugString(result);
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, "ObjectFromLresult" );
OutputDebugStringA("getAddress \n");
if ( pfObjectFromLresult != NULL )
{
HRESULT hr;
//hr = ObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc);
hr=(*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
OutputDebugStringA("Get Object:");
if ( SUCCEEDED(hr) ){
OutputDebugStringA(" Success !\n");
CComPtr<IDispatch> spDisp;
CComQIPtr<IHTMLWindow2> spWin;
spDoc->get_Script( &spDisp );
spWin = spDisp;
spWin->get_document( &pDoc2 );
}
else
{
OutputDebugStringA("Fail ! \n");
}
}
}
::FreeLibrary(hInst);
}
else{//如果没有安装MSAA
//MessageBox(_T("请您安装Microsoft Active Accessibility"));
}
char PDoc2[100];
CString temp = _T("the return value is :");
temp += itoa((int)pDoc2,PDoc2,16);
OutputDebugString(temp + _T("\n"));
CoUninitialize();
return pDoc2;
}
代码是在执行到hr=(*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );的时候,发生了0x8001010D的错误,所以从VS2008的输出窗口上可以看到如下信息:
First-chance exception at 0x75a29617 in Hook.exe: 0x8001010D: 因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫。.
Get Object:Fail !
我想问下,这个错误是由全局钩子造成的么?因为在我不用Hook,而是通过别的窗体事件来激发上面的GetDocInterface函数,这段代码可以正确的执行。
想请教有做过类似的项目的大哥给点提示,告诉我错误的原因。小弟万分感谢