需要攔截並獲取到瀏覽器中alert的信息。實際上就是Hook IHTMLWindow2.alert 接口 .
首先在引入Mshtml.h前,需要定義#define CINTERFACE,強制使用C類型的COM接口。
#define CINTERFACE
#include <MsHTML.h>
#include <exdispid.h>
#include <mshtmcid.h> // CMDSETID_Forms3 definition
#include <mshtmhst.h> // IDM_menu item definitions
我比較偷懶,直接使用detours來hook COM.
#include <detours.h>
#pragma comment(lib, "detours.lib")
對COM接口的hook, 在detours中和 API hook非常近似。首先定義函數指針原型。
typedef HRESULT (STDMETHODCALLTYPE *PFN_IHTMLWindow2_alert)(IHTMLWindow2 * This, BSTR message);
然後弄一個替代函數
// static
HRESULT STDMETHODCALLTYPE CWebBrowser::IHTMLWindow2_alert(IHTMLWindow2 * This, BSTR message)
{
return S_OK;
}
IHTMLWindow2 * pWnd2 = ...
// hook IHTMLWindow2.alert
if( pWnd2 )
{
PFN_IHTMLWindow2_alert pfnRealAlert = pWnd2->lpVtbl->alert;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)pfnRealAlert, IHTMLWindow2_alert);
DetourTransactionCommit();
}
大功告成