Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
7.7 KiB

  1. //
  2. // Copyright (C) 1997 Microsoft Corporation. All Rights Reserved.
  3. //
  4. // MODULE: w3ext.cpp
  5. //
  6. // Purpose: main file for the w3 shell extension
  7. #include "priv.h"
  8. #include <pudebug.h>
  9. // guids and com stuff
  10. #include <shlguid.h>
  11. //#include <shlwapi.h>
  12. #include "wrapmb.h"
  13. #include "sink.h"
  14. #include "eddir.h"
  15. #include "shellext.h"
  16. #include "tchar.h"
  17. //
  18. // Global variables
  19. //
  20. UINT g_cRefThisDll = 0; // Reference count of this DLL.
  21. HINSTANCE g_hmodThisDll = NULL; // Handle to this DLL itself.
  22. //---------------------------------------------------------------------------
  23. extern "C" int APIENTRY
  24. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  25. {
  26. if (dwReason == DLL_PROCESS_ATTACH)
  27. {
  28. ODS(TEXT("In DLLMain, DLL_PROCESS_ATTACH\r\n"));
  29. g_hmodThisDll = hInstance;
  30. }
  31. else if (dwReason == DLL_PROCESS_DETACH)
  32. {
  33. ODS(TEXT("In DLLMain, DLL_PROCESS_DETACH\r\n"));
  34. }
  35. return 1; // ok
  36. }
  37. //---------------------------------------------------------------------------
  38. STDAPI DllCanUnloadNow(void)
  39. {
  40. ODS(TEXT("In DLLCanUnloadNow\r\n"));
  41. return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
  42. }
  43. //---------------------------------------------------------------------------
  44. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
  45. {
  46. ODS(TEXT("In DllGetClassObject\r\n"));
  47. *ppvOut = NULL;
  48. if (IsEqualIID(rclsid, CLSID_ShellExtension))
  49. {
  50. CShellExtClassFactory *pcf = new CShellExtClassFactory;
  51. return pcf->QueryInterface(riid, ppvOut);
  52. }
  53. return CLASS_E_CLASSNOTAVAILABLE;
  54. }
  55. //
  56. // Registry Values Definitions
  57. //
  58. typedef struct tagVALUE_PAIR
  59. {
  60. HKEY hKeyBase;
  61. BOOL fOwnKey;
  62. BOOL fOwnValue;
  63. LPCTSTR lpstrKey;
  64. LPCTSTR lpstrValueName;
  65. LPCTSTR lpstrValue; // Blank is replaced with module path
  66. } VALUE_PAIR;
  67. //
  68. // Registry Entries
  69. //
  70. // NOTE: The table must be constructed so that sub keys are listed
  71. // after their parents, because we delete keys in the same
  72. // order in which they are declared here.
  73. //
  74. VALUE_PAIR g_aValues[] =
  75. {
  76. //
  77. // Base Key DelKey DelVal Key Name Value Name Value
  78. // ============================================================================================================================================================
  79. { HKEY_CLASSES_ROOT, TRUE, FALSE, _T("CLSID\\") STR_GUID _T("\\InProcServer32"), _T(""), _T("") },
  80. { HKEY_CLASSES_ROOT, FALSE, FALSE, _T("CLSID\\") STR_GUID _T("\\InProcServer32"), _T("ThreadingModel"), STR_THREAD_MODEL },
  81. { HKEY_CLASSES_ROOT, TRUE, FALSE, _T("CLSID\\") STR_GUID, _T(""), STR_NAME },
  82. { HKEY_CLASSES_ROOT, TRUE, FALSE, _T("Folder\\shellex\\PropertySheetHandlers\\PWS Sharing"), _T(""), STR_GUID },
  83. { HKEY_CLASSES_ROOT, FALSE, TRUE, _T("Folder\\shellex\\PropertySheetHandlers"), _T(""), _T("IISSEPage") },
  84. { HKEY_LOCAL_MACHINE, FALSE, TRUE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"),STR_GUID, STR_NAME },
  85. };
  86. #define NUM_ENTRIES (sizeof(g_aValues) / sizeof(g_aValues[0]))
  87. //--------------------------------------------------------------------
  88. // Auto-(un) registration Entry Point
  89. //
  90. STDAPI
  91. DllUnregisterServer(void)
  92. {
  93. HKEY hKey;
  94. DWORD dw;
  95. DWORD err = ERROR_SUCCESS;
  96. do
  97. {
  98. //
  99. // Loop through the entries
  100. //
  101. for (int i = 0; i < NUM_ENTRIES; ++i)
  102. {
  103. //
  104. // Do we own this key? If so delete the whole thing, including
  105. // its values.
  106. //
  107. if (g_aValues[i].fOwnKey)
  108. {
  109. err = RegDeleteKey(g_aValues[i].hKeyBase, g_aValues[i].lpstrKey);
  110. }
  111. //
  112. // Otherwise, do we own the value? Only delete it then
  113. //
  114. else if (g_aValues[i].fOwnValue)
  115. {
  116. // create / open the key
  117. err = RegCreateKeyEx(
  118. g_aValues[i].hKeyBase, // handle of an open key
  119. g_aValues[i].lpstrKey, // address of subkey name
  120. 0, // reserved
  121. _T(""), // address of class string
  122. REG_OPTION_NON_VOLATILE,// special options flag
  123. KEY_ALL_ACCESS, // desired security access
  124. NULL, // address of key security structure
  125. &hKey, // address of buffer for opened handle
  126. &dw // address of disposition value buffer
  127. );
  128. // if we opened the key, delete the value
  129. if ( err == ERROR_SUCCESS )
  130. {
  131. RegDeleteValue( hKey, g_aValues[i].lpstrValueName );
  132. // close the registry key
  133. RegCloseKey( hKey );
  134. }
  135. }
  136. //
  137. // If the key or value is already gone, that's ok
  138. //
  139. if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
  140. {
  141. err = ERROR_SUCCESS;
  142. }
  143. else
  144. {
  145. break;
  146. }
  147. }
  148. }
  149. while(FALSE);
  150. return HRESULT_FROM_WIN32(err);
  151. }
  152. //--------------------------------------------------------------------
  153. // Auto-registration Entry Point
  154. //
  155. STDAPI
  156. DllRegisterServer(void)
  157. {
  158. DWORD err = ERROR_SUCCESS;
  159. TCHAR strModulePath[MAX_PATH];
  160. HKEY hKey;
  161. DWORD dw;
  162. do
  163. {
  164. //
  165. // Build current module path
  166. //
  167. HINSTANCE hCurrent = g_hmodThisDll;
  168. if (hCurrent == NULL)
  169. {
  170. err = ERROR_INVALID_HANDLE;
  171. break;
  172. }
  173. if (GetModuleFileName(hCurrent, strModulePath, MAX_PATH) == 0)
  174. {
  175. err = GetLastError();
  176. break;
  177. }
  178. //
  179. // Loop through the entries.
  180. // If the reg value is blank, use the module path
  181. //
  182. for (int i = 0; i < NUM_ENTRIES; ++i)
  183. {
  184. // create / open the key
  185. if (ERROR_SUCCESS != (err = RegCreateKeyEx(
  186. g_aValues[i].hKeyBase,
  187. g_aValues[i].lpstrKey, // address of subkey name
  188. 0, // reserved
  189. _T(""), // address of class string
  190. REG_OPTION_NON_VOLATILE, // special options flag
  191. KEY_ALL_ACCESS, // desired security access
  192. NULL, // address of key security structure
  193. &hKey, // address of buffer for opened handle
  194. &dw // address of disposition value buffer
  195. )))
  196. {
  197. break;
  198. }
  199. LPCTSTR pValue;
  200. int len;
  201. if (g_aValues[i].lpstrValue[0] != 0)
  202. pValue = g_aValues[i].lpstrValue;
  203. else
  204. pValue = strModulePath;
  205. len = _tcslen(pValue) * sizeof(TCHAR);
  206. // set the value
  207. if (ERROR_SUCCESS != (err = RegSetValueEx(hKey,
  208. g_aValues[i].lpstrValueName,
  209. 0,
  210. REG_SZ,
  211. (const unsigned char *)pValue,
  212. len)))
  213. {
  214. break;
  215. }
  216. // close the registry key
  217. RegCloseKey( hKey );
  218. }
  219. }
  220. while(FALSE);
  221. return HRESULT_FROM_WIN32(err);
  222. }