Addin.h
上传用户:tao_1987
上传日期:2019-03-05
资源大小:16k
文件大小:4k
源码类别:

DNA

开发平台:

C/C++

  1. // Addin.h : Declaration of the CAddin
  2. #ifndef __ADDIN_H_
  3. #define __ADDIN_H_
  4. #include "resource.h"       // main symbols
  5. #import "C:Program FilesCommon FilesDesignerMSADDNDR.DLL" raw_interfaces_only, raw_native_types, no_namespace, named_guids 
  6. extern BYTE jmp[5];
  7. extern BYTE enter[5];
  8. typedef HRESULT (WINAPI *PFNQueryContinueDrag)(IDropSource *This,BOOL fEscapePressed,DWORD grfKeyState);
  9. extern PFNQueryContinueDrag pfnQueryContinueDrag;
  10. typedef HRESULT (WINAPI* PFNDoDragDrop)(IDataObject * pDataObject,IDropSource * pDropSource,DWORD dwOKEffect,DWORD * pdwEffect);
  11. extern PFNDoDragDrop pfnDoDragDrop;
  12. extern HANDLE hProcess;
  13. HRESULT WINAPI MyQueryContinueDrag(IDropSource *This,BOOL fEscapePressed,DWORD grfKeyState);
  14. HRESULT WINAPI MyDoDragDrop(IDataObject * pDataObject,IDropSource * pDropSource,DWORD dwOKEffect,DWORD * pdwEffect);
  15. //从SDK头文件里找到的,用于C语言编写COM时用的虚函数表定义
  16. //要知道它才是真的的虚函数表结构
  17. typedef struct IDropSourceVtbl
  18. {
  19. BEGIN_INTERFACE
  20.         
  21.         HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 
  22. IDropSource * This,
  23. /* [in] */ REFIID riid,
  24. /* [iid_is][out] */ void **ppvObject);
  25.         
  26.         ULONG ( STDMETHODCALLTYPE *AddRef )( 
  27. IDropSource * This);
  28. ULONG ( STDMETHODCALLTYPE *Release )( 
  29. IDropSource * This);
  30. HRESULT ( STDMETHODCALLTYPE *QueryContinueDrag )( 
  31. IDropSource * This,
  32. /* [in] */ BOOL fEscapePressed,
  33. /* [in] */ DWORD grfKeyState);
  34.         
  35.         HRESULT ( STDMETHODCALLTYPE *GiveFeedback )( 
  36. IDropSource * This,
  37. /* [in] */ DWORD dwEffect);
  38.         
  39.         END_INTERFACE
  40. } IDropSourceVtbl;
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CAddin
  43. class ATL_NO_VTABLE CAddin : 
  44. public CComObjectRootEx<CComSingleThreadModel>,
  45. public CComCoClass<CAddin, &CLSID_Addin>,
  46. public IDispatchImpl<IAddin, &IID_IAddin, &LIBID_WORDSPYLib>,
  47. public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>
  48. {
  49. public:
  50. CAddin()
  51. {
  52. }
  53. DECLARE_REGISTRY_RESOURCEID(IDR_ADDIN)
  54. DECLARE_PROTECT_FINAL_CONSTRUCT()
  55. BEGIN_COM_MAP(CAddin)
  56. COM_INTERFACE_ENTRY(IAddin)
  57. //DEL  COM_INTERFACE_ENTRY(IDispatch)
  58. COM_INTERFACE_ENTRY2(IDispatch, IAddin)
  59. COM_INTERFACE_ENTRY(_IDTExtensibility2)
  60. END_COM_MAP()
  61. // IAddin
  62. public:
  63. // _IDTExtensibility2
  64. STDMETHOD(OnConnection)(IDispatch * Application, ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
  65. {
  66. //取得Ole32.DLL的句柄
  67. HMODULE hDll=GetModuleHandle(_T("Ole32.dll"));
  68. if(!hDll)
  69. {
  70. hDll=LoadLibrary(_T("Ole32.dll"));
  71. if(!hDll)
  72. return S_FALSE;
  73. }
  74. //取得DoDragDrop函数地址
  75. pfnDoDragDrop=(PFNDoDragDrop)GetProcAddress(hDll,_T("DoDragDrop"));
  76. memcpy(enter,(LPVOID)pfnDoDragDrop,5);
  77. //动态构造一个长跳转指令
  78. jmp[0]=0xe9;
  79. *(int*)&jmp[1]=(int)&MyDoDragDrop-((int)pfnDoDragDrop+5);
  80. //将长跳转指令写入DoDragDrop前5个字节,Word调用DoDragDrop时会跳到我们的函数中
  81. DWORD dwOld=0;
  82. if(!VirtualProtect(pfnDoDragDrop,5,PAGE_EXECUTE_READWRITE,&dwOld))
  83. return S_FALSE;
  84. memcpy(pfnDoDragDrop,jmp,5);
  85. //刷新指令cache中相关地址指令的内容
  86. hProcess=GetCurrentProcess();
  87. FlushInstructionCache(hProcess,(LPVOID)pfnDoDragDrop,5);
  88. return S_OK;
  89. }
  90. STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
  91. {
  92. return S_OK;
  93. }
  94. STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom)
  95. {
  96. return S_OK;
  97. }
  98. STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom)
  99. {
  100. return S_OK;
  101. }
  102. STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom)
  103. {
  104. return S_OK;
  105. }
  106. };
  107. #endif //__ADDIN_H_