main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <tchar.h>
  2. #import "vgcoreauto.tlb"
  3. // DllMain是动态链接库的入口函数
  4. // hModule: 动态链接库的模块句柄
  5. // ul_reason_for_call: 调用原因,指示动态链接库被加载或卸载的原因
  6. // lpReserved: 保留参数,通常设为NULL
  7. BOOL APIENTRY DllMain( HMODULE hModule,
  8. DWORD ul_reason_for_call,
  9. LPVOID lpReserved
  10. )
  11. {
  12. // 根据调用原因进行不同的操作
  13. switch (ul_reason_for_call)
  14. {
  15. case DLL_PROCESS_ATTACH: // 动态链接库被加载到进程地址空间时调用
  16. case DLL_THREAD_ATTACH: // 创建线程时调用
  17. case DLL_THREAD_DETACH: // 线程退出时调用
  18. case DLL_PROCESS_DETACH: // 动态链接库被卸载时调用
  19. break;
  20. }
  21. // 成功处理所有调用原因,返回TRUE
  22. return TRUE;
  23. }
  24. // CVGAppPlugin类实现了VGCore::IVGAppPlugin接口,用于插件开发
  25. class CVGAppPlugin : public VGCore::IVGAppPlugin
  26. {
  27. private:
  28. VGCore::IVGApplication *m_pApp; // 应用程序接口指针
  29. ULONG m_ulRefCount; // 对象引用计数
  30. long m_lCookie; // 用于标识插件的cookie
  31. bool m_bEnabled; // 插件是否启用的标志
  32. public:
  33. // 默认构造函数
  34. CVGAppPlugin();
  35. // IUnknown 接口实现
  36. // QueryInterface用于获取接口指针
  37. STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject);
  38. // AddRef增加对象的引用计数
  39. STDMETHOD_(ULONG, AddRef)(void)
  40. {
  41. return ++m_ulRefCount;
  42. }
  43. // Release减少对象的引用计数,当计数为0时释放对象
  44. STDMETHOD_(ULONG, Release)(void)
  45. {
  46. ULONG ulCount = --m_ulRefCount;
  47. if (ulCount == 0)
  48. {
  49. delete this;
  50. }
  51. return ulCount;
  52. }
  53. // IDispatch 接口实现
  54. // GetTypeInfoCount返回类型信息的数量
  55. STDMETHOD(GetTypeInfoCount)(UINT *pctinfo)
  56. {
  57. return E_NOTIMPL;
  58. }
  59. // GetTypeInfo获取类型信息
  60. STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
  61. {
  62. return E_NOTIMPL;
  63. }
  64. // GetIDsOfNames获取指定名称的成员或变量的ID
  65. STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
  66. {
  67. return E_NOTIMPL;
  68. }
  69. // Invoke调用对象的成员函数
  70. STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
  71. // IVGAppPlugin接口实现
  72. STDMETHOD(raw_OnLoad)(VGCore::IVGApplication *Application);
  73. STDMETHOD(raw_StartSession)();
  74. STDMETHOD(raw_StopSession)();
  75. STDMETHOD(raw_OnUnload)();
  76. };
  77. // 默认构造函数
  78. CVGAppPlugin::CVGAppPlugin() :
  79. m_pApp(NULL),
  80. m_lCookie(0),
  81. m_ulRefCount(1),
  82. m_bEnabled(false)
  83. {
  84. }
  85. // QueryInterface实现,用于获取接口指针
  86. STDMETHODIMP CVGAppPlugin::QueryInterface(REFIID riid, void **ppvObject)
  87. {
  88. HRESULT hr = S_OK;
  89. m_ulRefCount++;
  90. if (riid == IID_IUnknown)
  91. {
  92. *ppvObject = (IUnknown *)this;
  93. }
  94. else if (riid == IID_IDispatch)
  95. {
  96. *ppvObject = (IDispatch *)this;
  97. }
  98. else if (riid == __uuidof(VGCore::IVGAppPlugin))
  99. {
  100. *ppvObject = (VGCore::IVGAppPlugin *)this;
  101. }
  102. else
  103. {
  104. m_ulRefCount--;
  105. hr = E_NOINTERFACE;
  106. }
  107. return hr;
  108. }
  109. // Invoke方法的实现,目前仅返回S_OK
  110. STDMETHODIMP CVGAppPlugin::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
  111. {
  112. return S_OK;
  113. }
  114. // raw_OnLoad方法,在插件加载时被调用
  115. STDMETHODIMP CVGAppPlugin::raw_OnLoad(VGCore::IVGApplication *Application)
  116. {
  117. m_pApp = Application;
  118. if (m_pApp)
  119. {
  120. m_pApp->AddRef();
  121. }
  122. return S_OK;
  123. }
  124. // raw_StartSession方法,启动会话时被调用
  125. STDMETHODIMP CVGAppPlugin::raw_StartSession()
  126. {
  127. MessageBox(NULL, _T("插件加载到内存,本CPG使用VC2022 X64编译"), _T("蘭雅CPG64"), MB_ICONSTOP);
  128. return S_OK;
  129. }
  130. // raw_StopSession方法,结束会话时被调用
  131. STDMETHODIMP CVGAppPlugin::raw_StopSession()
  132. {
  133. MessageBox(NULL, _T("插件结束"), _T("蘭雅CPG64"), MB_ICONSTOP);
  134. return S_OK;
  135. }
  136. // raw_OnUnload方法,在插件卸载时被调用
  137. STDMETHODIMP CVGAppPlugin::raw_OnUnload()
  138. {
  139. if (m_pApp)
  140. {
  141. m_pApp->Release();
  142. m_pApp = NULL;
  143. }
  144. return S_OK;
  145. }
  146. // AttachPlugin函数用于将插件附加到应用程序中
  147. // ppIPlugin: 插件接口指针的地址
  148. // 返回值: 附加操作的结果,这里固定返回0x100
  149. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin **ppIPlugin)
  150. {
  151. *ppIPlugin = new CVGAppPlugin;
  152. return 0x100;
  153. }