Leaked source code of windows server 2003
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.

354 lines
10 KiB

  1. //
  2. // Microsoft Corporation 1998
  3. //
  4. // RIGPSNAP.CPP - DLL entry and registration routines
  5. //
  6. #include "main.h"
  7. #include <initguid.h>
  8. #include <rigpsnap.h>
  9. #include <gpedit.h>
  10. // Globals
  11. LONG g_cRefThisDll = 0;
  12. HINSTANCE g_hInstance;
  13. /////////////////////////////////////////////////////////////////////////////
  14. // DLL Entry Point
  15. extern "C"
  16. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  17. {
  18. if (dwReason == DLL_PROCESS_ATTACH)
  19. {
  20. g_hInstance = hInstance;
  21. DisableThreadLibraryCalls(hInstance);
  22. InitNameSpace();
  23. #if DBG
  24. InitDebugSupport();
  25. #endif
  26. }
  27. return TRUE; // ok
  28. }
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Used to determine whether the DLL can be unloaded by OLE
  31. STDAPI DllCanUnloadNow(void)
  32. {
  33. return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
  34. }
  35. /////////////////////////////////////////////////////////////////////////////
  36. // Returns a class factory to create an object of the requested type
  37. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  38. {
  39. return (CreateComponentDataClassFactory (rclsid, riid, ppv));
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // DllRegisterServer - Adds entries to the system registry
  43. const TCHAR szSnapInLocation[] = TEXT("%SystemRoot%\\System32\\RIGPSNAP.dll");
  44. STDAPI DllRegisterServer(void)
  45. {
  46. TCHAR szSnapInKey[50];
  47. TCHAR szSubKey[200];
  48. TCHAR szSnapInName[100];
  49. TCHAR szGUID[50];
  50. DWORD dwDisp, dwIndex;
  51. LONG lResult;
  52. HKEY hKey;
  53. StringFromGUID2 (CLSID_GPTRemoteInstall, szSnapInKey, ARRAYSIZE( szSnapInKey ));
  54. //
  55. // Register SnapIn in HKEY_CLASSES_ROOT
  56. //
  57. if (!LoadString (
  58. g_hInstance,
  59. IDS_SNAPIN_NAME,
  60. szSnapInName,
  61. ARRAYSIZE( szSnapInName ))) {
  62. return SELFREG_E_CLASS;
  63. }
  64. if (_snwprintf(szSubKey,
  65. ARRAYSIZE(szSubKey),
  66. TEXT("CLSID\\%s"), szSnapInKey) < 0) {
  67. return SELFREG_E_CLASS;
  68. }
  69. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  70. lResult = RegCreateKeyEx (HKEY_CLASSES_ROOT, szSubKey, 0, NULL,
  71. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  72. &hKey, &dwDisp);
  73. if (lResult != ERROR_SUCCESS) {
  74. return SELFREG_E_CLASS;
  75. }
  76. lResult = RegSetValueEx (hKey, NULL, 0, REG_SZ, (LPBYTE)szSnapInName,
  77. (lstrlen(szSnapInName) + 1) * sizeof(TCHAR));
  78. RegCloseKey (hKey);
  79. if (lResult != ERROR_SUCCESS) {
  80. return SELFREG_E_CLASS;
  81. }
  82. if (_snwprintf(szSubKey,
  83. ARRAYSIZE(szSubKey),
  84. TEXT("CLSID\\%s\\InProcServer32"), szSnapInKey) < 0) {
  85. return SELFREG_E_CLASS;
  86. }
  87. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  88. lResult = RegCreateKeyEx (HKEY_CLASSES_ROOT, szSubKey, 0, NULL,
  89. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  90. &hKey, &dwDisp);
  91. if (lResult != ERROR_SUCCESS) {
  92. return SELFREG_E_CLASS;
  93. }
  94. lResult = RegSetValueEx (hKey, NULL, 0, REG_EXPAND_SZ, (LPBYTE)szSnapInLocation,
  95. (lstrlen(szSnapInLocation) + 1) * sizeof(TCHAR));
  96. RegCloseKey (hKey);
  97. if (lResult != ERROR_SUCCESS) {
  98. return SELFREG_E_CLASS;
  99. }
  100. //
  101. // Register SnapIn with MMC
  102. //
  103. if (_snwprintf(szSubKey,
  104. ARRAYSIZE(szSubKey),
  105. TEXT("Software\\Microsoft\\MMC\\SnapIns\\%s"), szSnapInKey) < 0) {
  106. return SELFREG_E_CLASS;
  107. }
  108. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  109. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  110. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  111. &hKey, &dwDisp);
  112. if (lResult != ERROR_SUCCESS) {
  113. return SELFREG_E_CLASS;
  114. }
  115. TCHAR szModuleFileName[MAX_PATH];
  116. ZeroMemory(szModuleFileName, sizeof(szModuleFileName));
  117. DWORD dwRet = GetModuleFileName(
  118. g_hInstance,
  119. szModuleFileName,
  120. ARRAYSIZE(szModuleFileName));
  121. if (0 != dwRet) {
  122. TCHAR strNameIndirect[MAX_PATH];
  123. szModuleFileName[ARRAYSIZE(szModuleFileName)-1] = TEXT('\0');
  124. if (_snwprintf(
  125. strNameIndirect,
  126. ARRAYSIZE(strNameIndirect),
  127. TEXT("@%s,-%u"),
  128. szModuleFileName,
  129. IDS_SNAPIN_NAME) < 0) {
  130. lResult = ERROR_INSUFFICIENT_BUFFER;
  131. } else {
  132. strNameIndirect[ARRAYSIZE(strNameIndirect)-1] = TEXT('\0');
  133. lResult = RegSetValueEx(
  134. hKey,
  135. TEXT("NameStringIndirect"),
  136. 0,
  137. REG_SZ,
  138. (LPBYTE)strNameIndirect,
  139. (lstrlen(strNameIndirect) + 1) * sizeof(TCHAR));
  140. }
  141. } else {
  142. lResult = (LONG)dwRet;
  143. }
  144. if (lResult != ERROR_SUCCESS) {
  145. RegCloseKey (hKey);
  146. return SELFREG_E_CLASS;
  147. }
  148. lResult = RegSetValueEx (hKey, TEXT("NameString"), 0, REG_SZ, (LPBYTE)szSnapInName,
  149. (lstrlen(szSnapInName) + 1) * sizeof(TCHAR));
  150. RegCloseKey (hKey);
  151. if (lResult != ERROR_SUCCESS) {
  152. return SELFREG_E_CLASS;
  153. }
  154. for (dwIndex = 0; dwIndex < NUM_NAMESPACE_ITEMS; dwIndex++)
  155. {
  156. StringFromGUID2 (*g_NameSpace[dwIndex].pNodeID, szGUID, ARRAYSIZE( szGUID ));
  157. if (_snwprintf(
  158. szSubKey,
  159. ARRAYSIZE(szSubKey),
  160. TEXT("Software\\Microsoft\\MMC\\SnapIns\\%s\\NodeTypes\\%s"),
  161. szSnapInKey,
  162. szGUID) < 0) {
  163. return SELFREG_E_CLASS;
  164. }
  165. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  166. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  167. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  168. &hKey, &dwDisp);
  169. if (lResult != ERROR_SUCCESS) {
  170. return SELFREG_E_CLASS;
  171. }
  172. RegCloseKey (hKey);
  173. }
  174. //
  175. // Register in the NodeTypes key
  176. //
  177. for (dwIndex = 0; dwIndex < NUM_NAMESPACE_ITEMS; dwIndex++)
  178. {
  179. StringFromGUID2 (*g_NameSpace[dwIndex].pNodeID, szGUID, ARRAYSIZE( szGUID ));
  180. if (_snwprintf(
  181. szSubKey,
  182. ARRAYSIZE(szSubKey),
  183. TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"),
  184. szGUID) < 0) {
  185. return SELFREG_E_CLASS;
  186. }
  187. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  188. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  189. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  190. &hKey, &dwDisp);
  191. if (lResult != ERROR_SUCCESS) {
  192. return SELFREG_E_CLASS;
  193. }
  194. RegCloseKey (hKey);
  195. }
  196. //
  197. // Register as an extension for various nodes
  198. //
  199. StringFromGUID2 (NODEID_User, szGUID, 50);
  200. if (_snwprintf(
  201. szSubKey,
  202. ARRAYSIZE(szSubKey),
  203. TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace"),
  204. szGUID) < 0 ) {
  205. return SELFREG_E_CLASS;
  206. }
  207. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  208. lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
  209. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  210. &hKey, &dwDisp);
  211. if (lResult != ERROR_SUCCESS) {
  212. return SELFREG_E_CLASS;
  213. }
  214. lResult = RegSetValueEx (hKey, szSnapInKey, 0, REG_SZ, (LPBYTE)szSnapInName,
  215. (lstrlen(szSnapInName) + 1) * sizeof(TCHAR));
  216. RegCloseKey (hKey);
  217. if (lResult != ERROR_SUCCESS) {
  218. return SELFREG_E_CLASS;
  219. }
  220. return S_OK;
  221. }
  222. /////////////////////////////////////////////////////////////////////////////
  223. // DllUnregisterServer - Removes entries from the system registry
  224. STDAPI DllUnregisterServer(void)
  225. {
  226. TCHAR szSnapInKey[50];
  227. TCHAR szSubKey[200];
  228. TCHAR szGUID[50];
  229. DWORD dwIndex;
  230. LONG lResult;
  231. HKEY hKey;
  232. DWORD dwDisp;
  233. StringFromGUID2 (CLSID_GPTRemoteInstall, szSnapInKey, ARRAYSIZE( szSnapInKey ));
  234. if (_snwprintf(
  235. szSubKey,
  236. ARRAYSIZE(szSubKey),
  237. TEXT("CLSID\\%s"),
  238. szSnapInKey) < 0) {
  239. return SELFREG_E_CLASS;
  240. }
  241. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  242. RegDelnode (HKEY_CLASSES_ROOT, szSubKey);
  243. if (_snwprintf(
  244. szSubKey,
  245. ARRAYSIZE(szSubKey),
  246. TEXT("Software\\Microsoft\\MMC\\SnapIns\\%s"),
  247. szSnapInKey) < 0) {
  248. return SELFREG_E_CLASS;
  249. }
  250. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  251. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  252. #if 0
  253. //
  254. // Don't delete these keys because they aren't RIS-specific. Whacking
  255. // them here means we might be killing someone else.
  256. //
  257. for (dwIndex = 0; dwIndex < NUM_NAMESPACE_ITEMS; dwIndex++)
  258. {
  259. StringFromGUID2 (*g_NameSpace[dwIndex].pNodeID, szGUID, ARRAYSIZE( szGUID ));
  260. wsprintf (szSubKey, TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s"), szGUID);
  261. RegDelnode (HKEY_LOCAL_MACHINE, szSubKey);
  262. }
  263. #endif
  264. StringFromGUID2 (NODEID_User, szGUID, ARRAYSIZE( szGUID ));
  265. if (_snwprintf(
  266. szSubKey,
  267. ARRAYSIZE(szSubKey),
  268. TEXT("Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\NameSpace"),
  269. szGUID) < 0) {
  270. return SELFREG_E_CLASS;
  271. }
  272. szSubKey[ARRAYSIZE(szSubKey)-1] = TEXT('\0');
  273. lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, szSubKey, 0,
  274. KEY_WRITE, &hKey);
  275. if (lResult == ERROR_SUCCESS) {
  276. RegDeleteValue (hKey, szSnapInKey);
  277. RegCloseKey (hKey);
  278. }
  279. return S_OK;
  280. }