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.

415 lines
12 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1997.
  5. //
  6. // File: snapin.cpp
  7. //
  8. // Contents: DLL support routines, class factory and registration
  9. // functions.
  10. //
  11. // Classes:
  12. //
  13. // Functions:
  14. //
  15. // History: 2-12-1998 stevebl comment header added
  16. // 4-22-1998 rahulth added CSection objects for the ini files
  17. //
  18. //---------------------------------------------------------------------------
  19. #include "precomp.hxx"
  20. #include "initguid.h"
  21. #include "gpedit.h"
  22. extern const CLSID CLSID_Snapin = {0x88E729D6,0xBDC1,0x11D1,{0xBD,0x2A,0x00,0xC0,0x4F,0xB9,0x60,0x3F}};
  23. extern const wchar_t * szCLSID_Snapin = L"{88E729D6-BDC1-11D1-BD2A-00C04FB9603F}";
  24. // Main NodeType GUID on numeric format
  25. extern const GUID cNodeType = {0xE0494114,0xBDC1,0x11D1,{0xBD,0x2A,0x00,0xC0,0x4F,0xB9,0x60,0x3F}};
  26. // Main NodeType GUID on string format
  27. extern const wchar_t* cszNodeType = L"{E0494114-BDC1-11D1-BD2A-00C04FB9603F}";
  28. // RSOP GUIDs
  29. extern const CLSID CLSID_RSOP_Snapin = {0XC40D66A0,0XE90C,0X46C6,{0XAA,0X3B,0X47,0X3E,0X38,0XC7,0X2B,0XF2}};
  30. extern const wchar_t * szCLSID_RSOP_Snapin = L"{C40D66A0-E90C-46C6-AA3B-473E38C72BF2}";
  31. IMalloc * g_pIMalloc;
  32. #include "safereg.hxx"
  33. #define BREAK_ON_FAIL_HRESULT(hr) if (FAILED(hr)) break
  34. #define THREADING_STR L"Apartment"
  35. //+--------------------------------------------------------------------------
  36. //
  37. // Function: RegDeleteTree
  38. //
  39. // Synopsis: deletes a registry key and all of its children
  40. //
  41. // Arguments: [hKey] - handle to the key's parent
  42. // [szSubKey] - name of the key to be deleted
  43. //
  44. // Returns: ERROR_SUCCESS
  45. //
  46. // History: 3-17-1998 stevebl Copied from ADE
  47. //
  48. //---------------------------------------------------------------------------
  49. LONG RegDeleteTree(HKEY hKey, TCHAR * szSubKey)
  50. {
  51. HKEY hKeyNew;
  52. LONG lResult = RegOpenKey(hKey, szSubKey, &hKeyNew);
  53. if (lResult != ERROR_SUCCESS)
  54. {
  55. return lResult;
  56. }
  57. TCHAR szName[256];
  58. while (ERROR_SUCCESS == RegEnumKey(hKeyNew, 0, szName, 256))
  59. {
  60. RegDeleteTree(hKeyNew, szName);
  61. }
  62. RegCloseKey(hKeyNew);
  63. return RegDeleteKey(hKey, szSubKey);
  64. }
  65. HRESULT
  66. RegisterInterface(
  67. CSafeReg *pshkInterface,
  68. LPWSTR wszInterfaceGUID,
  69. LPWSTR wszInterfaceName,
  70. LPWSTR wszNumMethods,
  71. LPWSTR wszProxyCLSID);
  72. CComModule _Module;
  73. BEGIN_OBJECT_MAP(ObjectMap)
  74. OBJECT_ENTRY(CLSID_Snapin, CUserComponentDataImpl)
  75. OBJECT_ENTRY(CLSID_RSOP_Snapin, CRSOPUserComponentDataImpl)
  76. END_OBJECT_MAP()
  77. #ifdef _DEBUG
  78. #define new DEBUG_NEW
  79. #undef THIS_FILE
  80. static char THIS_FILE[] = __FILE__;
  81. #endif
  82. class CSnapinApp : public CWinApp
  83. {
  84. public:
  85. virtual BOOL InitInstance();
  86. virtual int ExitInstance();
  87. };
  88. CSnapinApp theApp;
  89. HINSTANCE ghInstance;
  90. BOOL CSnapinApp::InitInstance()
  91. {
  92. ghInstance = m_hInstance;
  93. _Module.Init(ObjectMap, m_hInstance);
  94. if (FAILED(CoGetMalloc(1, &g_pIMalloc)))
  95. return FALSE;
  96. return CWinApp::InitInstance();
  97. }
  98. int CSnapinApp::ExitInstance()
  99. {
  100. _Module.Term();
  101. DEBUG_VERIFY_INSTANCE_COUNT(CResultPane);
  102. DEBUG_VERIFY_INSTANCE_COUNT(CScopePane);
  103. g_pIMalloc->Release();
  104. return CWinApp::ExitInstance();
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // Used to determine whether the DLL can be unloaded by OLE
  108. STDAPI DllCanUnloadNow(void)
  109. {
  110. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  111. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  112. }
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Returns a class factory to create an object of the requested type
  115. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  116. {
  117. return _Module.GetClassObject(rclsid, riid, ppv);
  118. }
  119. const wchar_t * szUser_Namespace = L"{08114B47-BDC2-11D1-BD2A-00C04FB9603F}";
  120. const wchar_t * szUserAppName = L"Folder Redirection Editor (Users)";
  121. const wchar_t * szUser_RSOP_Namespace = L"{8CDE1CC8-0D3A-4B60-99EA-27EF3D7C0174}";
  122. /////////////////////////////////////////////////////////////////////////////
  123. // DllRegisterServer - Adds entries to the system registry
  124. STDAPI DllRegisterServer(void)
  125. {
  126. AFX_MANAGE_STATE (AfxGetStaticModuleState());
  127. CSafeReg shk;
  128. CSafeReg shkCLSID;
  129. CSafeReg shkServer;
  130. CSafeReg shkTemp;
  131. WCHAR szMUIUserAppName[MAX_PATH + 50];
  132. WCHAR szModule[MAX_PATH];
  133. // Get the path to the module.
  134. if (0 != ::GetModuleFileName(AfxGetInstanceHandle(), szModule, MAX_PATH))
  135. {
  136. // MUI: MMC: Use the new NameStringIndirect value of MMC to display
  137. // a localized name for the snap-in while using MUI.
  138. wsprintf (szMUIUserAppName, L"@%s,-%d", szModule, IDS_SNAPIN_NAME);
  139. }
  140. else
  141. {
  142. szMUIUserAppName[0] = L'\0';
  143. }
  144. HRESULT hr = S_OK;
  145. do
  146. {
  147. hr = _Module.RegisterServer(FALSE);
  148. BREAK_ON_FAIL_HRESULT(hr);
  149. // register extension
  150. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns", KEY_WRITE);
  151. BREAK_ON_FAIL_HRESULT(hr);
  152. hr = shkCLSID.Create(szCLSID_Snapin, &shk);
  153. BREAK_ON_FAIL_HRESULT(hr);
  154. hr = shk.SetValue(L"NameString",
  155. REG_SZ,
  156. (CONST BYTE *) szUserAppName,
  157. sizeof(WCHAR) * (lstrlen(szUserAppName)+ 1));
  158. // MUI: MMC: Use localized snap-in name for MUI.
  159. if (L'\0' != szMUIUserAppName[0])
  160. {
  161. hr = shk.SetValue(L"NameStringIndirect",
  162. REG_SZ,
  163. (CONST BYTE *) szMUIUserAppName,
  164. sizeof(WCHAR) * (lstrlen(szMUIUserAppName) + 1));
  165. }
  166. hr = shk.Create(L"NodeTypes", &shkTemp);
  167. BREAK_ON_FAIL_HRESULT(hr);
  168. hr = shkTemp.Create(szUser_Namespace, &shkServer);
  169. BREAK_ON_FAIL_HRESULT(hr);
  170. shkServer.Close();
  171. shkTemp.Close();
  172. shk.Close();
  173. shkCLSID.Close();
  174. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes", KEY_WRITE);
  175. BREAK_ON_FAIL_HRESULT(hr);
  176. hr = shkCLSID.Create(szUser_Namespace, &shk);
  177. BREAK_ON_FAIL_HRESULT(hr);
  178. shk.Close();
  179. WCHAR szGUID[50];
  180. StringFromGUID2 (NODEID_User, szGUID, 50);
  181. hr = shkCLSID.Create(szGUID, &shk);
  182. BREAK_ON_FAIL_HRESULT(hr);
  183. hr = shk.Create(L"Extensions", &shkServer);
  184. BREAK_ON_FAIL_HRESULT(hr);
  185. hr = shkServer.Create(L"NameSpace", &shkTemp);
  186. BREAK_ON_FAIL_HRESULT(hr);
  187. hr = shkTemp.SetValue(szCLSID_Snapin,
  188. REG_SZ,
  189. (CONST BYTE *) szUserAppName,
  190. sizeof(WCHAR) * (lstrlen(szUserAppName)+ 1));
  191. shkTemp.Close();
  192. shkServer.Close();
  193. shk.Close();
  194. shkCLSID.Close();
  195. // register RSOP extension
  196. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns", KEY_WRITE);
  197. BREAK_ON_FAIL_HRESULT(hr);
  198. hr = shkCLSID.Create(szCLSID_RSOP_Snapin, &shk);
  199. BREAK_ON_FAIL_HRESULT(hr);
  200. hr = shk.SetValue(L"NameString",
  201. REG_SZ,
  202. (CONST BYTE *) szUserAppName,
  203. sizeof(WCHAR) * (lstrlen(szUserAppName)+ 1));
  204. // MUI: MMC: Use localized snap-in name for MUI.
  205. if (L'\0' != szMUIUserAppName[0])
  206. {
  207. hr = shk.SetValue(L"NameStringIndirect",
  208. REG_SZ,
  209. (CONST BYTE *) szMUIUserAppName,
  210. sizeof (WCHAR) * (lstrlen(szMUIUserAppName) + 1));
  211. }
  212. hr = shk.Create(L"NodeTypes", &shkTemp);
  213. BREAK_ON_FAIL_HRESULT(hr);
  214. hr = shkTemp.Create(szUser_RSOP_Namespace, &shkServer);
  215. BREAK_ON_FAIL_HRESULT(hr);
  216. shkServer.Close();
  217. shkTemp.Close();
  218. shk.Close();
  219. shkCLSID.Close();
  220. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes", KEY_WRITE);
  221. BREAK_ON_FAIL_HRESULT(hr);
  222. hr = shkCLSID.Create(szUser_RSOP_Namespace, &shk);
  223. BREAK_ON_FAIL_HRESULT(hr);
  224. shk.Close();
  225. StringFromGUID2 (NODEID_RSOPUser, szGUID, 50);
  226. hr = shkCLSID.Create(szGUID, &shk);
  227. BREAK_ON_FAIL_HRESULT(hr);
  228. hr = shk.Create(L"Extensions", &shkServer);
  229. BREAK_ON_FAIL_HRESULT(hr);
  230. hr = shkServer.Create(L"NameSpace", &shkTemp);
  231. BREAK_ON_FAIL_HRESULT(hr);
  232. hr = shkTemp.SetValue(szCLSID_RSOP_Snapin,
  233. REG_SZ,
  234. (CONST BYTE *) szUserAppName,
  235. sizeof(WCHAR) * (lstrlen(szUserAppName)+ 1));
  236. shkTemp.Close();
  237. shkServer.Close();
  238. shk.Close();
  239. shkCLSID.Close();
  240. } while (0);
  241. return hr;
  242. }
  243. /////////////////////////////////////////////////////////////////////////////
  244. // DllUnregisterServer - Removes entries from the system registry
  245. STDAPI DllUnregisterServer(void)
  246. {
  247. _Module.UnregisterServer();
  248. HKEY hkey;
  249. CString sz;
  250. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns\\", 0, KEY_WRITE, &hkey);
  251. RegDeleteTree(hkey, (LPOLESTR)((LPCOLESTR)szCLSID_Snapin));
  252. RegCloseKey(hkey);
  253. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes\\", 0, KEY_WRITE, &hkey);
  254. RegDeleteTree(HKEY_LOCAL_MACHINE, (LPOLESTR)((LPCOLESTR)szUser_Namespace));
  255. RegCloseKey(hkey);
  256. WCHAR szGUID[50];
  257. sz = L"Software\\Microsoft\\MMC\\NodeTypes\\";
  258. StringFromGUID2 (NODEID_User, szGUID, 50);
  259. sz += szGUID;
  260. sz += L"\\Extensions\\NameSpace";
  261. RegOpenKeyEx(HKEY_LOCAL_MACHINE, sz, 0, KEY_WRITE, &hkey);
  262. RegDeleteValue(hkey, szCLSID_Snapin);
  263. RegCloseKey(hkey);
  264. // unregister RSOP nodes
  265. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns\\", 0, KEY_WRITE, &hkey);
  266. RegDeleteTree(hkey, (LPOLESTR)((LPCOLESTR)szCLSID_RSOP_Snapin));
  267. RegCloseKey(hkey);
  268. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes\\", 0, KEY_WRITE, &hkey);
  269. RegDeleteTree(HKEY_LOCAL_MACHINE, (LPOLESTR)((LPCOLESTR)szUser_RSOP_Namespace));
  270. RegCloseKey(hkey);
  271. sz = L"Software\\Microsoft\\MMC\\NodeTypes\\";
  272. StringFromGUID2 (NODEID_RSOPUser, szGUID, 50);
  273. sz += szGUID;
  274. sz += L"\\Extensions\\NameSpace";
  275. RegOpenKeyEx(HKEY_LOCAL_MACHINE, sz, 0, KEY_WRITE, &hkey);
  276. RegDeleteValue(hkey, szCLSID_RSOP_Snapin);
  277. RegCloseKey(hkey);
  278. return S_OK;
  279. }
  280. //+--------------------------------------------------------------------------
  281. //
  282. // Function: RegisterInterface
  283. //
  284. // Synopsis: Add the registry entries required for an interface.
  285. //
  286. // Arguments: [pshkInterface] - handle to CLSID\Interface key
  287. // [wszInterfaceGUID] - GUID of interface to add
  288. // [wszInterfaceName] - human-readable name of interface
  289. // [wszNumMethods] - number of methods (including inherited)
  290. // [wszProxyCLSID] - GUID of dll containing proxy/stubs
  291. //
  292. // Returns: HRESULT
  293. //
  294. // History: 3-31-1997 DavidMun Created
  295. // 5-09-1997 SteveBl Modified for use with AppMgr
  296. //
  297. //---------------------------------------------------------------------------
  298. HRESULT
  299. RegisterInterface(
  300. CSafeReg *pshkInterface,
  301. LPWSTR wszInterfaceGUID,
  302. LPWSTR wszInterfaceName,
  303. LPWSTR wszNumMethods,
  304. LPWSTR wszProxyCLSID)
  305. {
  306. HRESULT hr = S_OK;
  307. CSafeReg shkIID;
  308. CSafeReg shkNumMethods;
  309. CSafeReg shkProxy;
  310. do
  311. {
  312. hr = pshkInterface->Create(wszInterfaceGUID, &shkIID);
  313. BREAK_ON_FAIL_HRESULT(hr);
  314. hr = shkIID.SetValue(NULL,
  315. REG_SZ,
  316. (CONST BYTE *) wszInterfaceName,
  317. sizeof(WCHAR) * (lstrlen(wszInterfaceName) + 1));
  318. BREAK_ON_FAIL_HRESULT(hr);
  319. hr = shkIID.Create(L"NumMethods", &shkNumMethods);
  320. BREAK_ON_FAIL_HRESULT(hr);
  321. hr = shkNumMethods.SetValue(NULL,
  322. REG_SZ,
  323. (CONST BYTE *)wszNumMethods,
  324. sizeof(WCHAR) * (lstrlen(wszNumMethods) + 1));
  325. BREAK_ON_FAIL_HRESULT(hr);
  326. hr = shkIID.Create(L"ProxyStubClsid32", &shkProxy);
  327. BREAK_ON_FAIL_HRESULT(hr);
  328. hr = shkProxy.SetValue(NULL,
  329. REG_SZ,
  330. (CONST BYTE *)wszProxyCLSID,
  331. sizeof(WCHAR) * (lstrlen(wszProxyCLSID) + 1));
  332. BREAK_ON_FAIL_HRESULT(hr);
  333. } while (0);
  334. return hr;
  335. }