ShellExt.cpp
上传用户:lswyart
上传日期:2008-06-12
资源大小:3441k
文件大小:7k
源码类别:

杀毒

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. // Name:        ShellExt.cpp
  3. // Product:     ClamWin Antivirus
  4. //
  5. // Author:      alch [alch at users dot sourceforge dot net]
  6. //
  7. // Created:     2004/19/03
  8. // Copyright:   Copyright alch (c) 2004
  9. // Licence:     
  10. //   This program is free software; you can redistribute it and/or modify
  11. //   it under the terms of the GNU General Public License as published by
  12. //   the Free Software Foundation; either version 2 of the License, or
  13. //   (at your option) any later version.
  14. // 
  15. //   This program is distributed in the hope that it will be useful,
  16. //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. //   GNU General Public License for more details.
  19. // 
  20. //   You should have received a copy of the GNU General Public License
  21. //   along with this program; if not, write to the Free Software
  22. //   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. //-----------------------------------------------------------------------------
  24.  
  25. //
  26. // Initialize GUIDs (should be done only and at-least once per DLL/EXE)
  27. //
  28. #include <windows.h>
  29. #include <shlobj.h>
  30. #define INITGUID
  31. #include <initguid.h>
  32. #include <shlguid.h>
  33. #include "ShellExt.h"
  34. //
  35. // Global variables
  36. //
  37. UINT      g_cRefThisDll = 0;    // Reference count of this DLL.
  38. HINSTANCE g_hmodThisDll = NULL; // Handle to this DLL itself.
  39. extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  40. {
  41. if (dwReason == DLL_PROCESS_ATTACH)
  42. g_hmodThisDll = hInstance;
  43. return 1;   // ok
  44. }
  45. //---------------------------------------------------------------------------
  46. // DllCanUnloadNow
  47. //---------------------------------------------------------------------------
  48. STDAPI  DllCanUnloadNow(void)
  49. {
  50. return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
  51. }
  52. //---------------------------------------------------------------------------
  53. // DllGetClassObject
  54. //---------------------------------------------------------------------------
  55. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
  56. {
  57. *ppvOut = NULL;
  58. if (IsEqualIID(rclsid, CLSID_ShellExtension))
  59. {
  60. CShellExtClassFactory *pcf = new CShellExtClassFactory;
  61. return pcf->QueryInterface(riid, ppvOut);
  62. }
  63. return CLASS_E_CLASSNOTAVAILABLE;
  64. }
  65. //---------------------------------------------------------------------------
  66. // CShellExtClassFactory::CShellExtClassFactory
  67. //---------------------------------------------------------------------------
  68. CShellExtClassFactory::CShellExtClassFactory()
  69. {
  70. m_cRef = 0L;
  71. g_cRefThisDll++;    
  72. }
  73. //---------------------------------------------------------------------------
  74. // CShellExtClassFactory::~CShellExtClassFactory
  75. //---------------------------------------------------------------------------
  76. CShellExtClassFactory::~CShellExtClassFactory()                         
  77. {
  78. g_cRefThisDll--;
  79. }
  80. //---------------------------------------------------------------------------
  81. // CShellExtClassFactory::QueryInterface
  82. //---------------------------------------------------------------------------
  83. STDMETHODIMP CShellExtClassFactory::QueryInterface(REFIID riid,
  84.                                                    LPVOID FAR *ppv)
  85. {
  86. *ppv = NULL;
  87. // Any interface on this object is the object pointer
  88. if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  89. {
  90. *ppv = (LPCLASSFACTORY)this;
  91. AddRef();
  92. return NOERROR;
  93. }
  94. return E_NOINTERFACE;
  95. }       
  96. //---------------------------------------------------------------------------
  97. // CShellExtClassFactory::AddRef
  98. //---------------------------------------------------------------------------
  99. STDMETHODIMP_(ULONG) CShellExtClassFactory::AddRef()
  100. {
  101. return ++m_cRef;
  102. }
  103. //---------------------------------------------------------------------------
  104. // CShellExtClassFactory::Release
  105. //---------------------------------------------------------------------------
  106. STDMETHODIMP_(ULONG) CShellExtClassFactory::Release()
  107. {
  108. if (--m_cRef)
  109. return m_cRef;
  110. delete this;
  111. return 0L;
  112. }
  113. //---------------------------------------------------------------------------
  114. // CShellExtClassFactory::CreateInstance
  115. //---------------------------------------------------------------------------
  116. STDMETHODIMP CShellExtClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
  117. REFIID riid,
  118. LPVOID *ppvObj)
  119. {
  120. *ppvObj = NULL;
  121. // Shell extensions typically don't support aggregation (inheritance)
  122. if (pUnkOuter)
  123. return CLASS_E_NOAGGREGATION;
  124. // Create the main shell extension object.  The shell will then call
  125. // QueryInterface with IID_IShellExtInit--this is how shell extensions are
  126. // initialized.
  127. LPCSHELLEXT pShellExt = new CShellExt();  //Create the CShellExt object
  128. if (NULL == pShellExt)
  129. return E_OUTOFMEMORY;
  130. return pShellExt->QueryInterface(riid, ppvObj);
  131. }
  132. //---------------------------------------------------------------------------
  133. // CShellExtClassFactory::LockServer
  134. //---------------------------------------------------------------------------
  135. STDMETHODIMP CShellExtClassFactory::LockServer(BOOL fLock)
  136. {
  137. return NOERROR;
  138. }
  139. // *********************** CShellExt *************************
  140. //---------------------------------------------------------------------------
  141. // CShellExt::CShellExt
  142. //---------------------------------------------------------------------------
  143. CShellExt::CShellExt()
  144. {
  145. m_cRef = 0L;
  146. m_pDataObj = NULL;
  147. m_szPath = NULL;
  148. g_cRefThisDll++;
  149. }
  150. //---------------------------------------------------------------------------
  151. // CShellExt::~CShellExt
  152. //---------------------------------------------------------------------------
  153. CShellExt::~CShellExt()
  154. {
  155.     if (m_szPath)
  156.         delete [] m_szPath;
  157. if (m_pDataObj)
  158. m_pDataObj->Release();
  159. g_cRefThisDll--;
  160. }
  161. //---------------------------------------------------------------------------
  162. // CShellExt::QueryInterface
  163. //---------------------------------------------------------------------------
  164. STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  165. {
  166. *ppv = NULL;
  167. if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
  168. *ppv = (LPSHELLEXTINIT)this;
  169. else if (IsEqualIID(riid, IID_IContextMenu))
  170. *ppv = (LPCONTEXTMENU)this;
  171. if (*ppv)
  172. {
  173. AddRef();
  174. return NOERROR;
  175. }
  176. return E_NOINTERFACE;
  177. }
  178. //---------------------------------------------------------------------------
  179. // CShellExt::AddRef
  180. //---------------------------------------------------------------------------
  181. STDMETHODIMP_(ULONG) CShellExt::AddRef()
  182. {
  183. return ++m_cRef;
  184. }
  185. //---------------------------------------------------------------------------
  186. // CShellExt::Release
  187. //---------------------------------------------------------------------------
  188. STDMETHODIMP_(ULONG) CShellExt::Release()
  189. {
  190. if (--m_cRef)
  191. return m_cRef;
  192. delete this;
  193. return 0L;
  194. }