IE编程---DLL绑定 选择自 solarsoft 的 Blog
关键字 IE,VC COM,ALT
出处
IE编程---DLL绑定
我查阅了一些资料,但写关于IE的DLL绑定的文章并不多,而且,写得不是很清楚。为此,我想介绍一下,这个方法:
基础知识:VC COM,(ALT方法来做)
IE的启动过程:
当IE的实例启动的时候,它会在注册表中寻找一个CLSID,具体位置在
HKEY_LOCALL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects
当这里存在一CLSID时,IE会建立一个对象的实例。比如:{B5D4581D-ED6A-4905-A267-25BAF7BE79C1}如果这个看不懂,你可以去找一下COM开发的书。
开发实例:
VC建立工程,ALT COM AppWizard 注意是DLL.
建立一个新的类,一定要是ALT
在菜单,Insert>New ALT Object:Simple Object
修改并实现方法
// FiltrateIE.h : Declaration of the CFiltrateIE
#ifndef __FILTRATEIE_H_
#define __FILTRATEIE_H_
#include "resource.h" // main symbols
#include <ExDisp.h> //这里必须加上这两个头文件
#include <msHtml.h>
#define MAX_DOCUMENTS 50
/////////////////////////////////////////////////////////////////////////////
// CFiltrateIE
class ATL_NO_VTABLE CFiltrateIE :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CFiltrateIE, &CLSID_FiltrateIE>,
public IObjectWithSiteImpl<CFiltrateIE>,//加入这个,以继承该接口
public IDispatchImpl<IFiltrateIE, &IID_IFiltrateIE, &LIBID_SAFEIELib>
{
public:
CFiltrateIE()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_FILTRATEIE)
DECLARE_NOT_AGGREGATABLE(CFiltrateIE)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(CFiltrateIE)
COM_INTERFACE_ENTRY(IFiltrateIE)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IObjectWithSite)//加入
END_COM_MAP()
// IFiltrateIE
public
//下面两个方法必须实现
STDMETHOD(SetSite)(IUnknown *pUnkSite);
STDMETHOD(Invoke)(DISPID dispidMember,REFIID riid, LCID lcid, WORD wFlags,
DISPPARAMS * pdispparams, VARIANT * pvarResult,
EXCEPINFO * pexcepinfo, UINT * puArgErr);
private:
//下面一个方法必须实现
enum ConnectType {Advise,Unadvise};
BOOL ManageConnection(enum ConnectType action);
CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> m_spWebBrowser2;
HWND m_hwndIE;
DWORD m_dwCookie;
BOOL m_bDocComplete;
BOOL m_test;
};
#endif //__FILTRATEIE_H_
// FiltrateIE.cpp : Implementation of CFiltrateIE
#include "stdafx.h"
#include "SafeIE.h"
#include "FiltrateIE.h"
#include <tchar.h>
#include <ExDisp.h>
#include <ExDispID.h>
#include <comdef.h>
#include <Commctrl.h>
#include <strstrea.h>
/////////////////////////////////////////////////////////////////////////////
// CFiltrateIE
BOOL CFiltrateIE::ManageConnection(enum ConnectType action)
{
if (!m_spWebBrowser2)
return S_OK;
HRESULT hr;
CComQIPtr<IConnectionPointContainer, &IID_IConnectionPointContainer> spCPContainer(m_spWebBrowser2);
if (spCPContainer != NULL)
{
CComPtr<IConnectionPoint> spConnectionPoint;
hr = spCPContainer->FindConnectionPoint(DIID_DWebBrowserEvents2, &spConnectionPoint);
if (SUCCEEDED(hr))
{
if (action == Advise)
// Advise the client site that we want to sink events
hr = spConnectionPoint->Advise((IDispatch*)this, &m_dwCookie);
else
// Remove us from the list of active sinks
hr = spConnectionPoint->Unadvise(m_dwCookie);
}
}
return (SUCCEEDED(hr));
}
STDMETHODIMP CFiltrateIE::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags,
DISPPARAMS* pDispParams, VARIANT* pvarResult,
EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
m_test=TRUE;
if (!pDispParams)
return E_INVALIDARG;
CComPtr<IDispatch> spDisp;
HRESULT hr;
switch (dispidMember)
{
case DISPID_BEFORENAVIGATE2:
m_bDocComplete = FALSE;
m_spWebBrowser2 = pDispParams->rgvarg[6].pdispVal;
hr = m_spWebBrowser2->get_Document(&spDisp);
if (SUCCEEDED(hr))
{
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
spHTML = spDisp;
// if (spHTML)
// RemoveDocument(spHTML);
}
break;
case DISPID_NAVIGATECOMPLETE2:
//
/* if (m_spWebBrowser2){
_bstr_t bsAddress=m_spWebBrowser2->GetLocationURL();
LPCTSTR str1,str2;
str1="SolarSafe.htm";
str2=(LPCTSTR)bsAddress;
if (strstr(str2,str1)!=NULL)
{
ShellExecute((HWND)m_spWebBrowser2->HWND,"open","C:\\solarsafe.htm",NULL,NULL,SW_SHOWNORMAL);
}
*/
break;
// [0]: New status bar text - VT_BSTR
//
/*
case DISPID_STATUSTEXTCHANGE:
break;
*/
// [0]: Maximum progress - VT_I4
// [1]: Amount of total progress - VT_I4
//
/*
case DISPID_PROGRESSCHANGE:
break;
*/
// [0]: Document URL - VT_BYREF|VT_VARIANT
// [1]: An object that evaluates to the top-level or frame
// WebBrowser object corresponding to the event.
case DISPID_DOCUMENTCOMPLETE:
// Temporarily disable keyboard hook
if(m_test)
{//ShellExecute(m_hwndIE,"open","C:\\solarsafe.htm",NULL,NULL,SW_SHOWNORMAL);
MessageBox(m_hwndIE, "AAAA", "A", MB_OK );
// m_spWebBrowser2->Quit();
m_test=FALSE;
}
// Fill in form if necessary
m_spWebBrowser2 = pDispParams->rgvarg[1].pdispVal;
// Get the WebBrowser's document object
hr = m_spWebBrowser2->get_Document(&spDisp);
if (SUCCEEDED(hr))
{
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
spHTML = spDisp;
// if (spHTML && DoFillIn(spHTML))
// AddDocument(spHTML);
}
m_bDocComplete = TRUE;
// Temporarily disable keyboard hook
break;
// No parameters
/*
case DISPID_DOWNLOADBEGIN:
break;
*/
// No parameters
/*
case DISPID_DOWNLOADCOMPLETE:
break;
*/
// [0]: Enabled state - VT_BOOL
// [1]: Command identifier - VT_I4
//
/*
case DISPID_COMMANDSTATECHANGE:
break;
*/
// [0]: Address of cancel flag - VT_BYREF|VT_BOOL
//
/*
case DISPID_NEWWINDOW2:
break;
*/
// [0]: Document title - VT_BSTR
// [1]: An object that evaluates to the top-level or frame
// WebBrowser object corresponding to the event.
//
/*
case DISPID_TITLECHANGE:
break;
*/
// [0]: Name of property that changed - VT_BSTR
//
/*
case DISPID_PROPERTYCHANGE:
break;
*/
// [0]: Address of cancel flag - VT_BYREF|VT_BOOL
//
case DISPID_ONQUIT:
// Unregister the keyboard hook previously installed
// for this instance.
ManageConnection(Unadvise);
break;
default:
break;
}
return S_OK;
}
STDMETHODIMP CFiltrateIE::SetSite(IUnknown *pUnkSite)
{
if (pUnkSite != NULL)
{
// Query pUnkSite for the IWebBrowser2 interface.
m_spWebBrowser2 = pUnkSite;
if (m_spWebBrowser2)
{
m_spWebBrowser2->get_HWND((long*) &m_hwndIE);
// We'll need the this pointer later when the keyboard
// hook will be called.
// if (!AddInstance(m_hwndIE, this, GetCurrentThreadId()))
// FormWhizError(IDS_ERROR_TOOMUCHWINDOWS, errError);
// Connect to the browser in order to handle events.
if (!ManageConnection(Advise))
MessageBox(m_hwndIE, "AAAA", "A", MB_OK );
// FormWhizError(IDS_ERROR_SINKINGFAILED, errError);
}
}
return S_OK;
}
找到.rgs文件,加入
HKLM
{
SOFTWARE
{
Microsoft
{
Windows
{
CurrentVersion
{
Explorer
{
'Browser Helper Objects'
{
ForceRemove {B5D4581D-ED6A-4905-A267-25BAF7BE79C1} = s 'SafeIE Utility'
}
}
}
}
}
}
}
上边的{B5D4581D-ED6A-4905-A267-25BAF7BE79C1}在这个文件中可以找到,注意,你做的CSLID肯定不一样。
一切OK,想试一下,可以先看一下,本工程代码