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.

478 lines
13 KiB

  1. //==============================================================;
  2. //
  3. // This source code is only intended as a supplement to existing Microsoft documentation.
  4. //
  5. //
  6. //
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  9. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11. // PURPOSE.
  12. //
  13. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  14. //
  15. //
  16. //
  17. //==============================================================;
  18. #include <objbase.h>
  19. #include <assert.h>
  20. #include "Extend.h"
  21. #include "Registry.h"
  22. #define STRINGS_ONLY
  23. #include "globals.h"
  24. // we need access to our g_nodes
  25. extern NODESTRUCT g_Nodes[];
  26. ////////////////////////////////////////////////////////
  27. //
  28. // Internal helper functions prototypes
  29. //
  30. // Set the given key and its value.
  31. BOOL setKeyAndValue(const _TCHAR* pszPath,
  32. const _TCHAR* szSubkey,
  33. const _TCHAR* szValue) ;
  34. // Set the given key and its value in the MMC Snapin location
  35. BOOL setSnapInKeyAndValue(const _TCHAR* szKey,
  36. const _TCHAR* szSubkey,
  37. const _TCHAR* szName,
  38. const _TCHAR* szValue);
  39. BOOL setSnapInExtensionNode(const _TCHAR* szSnapID,
  40. const _TCHAR* szNodeID,
  41. const _TCHAR* szDescription);
  42. // Set the given valuename under the key to value
  43. BOOL setValue(const _TCHAR* szKey,
  44. const _TCHAR* szValueName,
  45. const _TCHAR* szValue);
  46. // Delete szKeyChild and all of its descendents.
  47. LONG recursiveDeleteKey(HKEY hKeyParent, const _TCHAR* szKeyChild) ;
  48. ////////////////////////////////////////////////////////
  49. //
  50. // Constants
  51. //
  52. // Size of a CLSID as a string
  53. //const int CLSID_STRING_SIZE = 39 ;
  54. /////////////////////////////////////////////////////////
  55. //
  56. // Public function implementation
  57. //
  58. //
  59. // Register the component in the registry.
  60. //
  61. HRESULT RegisterServer(HMODULE hModule, // DLL module handle
  62. const CLSID& clsid, // Class ID
  63. const _TCHAR* szFriendlyName) // IDs
  64. {
  65. // Get server location.
  66. _TCHAR szModule[512] ;
  67. DWORD dwResult =
  68. ::GetModuleFileName(hModule,
  69. szModule,
  70. sizeof(szModule)/sizeof(_TCHAR)) ;
  71. assert(dwResult != 0) ;
  72. // Get CLSID
  73. LPOLESTR wszCLSID = NULL ;
  74. HRESULT hr = StringFromCLSID(clsid, &wszCLSID) ;
  75. assert(SUCCEEDED(hr)) ;
  76. MAKE_TSTRPTR_FROMWIDE(pszCLSID, wszCLSID);
  77. // Build the key CLSID\\{...}
  78. _TCHAR szKey[64] ;
  79. _tcscpy(szKey, _T("CLSID\\")) ;
  80. _tcscat(szKey, pszCLSID) ;
  81. // Add the CLSID to the registry.
  82. setKeyAndValue(szKey, NULL, szFriendlyName) ;
  83. // Add the server filename subkey under the CLSID key.
  84. setKeyAndValue(szKey, _T("InprocServer32"), szModule) ;
  85. // set the threading model
  86. _tcscat(szKey, _T("\\InprocServer32"));
  87. setValue(szKey, _T("ThreadingModel"), _T("Apartment"));
  88. // Free memory.
  89. CoTaskMemFree(wszCLSID) ;
  90. return S_OK ;
  91. }
  92. //
  93. // Remove the component from the registry.
  94. //
  95. LONG UnregisterServer(const CLSID& clsid) // IDs
  96. {
  97. // Get CLSID
  98. LPOLESTR wszCLSID = NULL ;
  99. HRESULT hr = StringFromCLSID(clsid, &wszCLSID) ;
  100. // Build the key CLSID\\{...}
  101. _TCHAR szKey[64] ;
  102. _tcscpy(szKey, _T("CLSID\\")) ;
  103. MAKE_TSTRPTR_FROMWIDE(pszT, wszCLSID);
  104. _tcscat(szKey, pszT) ;
  105. // Delete the CLSID Key - CLSID\{...}
  106. LONG lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey) ;
  107. assert((lResult == ERROR_SUCCESS) ||
  108. (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist.
  109. // Free memory.
  110. CoTaskMemFree(wszCLSID) ;
  111. return S_OK ;
  112. }
  113. //
  114. // Register the snap-in in the registry.
  115. //
  116. HRESULT RegisterSnapin(const CLSID& clsid, // Class ID
  117. const _TCHAR* szNameString, // NameString
  118. const CLSID& clsidAbout) // Class Id for About Class
  119. {
  120. // Get CLSID
  121. LPOLESTR wszCLSID = NULL ;
  122. LPOLESTR wszAboutCLSID = NULL;
  123. LPOLESTR wszExtendCLSID = NULL;
  124. NODESTRUCT *pNode;
  125. HRESULT hr = StringFromCLSID(clsid, &wszCLSID) ;
  126. if (IID_NULL != clsidAbout)
  127. hr = StringFromCLSID(clsidAbout, &wszAboutCLSID);
  128. MAKE_TSTRPTR_FROMWIDE(pszCLSID, wszCLSID);
  129. MAKE_TSTRPTR_FROMWIDE(pszAboutCLSID, wszAboutCLSID);
  130. // Add the CLSID to the registry.
  131. setSnapInKeyAndValue(pszCLSID, NULL, _T("NameString"), szNameString) ;
  132. setSnapInKeyAndValue(pszCLSID, _T("StandAlone"), NULL, NULL);
  133. if (IID_NULL != clsidAbout)
  134. setSnapInKeyAndValue(pszCLSID, NULL, _T("About"), pszAboutCLSID);
  135. // register each of the node types in g_Nodes as an extendable node
  136. for (pNode = &(g_Nodes[0]);*pNode->szDescription;pNode++)
  137. {
  138. hr = StringFromCLSID(pNode->GUID, &wszExtendCLSID);
  139. MAKE_TSTRPTR_FROMWIDE(pszExtendCLSID, wszExtendCLSID);
  140. setSnapInExtensionNode(pszCLSID, pszExtendCLSID, pNode->szDescription);
  141. CoTaskMemFree(wszExtendCLSID);
  142. }
  143. // Free memory.
  144. CoTaskMemFree(wszCLSID) ;
  145. if (IID_NULL != clsidAbout)
  146. CoTaskMemFree(wszAboutCLSID);
  147. return S_OK ;
  148. }
  149. //
  150. // Unregister the snap-in in the registry.
  151. //
  152. HRESULT UnregisterSnapin(const CLSID& clsid) // Class ID
  153. {
  154. _TCHAR szKeyBuf[1024];
  155. LPOLESTR wszCLSID = NULL;
  156. // Get CLSID
  157. HRESULT hr = StringFromCLSID(clsid, &wszCLSID);
  158. MAKE_TSTRPTR_FROMWIDE(pszCLSID, wszCLSID);
  159. // Load the buffer with the Snap-In Location
  160. _tcscpy(szKeyBuf, _T("SOFTWARE\\Microsoft\\MMC\\SnapIns"));
  161. // Copy keyname into buffer.
  162. _tcscat(szKeyBuf, _T("\\"));
  163. _tcscat(szKeyBuf, pszCLSID);
  164. // Delete the CLSID Key - CLSID\{...}
  165. LONG lResult = recursiveDeleteKey(HKEY_LOCAL_MACHINE, szKeyBuf);
  166. assert((lResult == ERROR_SUCCESS) ||
  167. (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist.
  168. //Uncomment following for loop to unregister all extendable node types
  169. //Note that if a snap-in's extendable node types are unregistered,
  170. //any extension snap-ins for these node types will have to be re-registered
  171. //in order to rebuild their entries under the SOFTWARE\Microsoft\MMC\NodeTypes key
  172. /*
  173. // Unregister each of the node types in g_Nodes as an extendable node
  174. NODESTRUCT *pNode;
  175. LPOLESTR wszExtendCLSID = NULL;
  176. for (pNode = &(g_Nodes[0]);*pNode->szDescription;pNode++)
  177. {
  178. hr = StringFromCLSID(pNode->GUID, &wszExtendCLSID);
  179. // Load the buffer with the Snap-In Location
  180. MAKE_TSTRPTR_FROMWIDE(pszExtendCLSID, wszExtendCLSID);
  181. _tcscpy(szKeyBuf, _T("SOFTWARE\\Microsoft\\MMC\\NodeTypes\\"));
  182. // Copy keyname into buffer.
  183. _tcscat(szKeyBuf, pszExtendCLSID);
  184. recursiveDeleteKey(HKEY_LOCAL_MACHINE, szKeyBuf);
  185. CoTaskMemFree(wszExtendCLSID);
  186. }
  187. */
  188. // free the memory
  189. CoTaskMemFree(wszCLSID);
  190. return S_OK;
  191. }
  192. //
  193. // Delete a key and all of its descendents.
  194. //
  195. LONG recursiveDeleteKey(HKEY hKeyParent, // Parent of key to delete
  196. const _TCHAR* lpszKeyChild) // Key to delete
  197. {
  198. // Open the child.
  199. HKEY hKeyChild ;
  200. LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0,
  201. KEY_ALL_ACCESS, &hKeyChild) ;
  202. if (lRes != ERROR_SUCCESS)
  203. {
  204. return lRes ;
  205. }
  206. // Enumerate all of the decendents of this child.
  207. FILETIME time ;
  208. _TCHAR szBuffer[256] ;
  209. DWORD dwSize = 256 ;
  210. while (RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, NULL,
  211. NULL, NULL, &time) == S_OK)
  212. {
  213. // Delete the decendents of this child.
  214. lRes = recursiveDeleteKey(hKeyChild, szBuffer) ;
  215. if (lRes != ERROR_SUCCESS)
  216. {
  217. // Cleanup before exiting.
  218. RegCloseKey(hKeyChild) ;
  219. return lRes;
  220. }
  221. dwSize = 256 ;
  222. }
  223. // Close the child.
  224. RegCloseKey(hKeyChild) ;
  225. // Delete this child.
  226. return RegDeleteKey(hKeyParent, lpszKeyChild) ;
  227. }
  228. //
  229. // Create a key and set its value.
  230. // - This helper function was borrowed and modifed from
  231. // Kraig Brockschmidt's book Inside OLE.
  232. //
  233. BOOL setKeyAndValue(const _TCHAR* szKey,
  234. const _TCHAR* szSubkey,
  235. const _TCHAR* szValue)
  236. {
  237. HKEY hKey;
  238. _TCHAR szKeyBuf[1024] ;
  239. // Copy keyname into buffer.
  240. _tcscpy(szKeyBuf, szKey) ;
  241. // Add subkey name to buffer.
  242. if (szSubkey != NULL)
  243. {
  244. _tcscat(szKeyBuf, _T("\\")) ;
  245. _tcscat(szKeyBuf, szSubkey ) ;
  246. }
  247. // Create and open key and subkey.
  248. long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT ,
  249. szKeyBuf,
  250. 0, NULL, REG_OPTION_NON_VOLATILE,
  251. KEY_ALL_ACCESS, NULL,
  252. &hKey, NULL) ;
  253. if (lResult != ERROR_SUCCESS)
  254. {
  255. return FALSE ;
  256. }
  257. // Set the Value.
  258. if (szValue != NULL)
  259. {
  260. RegSetValueEx(hKey, NULL, 0, REG_SZ,
  261. (BYTE *)szValue,
  262. (_tcslen(szValue)+1)*sizeof(_TCHAR)) ;
  263. }
  264. RegCloseKey(hKey) ;
  265. return TRUE ;
  266. }
  267. //
  268. // Open a key value and set it
  269. //
  270. BOOL setValue(const _TCHAR* szKey,
  271. const _TCHAR* szValueName,
  272. const _TCHAR* szValue)
  273. {
  274. HKEY hKey;
  275. _TCHAR szKeyBuf[1024] ;
  276. // Copy keyname into buffer.
  277. _tcscpy(szKeyBuf, szKey) ;
  278. // Create and open key and subkey.
  279. long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT ,
  280. szKeyBuf,
  281. 0, NULL, REG_OPTION_NON_VOLATILE,
  282. KEY_ALL_ACCESS, NULL,
  283. &hKey, NULL) ;
  284. if (lResult != ERROR_SUCCESS)
  285. {
  286. return FALSE ;
  287. }
  288. // Set the Value.
  289. if (szValue != NULL)
  290. {
  291. RegSetValueEx(hKey, szValueName, 0, REG_SZ,
  292. (BYTE *)szValue,
  293. (_tcslen(szValue)+1)*sizeof(_TCHAR)) ;
  294. }
  295. RegCloseKey(hKey) ;
  296. return TRUE ;
  297. }
  298. //
  299. // Create a key and set its value.
  300. // - This helper function was borrowed and modifed from
  301. // Kraig Brockschmidt's book Inside OLE.
  302. //
  303. BOOL setSnapInKeyAndValue(const _TCHAR* szKey,
  304. const _TCHAR* szSubkey,
  305. const _TCHAR* szName,
  306. const _TCHAR* szValue)
  307. {
  308. HKEY hKey;
  309. _TCHAR szKeyBuf[1024] ;
  310. // Load the buffer with the Snap-In Location
  311. _tcscpy(szKeyBuf, _T("SOFTWARE\\Microsoft\\MMC\\SnapIns"));
  312. // Copy keyname into buffer.
  313. _tcscat(szKeyBuf, _T("\\")) ;
  314. _tcscat(szKeyBuf, szKey) ;
  315. // Add subkey name to buffer.
  316. if (szSubkey != NULL)
  317. {
  318. _tcscat(szKeyBuf, _T("\\")) ;
  319. _tcscat(szKeyBuf, szSubkey ) ;
  320. }
  321. // Create and open key and subkey.
  322. long lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE ,
  323. szKeyBuf,
  324. 0, NULL, REG_OPTION_NON_VOLATILE,
  325. KEY_ALL_ACCESS, NULL,
  326. &hKey, NULL) ;
  327. if (lResult != ERROR_SUCCESS)
  328. {
  329. return FALSE ;
  330. }
  331. // Set the Value.
  332. if (szValue != NULL)
  333. {
  334. RegSetValueEx(hKey, szName, 0, REG_SZ,
  335. (BYTE *)szValue,
  336. (_tcslen(szValue)+1)*sizeof(_TCHAR)) ;
  337. }
  338. RegCloseKey(hKey) ;
  339. return TRUE ;
  340. }
  341. BOOL setSnapInExtensionNode(const _TCHAR* szSnapID,
  342. const _TCHAR* szNodeID,
  343. const _TCHAR* szDescription)
  344. {
  345. HKEY hKey;
  346. _TCHAR szSnapNodeKeyBuf[1024] ;
  347. _TCHAR szMMCNodeKeyBuf[1024];
  348. // Load the buffer with the Snap-In Location
  349. _tcscpy(szSnapNodeKeyBuf, _T("SOFTWARE\\Microsoft\\MMC\\SnapIns\\"));
  350. // add in the clisid into buffer.
  351. _tcscat(szSnapNodeKeyBuf, szSnapID) ;
  352. _tcscat(szSnapNodeKeyBuf, _T("\\NodeTypes\\"));
  353. _tcscat(szSnapNodeKeyBuf, szNodeID) ;
  354. // Load the buffer with the NodeTypes Location
  355. _tcscpy(szMMCNodeKeyBuf, _T("SOFTWARE\\Microsoft\\MMC\\NodeTypes\\"));
  356. _tcscat(szMMCNodeKeyBuf, szNodeID) ;
  357. // Create and open the Snapin Key.
  358. long lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE ,
  359. szSnapNodeKeyBuf,
  360. 0, NULL, REG_OPTION_NON_VOLATILE,
  361. KEY_ALL_ACCESS, NULL,
  362. &hKey, NULL) ;
  363. if (lResult != ERROR_SUCCESS)
  364. {
  365. return FALSE ;
  366. }
  367. // Set the Value.
  368. if (szDescription != NULL)
  369. {
  370. RegSetValueEx(hKey, NULL, 0, REG_SZ,
  371. (BYTE *)szDescription,
  372. (_tcslen(szDescription)+1)*sizeof(_TCHAR)) ;
  373. }
  374. RegCloseKey(hKey) ;
  375. // Create and open the NodeTypes Key.
  376. lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE ,
  377. szMMCNodeKeyBuf,
  378. 0, NULL, REG_OPTION_NON_VOLATILE,
  379. KEY_ALL_ACCESS, NULL,
  380. &hKey, NULL) ;
  381. if (lResult != ERROR_SUCCESS)
  382. {
  383. return FALSE ;
  384. }
  385. // Set the Value.
  386. if (szDescription != NULL)
  387. {
  388. RegSetValueEx(hKey, NULL, 0, REG_SZ,
  389. (BYTE *)szDescription,
  390. (_tcslen(szDescription)+1)*sizeof(_TCHAR)) ;
  391. }
  392. RegCloseKey(hKey) ;
  393. return TRUE ;
  394. }