get COM interface method address .

来源:互联网 发布:动力学分析软件 编辑:程序博客网 时间:2024/05/16 15:38

在r3 hook之前, 需要得到API地址.

如果要 hook 的是COM接口中的方法, 得到API地址的方法和得到普通Win32API地址的方法不同.


[cpp] view plaincopyprint?
  1. /// @file       prjGetComInterfaceMethodAddr.cpp  
  2. /// @brief      得到COM接口方法地址   
  3.   
  4. #include "stdafx.h" ///< 由 <WinInet.h> 包含COM接口定义  
  5. #include "prjGetComInterfaceMethodAddr.h"  
  6.   
  7. /// 自己从C++接口定义中拷贝出来的C风格接口定义   
  8. /// 当前是C++程序,无法直接使用C风格接口定义   
  9. /// 使用C风格接口定义,是为了得到COM接口虚表中的方法地址  
  10. #include "ctype_interace.h"   
  11.   
  12. #ifdef _DEBUG   
  13. #define new DEBUG_NEW   
  14. #endif   
  15.   
  16. // The one and only application object   
  17.   
  18. CWinApp theApp;  
  19.   
  20. using namespace std;  
  21.   
  22. /// 得到 IWebBrowser::get_LocationURL 的函数地址   
  23. ULONG_PTR GetComApiInterfaceAddr_IWebBrowser_get_LocationURL();  
  24.   
  25. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])  
  26. {  
  27.     DWORD_PTR   dwAddr = 0;  
  28.     int         nRetCode = 0;  
  29.   
  30.     HMODULE hModule = ::GetModuleHandle(NULL);  
  31.   
  32.     if (hModule != NULL)  
  33.     {  
  34.         // initialize MFC and print and error on failure  
  35.         if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))  
  36.         {  
  37.             // TODO: change error code to suit your needs  
  38.             _tprintf(_T("Fatal Error: MFC initialization failed\n"));  
  39.             nRetCode = 1;  
  40.         }  
  41.         else  
  42.         {  
  43.             dwAddr = GetComApiInterfaceAddr_IWebBrowser_get_LocationURL();  
  44.             _tprintf(  
  45.                 L"GetComApiInterfaceAddr_"  
  46.                 L"IWebBrowser_get_LocationURL = 0x%X\r\n",   
  47.                 dwAddr);  
  48.         }  
  49.     }  
  50.     else  
  51.     {  
  52.         // TODO: change error code to suit your needs  
  53.         _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));  
  54.         nRetCode = 1;  
  55.     }  
  56.   
  57.     /** runresults 
  58.     GetComApiInterfaceAddr_IWebBrowser_get_LocationURL = 0x5D9C680C 
  59.     */  
  60.     getwchar();  
  61.     return nRetCode;  
  62. }  
  63.   
  64. typedef HRESULT (STDMETHODCALLTYPE * PFN_get_LocationURL)(   
  65.     IWebBrowser * This,  
  66.     BSTR *LocationURL);  
  67.   
  68. DWORD_PTR GetComApiInterfaceAddr_IWebBrowser_get_LocationURL()  
  69. {  
  70.     DWORD_PTR   dwAddr = 0;  
  71.     HRESULT hr;  
  72.     IWebBrowser* pInterface = NULL;  
  73.     ctype_IWebBrowser * pCtypeInterface = NULL;  
  74.     PFN_get_LocationURL pfn_get_LocationURL = NULL;  
  75.       
  76.     CoInitialize ( NULL );  
  77.     hr = CoCreateInstance ( CLSID_WebBrowser,  
  78.         NULL,  
  79.         CLSCTX_INPROC_SERVER,  
  80.         IID_IWebBrowser,  
  81.         (void**) &pInterface);  
  82.   
  83.     if (SUCCEEDED(hr))  
  84.     {  
  85.         pCtypeInterface = (ctype_IWebBrowser *)pInterface;  
  86.         pfn_get_LocationURL = pCtypeInterface->lpVtbl->get_LocationURL;  
  87.         dwAddr = (DWORD_PTR)pfn_get_LocationURL;  
  88.   
  89.         pInterface->Release();  
  90.     }  
  91.   
  92.     CoUninitialize();  
  93.   
  94.     return dwAddr;  
  95. }  

[cpp] view plaincopyprint?
  1. // stdafx.h : include file for standard system include files,  
  2. // or project specific include files that are used frequently, but  
  3. // are changed infrequently   
  4. //   
  5.   
  6. #pragma once   
  7.   
  8. #include "targetver.h"   
  9.   
  10. #include <stdio.h>   
  11. #include <tchar.h>   
  12. #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit  
  13.   
  14. #ifndef VC_EXTRALEAN   
  15. #define VC_EXTRALEAN            // Exclude rarely-used stuff from Windows headers  
  16. #endif   
  17.   
  18. #include <afx.h>   
  19. #include <afxwin.h>         // MFC core and standard components  
  20. #include <WinInet.h>        ///< for COM Interface !  
  21. #include <afxext.h>         // MFC extensions  
  22. #ifndef _AFX_NO_OLE_SUPPORT   
  23. #include <afxdtctl.h>           // MFC support for Internet Explorer 4 Common Controls  
  24. #endif   
  25. #ifndef _AFX_NO_AFXCMN_SUPPORT  
  26. #include <afxcmn.h>                     // MFC support for Windows Common Controls  
  27. #endif // _AFX_NO_AFXCMN_SUPPORT  
  28.   
  29. #include <iostream>   
  30.   
  31.   
  32.   
  33. // TODO: reference additional headers your program requires here  

[cpp] view plaincopyprint?
  1. /// @file       ctype_interace.h   
  2. /// @brief      C风格的接口与方法定义   
  3. ///             在C++程序中, 为了使用COM接口的虚表指针,   
  4. ///             如果该COM接口为C++风格和C风格接口混合提供,   
  5. ///             需要将C风格的接口拷贝出来改名使用  
  6. ///             直接包含头文件,无法编译通过   
  7.   
  8. #ifndef __CTYPE_INTERACE_H__   
  9. #define __CTYPE_INTERACE_H__  
  10.   
  11. /* C style interface */  
  12.   
  13. typedef struct IWebBrowserVtbl  
  14. {  
  15.     BEGIN_INTERFACE  
  16.   
  17.         HRESULT ( STDMETHODCALLTYPE *QueryInterface )(   
  18.         __RPC__in IWebBrowser * This,  
  19.         /* [in] */ __RPC__in REFIID riid,  
  20.         /* [annotation][iid_is][out] */   
  21.         __RPC__deref_out  void **ppvObject);  
  22.   
  23.         ULONG ( STDMETHODCALLTYPE *AddRef )(   
  24.             __RPC__in IWebBrowser * This);  
  25.   
  26.         ULONG ( STDMETHODCALLTYPE *Release )(   
  27.             __RPC__in IWebBrowser * This);  
  28.   
  29.         HRESULT ( STDMETHODCALLTYPE *GetTypeInfoCount )(   
  30.             __RPC__in IWebBrowser * This,  
  31.             /* [out] */ __RPC__out UINT *pctinfo);  
  32.   
  33.         HRESULT ( STDMETHODCALLTYPE *GetTypeInfo )(   
  34.             __RPC__in IWebBrowser * This,  
  35.             /* [in] */ UINT iTInfo,  
  36.             /* [in] */ LCID lcid,  
  37.             /* [out] */ __RPC__deref_out_opt ITypeInfo **ppTInfo);  
  38.   
  39.         HRESULT ( STDMETHODCALLTYPE *GetIDsOfNames )(   
  40.             __RPC__in IWebBrowser * This,  
  41.             /* [in] */ __RPC__in REFIID riid,  
  42.             /* [size_is][in] */ __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames,  
  43.             /* [range][in] */ __RPC__in_range(0,16384) UINT cNames,  
  44.             /* [in] */ LCID lcid,  
  45.             /* [size_is][out] */ __RPC__out_ecount_full(cNames) DISPID *rgDispId);  
  46.   
  47.         /* [local] */ HRESULT ( STDMETHODCALLTYPE *Invoke )(   
  48.             IWebBrowser * This,  
  49.             /* [in] */ DISPID dispIdMember,  
  50.             /* [in] */ REFIID riid,  
  51.             /* [in] */ LCID lcid,  
  52.             /* [in] */ WORD wFlags,  
  53.             /* [out][in] */ DISPPARAMS *pDispParams,  
  54.             /* [out] */ VARIANT *pVarResult,  
  55.             /* [out] */ EXCEPINFO *pExcepInfo,  
  56.             /* [out] */ UINT *puArgErr);  
  57.   
  58.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *GoBack )(   
  59.             __RPC__in IWebBrowser * This);  
  60.   
  61.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *GoForward )(   
  62.             __RPC__in IWebBrowser * This);  
  63.   
  64.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *GoHome )(   
  65.             __RPC__in IWebBrowser * This);  
  66.   
  67.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *GoSearch )(   
  68.             __RPC__in IWebBrowser * This);  
  69.   
  70.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *Navigate )(   
  71.             __RPC__in IWebBrowser * This,  
  72.             /* [in] */ __RPC__in BSTR URL,  
  73.             /* [unique][optional][in] */ __RPC__in_opt VARIANT *Flags,  
  74.             /* [unique][optional][in] */ __RPC__in_opt VARIANT *TargetFrameName,  
  75.             /* [unique][optional][in] */ __RPC__in_opt VARIANT *PostData,  
  76.             /* [unique][optional][in] */ __RPC__in_opt VARIANT *Headers);  
  77.   
  78.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *Refresh )(   
  79.             __RPC__in IWebBrowser * This);  
  80.   
  81.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *Refresh2 )(   
  82.             __RPC__in IWebBrowser * This,  
  83.             /* [unique][optional][in] */ __RPC__in_opt VARIANT *Level);  
  84.   
  85.         /* [helpcontext][helpstring][id] */ HRESULT ( STDMETHODCALLTYPE *Stop )(   
  86.             __RPC__in IWebBrowser * This);  
  87.   
  88.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Application )(   
  89.             __RPC__in IWebBrowser * This,  
  90.             /* [retval][out] */ __RPC__deref_out_opt IDispatch **ppDisp);  
  91.   
  92.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Parent )(   
  93.             __RPC__in IWebBrowser * This,  
  94.             /* [retval][out] */ __RPC__deref_out_opt IDispatch **ppDisp);  
  95.   
  96.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Container )(   
  97.             __RPC__in IWebBrowser * This,  
  98.             /* [retval][out] */ __RPC__deref_out_opt IDispatch **ppDisp);  
  99.   
  100.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Document )(   
  101.             __RPC__in IWebBrowser * This,  
  102.             /* [retval][out] */ __RPC__deref_out_opt IDispatch **ppDisp);  
  103.   
  104.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_TopLevelContainer )(   
  105.             __RPC__in IWebBrowser * This,  
  106.             /* [retval][out] */ __RPC__out VARIANT_BOOL *pBool);  
  107.   
  108.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Type )(   
  109.             __RPC__in IWebBrowser * This,  
  110.             /* [retval][out] */ __RPC__deref_out_opt BSTR *Type);  
  111.   
  112.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Left )(   
  113.             __RPC__in IWebBrowser * This,  
  114.             /* [retval][out] */ __RPC__out long *pl);  
  115.   
  116.         /* [propput][id] */ HRESULT ( STDMETHODCALLTYPE *put_Left )(   
  117.             __RPC__in IWebBrowser * This,  
  118.             /* [in] */ long Left);  
  119.   
  120.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Top )(   
  121.             __RPC__in IWebBrowser * This,  
  122.             /* [retval][out] */ __RPC__out long *pl);  
  123.   
  124.         /* [propput][id] */ HRESULT ( STDMETHODCALLTYPE *put_Top )(   
  125.             __RPC__in IWebBrowser * This,  
  126.             /* [in] */ long Top);  
  127.   
  128.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Width )(   
  129.             __RPC__in IWebBrowser * This,  
  130.             /* [retval][out] */ __RPC__out long *pl);  
  131.   
  132.         /* [propput][id] */ HRESULT ( STDMETHODCALLTYPE *put_Width )(   
  133.             __RPC__in IWebBrowser * This,  
  134.             /* [in] */ long Width);  
  135.   
  136.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Height )(   
  137.             __RPC__in IWebBrowser * This,  
  138.             /* [retval][out] */ __RPC__out long *pl);  
  139.   
  140.         /* [propput][id] */ HRESULT ( STDMETHODCALLTYPE *put_Height )(   
  141.             __RPC__in IWebBrowser * This,  
  142.             /* [in] */ long Height);  
  143.   
  144.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_LocationName )(   
  145.             __RPC__in IWebBrowser * This,  
  146.             /* [retval][out] */ __RPC__deref_out_opt BSTR *LocationName);  
  147.   
  148.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_LocationURL )(   
  149.             __RPC__in IWebBrowser * This,  
  150.             /* [retval][out] */ __RPC__deref_out_opt BSTR *LocationURL);  
  151.   
  152.         /* [helpcontext][helpstring][propget][id] */ HRESULT ( STDMETHODCALLTYPE *get_Busy )(   
  153.             __RPC__in IWebBrowser * This,  
  154.             /* [retval][out] */ __RPC__out VARIANT_BOOL *pBool);  
  155.   
  156.     END_INTERFACE  
  157. } IWebBrowserVtbl;  
  158.   
  159. interface ctype_IWebBrowser ///< 改名了  
  160. {  
  161.     CONST_VTBL struct IWebBrowserVtbl *lpVtbl;  
  162. };  
  163.   
  164. #endif  
0 0