How to get IHTMLDocument2 from a HWND
来源:互联网 发布:网络文化建设和管理 编辑:程序博客网 时间:2024/05/01 02:10
http://support.microsoft.com/kb/249232
This article shows how to get the IHTMLDocument2 interface from a HWND. If Microsoft Active Accessibility (MSAA) is installed, you can send the WM_HTML_GETOBJECT message to the document's window (with the window class "Internet Explorer_Server") and then pass the result from SendMessage to an MSAA function, ObjectFromLresult, to get a fully marshaled IHTMLDocument2 pointer.
Back to the top
MORE INFORMATIONYou must have Active Accessibility components installed on the system for the c...
The following code demonstrates how to allow multiple instances of the Internet Explorer to be registered in the ROT using item moniker.
///////////////////////////////////////////////////////////////////////////// IEHELPER.H
#ifndef __IEHELPER_H_
#define __IEHELPER_H_
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CIEHelper
class CIEHelper :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CIEHelper, &CLSID_IEHelper>,
public IObjectWithSiteImpl<CIEHelper>,
public IIEHelper
{
public:
CIEHelper()
{
m_dwROTID = 0;
}
~CIEHelper()
{
}
// IObjectWithSite interface
STDMETHOD(SetSite)(IUnknown* pUnkSite);
DECLARE_REGISTRY_RESOURCEID(IDR_IEHELPER)
BEGIN_COM_MAP(CIEHelper)
COM_INTERFACE_ENTRY(IIEHelper)
COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()
// IIEHelper
public:
void SetROTID(DWORD dwValue)
{
m_dwROTID = dwValue;
}
private:
DWORD m_dwROTID;
void ROT_UnRegister();
void ROT_Register();
};
#endif //__IEHELPER_H_
/////////////////////////////////////////////////////////////////// IEHELPER.CPP
// IEHelper.cpp : Implementation of CIEHelper
#include "stdafx.h"
#include "IE.h"
#include "IEHelper.h"
#include <initguid.h>
#include <exdisp.h> // the IID_IWebBrowser is located over there
#include <stdio.h>
/////////////////////////////////////////////////////////////////////////////
// CIEHelper
//
// CObjectWithSite Methods
//
STDMETHODIMP CIEHelper::SetSite(IUnknown* pUnkSite)
{
USES_CONVERSION;
// remove previously registered object from the table
ROT_UnRegister();
// neccessary initialization
HRESULT hResult = IObjectWithSiteImpl<CIEHelper>::SetSite(pUnkSite);
// register helper object in the running object table
ROT_Register();
return hResult;
}
///////////////////////////////////////////////////////////////////////////
//
//
// Private Section
//
const static TCHAR* szMonikerTemplateName = _T("Browser Helper Object");
//
// register the object in the running object table
//
void CIEHelper::ROT_Register()
{
CComPtr<IRunningObjectTable> pTable;
if(GetRunningObjectTable(0, &pTable) == S_OK)
{
CComBSTR bstrItemName;
CComPtr<IMoniker> pMoniker;
TCHAR szBuffer[1024];
sprintf(szBuffer, "%s | %d", szMonikerTemplateName, m_spUnkSite);
bstrItemName = szBuffer;
if(CreateItemMoniker(NULL, bstrItemName, &pMoniker) == S_OK)
{
DWORD dwRegister;
if(pTable->Register(1, (IUnknown*)this, pMoniker, &dwRegister) == S_OK)
{
SetROTID(dwRegister);
ATLTRACE("IIEHelper: the moniker %s has been registered successfully/n", szBuffer);
}
else
{
ATLTRACE("IIEHelper: failed to register moniker % s in the ROT/n", szBuffer);
}
}
else
{
ATLTRACE("IIEHelper: failed to create moniker %s/n", szBuffer);
}
}
else
{
ATLTRACE("IIEHelper: Could not retrieve the ROT pointer/n");
}
}
void CIEHelper::ROT_UnRegister()
{
CComPtr<IRunningObjectTable> pTable;
if(GetRunningObjectTable(0, &pTable) == S_OK)
{
HRESULT hRes = pTable->Revoke(m_dwROTID);
ATLTRACE("IIEHelper: ID = %d, result code %x/n", m_dwROTID, hRes);
}
SetROTID(0);
}
////////////////////////////////////////////////////////////////////// IEHELPER.RGS
// The following lines should be included into the self-registration portion of
// COM application.
...
HKLM
{
SOFTWARE
{
Microsoft
{
Windows
{
CurrentVersion
{
Explorer
{
'Browser Helper Objects'
{
<CLSID>
}
}
}
}
}
}
}
Having implemented and registered the Browser Helper, an application can now obtain the pointer to a running instance of IE by traversing the ROT.
The following example shows the rough method I used to list the instances of IE:
IRunningObjectTable* pTable;
if(!FAILED(GetRunningObjectTable(0, &pTable)))
{
IEnumMoniker* pEnum;
if(!FAILED(pTable->EnumRunning(&pEnum)))
{
IUnknown* pUnknown;
IMoniker* pCurMoniker = NULL;
LPMALLOC pMalloc;
while (pEnum->Next(1, &pCurMoniker, NULL) == S_OK)
{
if(!FAILED(pTable->GetObject(pCurMoniker, &pUnknown)))
{
IObjectWithSite* pSite;
if(!FAILED(pUnknown->QueryInterface(IID_IObjectWithSite, (void**)&pSite)))
{
IDispatch* pDispatch;
if(pSite->GetSite(IID_IDispatch, (void**)&pDispatch) == S_OK)
{
CWebBrowserApp browser;
browser.AttachDispatch(pDispatch);
TRACE("LocationURL: %s/n", browser.GetLocationURL());
TRACE("LocationName: %s/n", browser.GetLocationName());
browser.DetachDispatch();
pDispatch->Release();
}
pSite->Release();
}
pUnknown->Release();
}
pCurMoniker->Release();
}
pEnum->Release();
}
pTable->Release();
}
MORE INFORMATION
The following file is available for download from the Microsoft Software Library:
- How to get IHTMLDocument2 from a HWND
- How to get an X11 Window from a Process ID?
- How to Get WiFi From a Mile Away
- [cernRoot] how to get contours from a TH2D
- Java How to get the PID from a process?
- how to get internal items from a dialog created from wizard of VC2005
- how to get a polygon
- How to get a solution?
- How to get the DOM of a WebBrowser control from a window handle (VB6)
- How to get file from classpath
- How to get parameters from the URL?
- How to get bean from tag component
- How to get byte array from imageview
- How to get UITableView from UITableViewCell?
- How to get UITableView from UITableViewCell?
- How to get kernel messages from Android?
- How to get source code from APK
- ASP.NET 2.0: How to get a specific ConnectionString from the Web.Config by name
- null 与 equals判断顺序引起的头疼事
- 在MFC中获得应用程序主窗口的指针
- 三层架构和mvc模式
- 做不了爱人,我们做什么
- 最近工作参考文献
- How to get IHTMLDocument2 from a HWND
- ajax实例
- oracle在linux下怎么安装
- 爱情和婚姻
- Linux 下共享内存建立时需要注意的问题
- RCP 带复选框的树的级联选择与取消
- 捕捉用户向DataGridView的单元格输入不正确值时发生的错误
- 备案网站打不开,如何备案?
- .Net基础:C#中对DatagridView部分常用操作