dllregshex.cpp
上传用户:sencon168
上传日期:2007-01-04
资源大小:22k
文件大小:9k
源码类别:

Shell编程

开发平台:

Visual C++

  1. #include "dllreg_xhdr.h"
  2. #include <olectl.h>
  3. #include "dllregshex.h"
  4. #include "dllreg_util.h"
  5. #include "resource.h"
  6. #ifndef __CLSID_NTSrv_Defined__
  7. #define __CLSID_NTSrv_Defined__
  8. DEFINE_GUID(CLSID_DLLRegisterer, 0x25d84cb0, 0x7345, 0x11d3, 0xa4, 0xa1, 0x0, 0x80, 0xc8, 0xec, 0xfe, 0xd4);
  9. #endif
  10. UINT      g_cRefThisDll = 0;    
  11. HINSTANCE g_hmodThisDll = NULL;
  12. HBITMAP hBmp_Install = 0;
  13. HBITMAP hBmp_Uninstall  = 0;
  14. HBITMAP hBmp_About = 0;
  15. HMENU hSubMenu = 0;
  16. typedef struct
  17. {
  18.    HKEY  hRootKey;
  19.    LPTSTR lpszSubKey;
  20.    LPTSTR lpszValueName;
  21.    LPTSTR lpszData;
  22. } REGSTRUCT, *LPREGSTRUCT;
  23. STDAPI DllRegisterServer(void);
  24. STDAPI DllUnregisterServer(void);
  25. VOID   _LoadResources();
  26. VOID   _UnloadResources();
  27. STDAPI 
  28. DllRegisterServer(void)
  29. {
  30. HINSTANCE hInst = g_hmodThisDll;
  31. int      i;
  32. HKEY     hKey;
  33. LRESULT  lResult;
  34. DWORD    dwDisp;
  35. TCHAR    szSubKey[MAX_PATH];
  36. TCHAR    szCLSID[MAX_PATH];
  37. TCHAR    szModule[MAX_PATH];
  38. LPWSTR   pwszShellExt;
  39. StringFromIID(CLSID_DLLRegisterer, &pwszShellExt);
  40. if (pwszShellExt) 
  41. {
  42. WideCharToLocal(szCLSID, pwszShellExt, ARRAYSIZE(szCLSID));
  43. LPMALLOC pMalloc;
  44. CoGetMalloc(1, &pMalloc);
  45. if(pMalloc)
  46. {
  47. pMalloc->Free(pwszShellExt);
  48. pMalloc->Release();
  49. }
  50. }    
  51. GetModuleFileName(hInst, szModule, ARRAYSIZE(szModule));
  52. REGSTRUCT ShExClsidEntries[] = 
  53. {
  54. HKEY_CLASSES_ROOT, TEXT("CLSID\%s"), NULL,                   TEXT(DLLREGUNREGNAME),
  55. HKEY_CLASSES_ROOT, TEXT("CLSID\%s\InProcServer32"), NULL,                   TEXT("%s"),
  56. HKEY_CLASSES_ROOT, TEXT("CLSID\%s\InProcServer32"), TEXT("ThreadingModel"), TEXT("Apartment"),
  57. NULL,                NULL, NULL,                   NULL
  58. };
  59. for(i = 0; ShExClsidEntries[i].hRootKey; i++) 
  60.     {
  61. wsprintf(szSubKey, ShExClsidEntries[i].lpszSubKey, szCLSID);
  62. lResult = RegCreateKeyEx(ShExClsidEntries[i].hRootKey, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
  63. KEY_WRITE, NULL, &hKey, &dwDisp);
  64. if(NOERROR == lResult) 
  65. {
  66. TCHAR szData[MAX_PATH];
  67. wsprintf(szData, ShExClsidEntries[i].lpszData, szModule);
  68. lResult = RegSetValueEx(hKey, ShExClsidEntries[i].lpszValueName, 0, REG_SZ, 
  69. (LPBYTE)szData, lstrlen(szData) + 1);
  70. RegCloseKey(hKey);
  71. else
  72. return SELFREG_E_CLASS;
  73.    }                                
  74. REGSTRUCT OtherShExEntries[] = 
  75. {
  76. HKEY_LOCAL_MACHINE, TEXT("software\classes\clsid\"DLLREGUNREGNAME)   , NULL, TEXT("%s"),
  77. HKEY_CLASSES_ROOT, TEXT("dllfile\shellex\ContextMenuHandlers\"DLLREGUNREGNAME), NULL, TEXT("%s"),
  78. NULL, NULL, NULL,                   NULL
  79. };
  80. for (i = 0; OtherShExEntries[i].hRootKey; i++) 
  81.     {
  82. wsprintf(szSubKey, OtherShExEntries[i].lpszSubKey, szCLSID);
  83. lResult = RegCreateKeyEx(OtherShExEntries[i].hRootKey, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
  84. KEY_WRITE, NULL, &hKey, &dwDisp);
  85. if(NOERROR == lResult) 
  86. {
  87. TCHAR szData[MAX_PATH];
  88. wsprintf(szData, OtherShExEntries[i].lpszData, szCLSID);
  89. lResult = RegSetValueEx(hKey, OtherShExEntries[i].lpszValueName, 0, REG_SZ,
  90. (LPBYTE)szData, lstrlen(szData) + 1);
  91. RegCloseKey(hKey);
  92. else
  93. return SELFREG_E_CLASS;
  94. }
  95. OSVERSIONINFO  osvi;
  96. osvi.dwOSVersionInfoSize = sizeof(osvi);
  97. GetVersionEx(&osvi);
  98. if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId)  
  99. {
  100. lstrcpy( szSubKey, TEXT("Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved"));
  101. lResult = RegCreateKeyEx(  HKEY_LOCAL_MACHINE, szSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
  102. NULL, &hKey, &dwDisp);
  103. if(NOERROR == lResult) 
  104. {
  105. TCHAR szData[MAX_PATH];
  106. lstrcpy(szData, DLLREGUNREGNAME);
  107. lResult = RegSetValueEx(hKey, szCLSID, 0, REG_SZ, (LPBYTE)szData, lstrlen(szData) + 1);
  108. RegCloseKey(hKey);
  109. else
  110. return SELFREG_E_CLASS;
  111. }
  112. return S_OK;
  113. }
  114. STDAPI 
  115. DllUnregisterServer(void)
  116. {
  117. int      i;
  118. LRESULT  lResult;
  119. TCHAR    szSubKey[MAX_PATH];
  120. TCHAR    szCLSID[MAX_PATH];
  121. LPWSTR   pwszShellExt;
  122. StringFromIID(CLSID_DLLRegisterer, &pwszShellExt);
  123. if (pwszShellExt) 
  124. {
  125. WideCharToLocal(szCLSID, pwszShellExt, ARRAYSIZE(szCLSID));
  126. LPMALLOC pMalloc;   
  127. CoGetMalloc(1, &pMalloc);
  128. if(pMalloc)
  129. {
  130. pMalloc->Free(pwszShellExt);
  131. pMalloc->Release();
  132. }
  133. }    
  134.     
  135. REGSTRUCT ShExClsidEntries[] = 
  136. {
  137. HKEY_CLASSES_ROOT, TEXT("CLSID\%s\InProcServer32"), NULL, NULL,
  138. HKEY_CLASSES_ROOT, TEXT("CLSID\%s"), NULL, NULL,
  139. NULL, NULL, NULL, NULL
  140. };
  141. for(i = 0; ShExClsidEntries[i].hRootKey; i++) 
  142. {
  143. wsprintf(szSubKey, ShExClsidEntries[i].lpszSubKey, szCLSID);
  144. lResult = RegDeleteKey(ShExClsidEntries[i].hRootKey, szSubKey);
  145.  
  146. if(lResult != NOERROR) 
  147. return SELFREG_E_CLASS;
  148. }
  149. REGSTRUCT OtherShExEntries[] = 
  150. {
  151. HKEY_CLASSES_ROOT, TEXT("dllfile\shellex\ContextMenuHandlers\"DLLREGUNREGNAME), NULL, NULL,
  152. NULL, NULL, NULL,                   NULL
  153. };
  154. for(i = 0; OtherShExEntries[i].hRootKey; i++) 
  155. {
  156. wsprintf(szSubKey, OtherShExEntries[i].lpszSubKey, szCLSID);
  157. lResult = RegDeleteKey(OtherShExEntries[i].hRootKey, szSubKey);
  158. if(lResult != NOERROR) 
  159. return SELFREG_E_CLASS;
  160. }
  161.     
  162. OSVERSIONINFO  osvi;
  163. osvi.dwOSVersionInfoSize = sizeof(osvi);
  164. GetVersionEx(&osvi);
  165. if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId)  
  166. {
  167. lstrcpy( szSubKey, TEXT("Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved\"DLLREGUNREGNAME));
  168. lResult = RegDeleteKey(HKEY_LOCAL_MACHINE, szSubKey);
  169. }
  170. return S_OK;
  171. }
  172. extern "C" 
  173. int APIENTRY
  174. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  175. {
  176. _UNUSED_PARAMETER(lpReserved);
  177.     if (dwReason == DLL_PROCESS_ATTACH)
  178. {
  179.         g_hmodThisDll = hInstance;
  180. _LoadResources();
  181. }
  182. else
  183. _UnloadResources();
  184.     return 1;
  185. }
  186. VOID
  187. _LoadResources(VOID)
  188. {
  189. hBmp_Install = LoadBitmap(g_hmodThisDll, MAKEINTRESOURCE(IDB_INSTALLA));
  190. hBmp_Uninstall  = LoadBitmap(g_hmodThisDll, MAKEINTRESOURCE(IDB_UNINSTALLA));
  191. hBmp_About = LoadBitmap(g_hmodThisDll, MAKEINTRESOURCE(IDB_ABOUTA));
  192. }
  193. VOID
  194. _UnloadResources(VOID)
  195. {
  196. DeleteObject(hBmp_Install);
  197. DeleteObject(hBmp_Uninstall);
  198. DeleteObject(hBmp_About);
  199. DestroyMenu(hSubMenu);
  200. }
  201. STDAPI 
  202. DllCanUnloadNow(void)
  203. {
  204.     return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
  205. }
  206. STDAPI 
  207. DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
  208. {
  209.     *ppvOut = NULL;
  210.     if (IsEqualIID(rclsid, CLSID_DLLRegisterer))
  211.     {
  212.         CDllRegSxClassFactory *pcf = new CDllRegSxClassFactory;
  213.         return pcf->QueryInterface(riid, ppvOut);
  214.     }
  215.     return CLASS_E_CLASSNOTAVAILABLE;
  216. }
  217. CDllRegSxClassFactory::CDllRegSxClassFactory()
  218. {
  219.     m_cRef = 0L;
  220.     g_cRefThisDll++;
  221. }
  222. CDllRegSxClassFactory::~CDllRegSxClassFactory()
  223. {
  224.     g_cRefThisDll--;
  225. }
  226. STDMETHODIMP 
  227. CDllRegSxClassFactory::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  228. {
  229.     *ppv = NULL;
  230.     if(IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  231.     {
  232.         *ppv = (LPCLASSFACTORY)this;
  233.         AddRef();
  234.         return NOERROR;
  235.     }
  236.     return E_NOINTERFACE;
  237. }
  238. STDMETHODIMP_(ULONG) 
  239. CDllRegSxClassFactory::AddRef()
  240. {
  241.     return ++m_cRef;
  242. }
  243. STDMETHODIMP_(ULONG) 
  244. CDllRegSxClassFactory::Release()
  245. {
  246.     if (--m_cRef)
  247.         return m_cRef;
  248.     delete this;
  249.     return 0L;
  250. }
  251. STDMETHODIMP 
  252. CDllRegSxClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObj)
  253. {
  254.     *ppvObj = NULL;
  255.     if (pUnkOuter)
  256.      return CLASS_E_NOAGGREGATION;
  257.     LPCSHELLEXT pShellExt = new CShellExt();
  258.     if (NULL == pShellExt)
  259.      return E_OUTOFMEMORY;
  260.     return pShellExt->QueryInterface(riid, ppvObj);
  261. }
  262. STDMETHODIMP 
  263. CDllRegSxClassFactory::LockServer(BOOL fLock)
  264. {
  265. _UNUSED_PARAMETER(fLock);
  266.     return NOERROR;
  267. }
  268. CShellExt::CShellExt()
  269. {
  270.     m_cRef = 0L;
  271.     m_pDataObj = NULL;
  272.     g_cRefThisDll++;
  273. }
  274. CShellExt::~CShellExt()
  275. {
  276.     if (m_pDataObj)
  277.         m_pDataObj->Release();
  278.     g_cRefThisDll--;
  279. }
  280. STDMETHODIMP 
  281. CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  282. {
  283.     *ppv = NULL;
  284.     if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
  285.      *ppv = (LPSHELLEXTINIT)this;
  286.     else if (IsEqualIID(riid, IID_IContextMenu))
  287.         *ppv = (LPCONTEXTMENU)this;
  288.     if(*ppv)
  289.     {
  290.         AddRef();
  291.         return NOERROR;
  292.     }
  293. return E_NOINTERFACE;
  294. }
  295. STDMETHODIMP_(ULONG) 
  296. CShellExt::AddRef()
  297. {
  298.     return ++m_cRef;
  299. }
  300. STDMETHODIMP_(ULONG) 
  301. CShellExt::Release()
  302. {
  303.     if(--m_cRef)
  304.         return m_cRef;
  305.     delete this;
  306.     return 0L;
  307. }
  308. STDMETHODIMP 
  309. CShellExt::Initialize(LPCITEMIDLIST pIDFolder, LPDATAOBJECT pDataObj, HKEY hRegKey)
  310. {
  311. _UNUSED_PARAMETER(hRegKey);
  312. _UNUSED_PARAMETER(pIDFolder);
  313. if (m_pDataObj)
  314.      m_pDataObj->Release();
  315.     if (pDataObj)
  316.     {
  317.      m_pDataObj = pDataObj;
  318.      pDataObj->AddRef();
  319.     }
  320.     return NOERROR;
  321. }