原文转载:http://www.cnblogs.com/chenzhifei/archive/2008/11/12/1332300.html
原文中代码编译不成功,其中有修改的地方标红。
#include <mshtml.h>
#include <atlbase.h>
#include <oleacc.h>
<pre name="code" class="cpp">IHTMLDocument2* GetDocInterface(HWND hWnd)
{
CoInitialize(NULL);
IHTMLDocument2* pDoc2 = 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(hWnd, EnumChildProc, (LPARAM)&hWndChild);
EnumWindows(EnumProc, (LPARAM)&hWndChild);
if (hWndChild)
{
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, _T("ObjectFromLresult"));
if (pfObjectFromLresult != NULL)
{
HRESULT hr;
hr = (*pfObjectFromLresult)(lRes, IID_IHTMLDocument, 0, (void**)&spDoc);
if (SUCCEEDED(hr))
{
CComPtr<IDispatch> spDisp; // 源代码为CComPtr spDisp;
CComQIPtr<IHTMLWindow2> spWin;// 源代码为CComQIPtr spWin;
spDoc->get_Script(&spDisp);
spWin = spDisp;
spWin->get_document(&pDoc2);
}
}
} // else document not ready
} // else Internet Explorer is not running
::FreeLibrary(hInst);
} // else Active Accessibility is not installed
CoUninitialize();
return pDoc2;
}
更多详细内容转载:http://www.cnblogs.com/chenzhifei/archive/2008/11/12.html