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.

340 lines
10 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. //
  17. //---------------------------------------------------------------------------
  18. #include "precomp.hxx"
  19. #include "initguid.h"
  20. #include "gpedit.h"
  21. extern const CLSID CLSID_Snapin = {0xBACF5C8A,0xA3C7,0x11D1,{0xA7,0x60,0x00,0xC0,0x4F,0xB9,0x60,0x3F}};
  22. extern const wchar_t * szCLSID_Snapin = L"{BACF5C8A-A3C7-11D1-A760-00C04FB9603F}";
  23. extern const CLSID CLSID_MachineSnapin = {0x942A8E4F,0xA261,0x11D1,{0xA7,0x60,0x00,0xc0,0x4f,0xb9,0x60,0x3f}};
  24. extern const wchar_t * szCLSID_MachineSnapin = L"{942A8E4F-A261-11D1-A760-00C04FB9603F}";
  25. // Main NodeType GUID on numeric format
  26. extern const GUID cNodeType = {0xF8B3A900,0X8EA5,0X11D0,{0X8D,0X3C,0X00,0XA0,0XC9,0X0D,0XCA,0XE7}};
  27. // Main NodeType GUID on string format
  28. extern const wchar_t* cszNodeType = L"{F8B3A900-8EA5-11D0-8D3C-00A0C90DCAE7}";
  29. #include "safereg.hxx"
  30. #define BREAK_ON_FAIL_HRESULT(hr) if (FAILED(hr)) break
  31. #define PSBUFFER_STR L"AppManagementBuffer"
  32. #define THREADING_STR L"Apartment"
  33. HRESULT
  34. RegisterInterface(
  35. CSafeReg *pshkInterface,
  36. LPWSTR wszInterfaceGUID,
  37. LPWSTR wszInterfaceName,
  38. LPWSTR wszNumMethods,
  39. LPWSTR wszProxyCLSID);
  40. CComModule _Module;
  41. BEGIN_OBJECT_MAP(ObjectMap)
  42. OBJECT_ENTRY(CLSID_Snapin, CUserComponentDataImpl)
  43. OBJECT_ENTRY(CLSID_MachineSnapin, CMachineComponentDataImpl)
  44. END_OBJECT_MAP()
  45. CLSID CLSID_Temp;
  46. #ifdef _DEBUG
  47. #define new DEBUG_NEW
  48. #undef THIS_FILE
  49. static char THIS_FILE[] = __FILE__;
  50. #endif
  51. class CSnapinApp : public CWinApp
  52. {
  53. public:
  54. virtual BOOL InitInstance();
  55. virtual int ExitInstance();
  56. };
  57. CSnapinApp theApp;
  58. HINSTANCE ghInstance;
  59. BOOL CSnapinApp::InitInstance()
  60. {
  61. ghInstance = m_hInstance;
  62. _Module.Init(ObjectMap, m_hInstance);
  63. CoGetMalloc(1, &g_pIMalloc);
  64. return CWinApp::InitInstance();
  65. }
  66. int CSnapinApp::ExitInstance()
  67. {
  68. _Module.Term();
  69. DEBUG_VERIFY_INSTANCE_COUNT(CSnapin);
  70. DEBUG_VERIFY_INSTANCE_COUNT(CComponentDataImpl);
  71. g_pIMalloc->Release();
  72. return CWinApp::ExitInstance();
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Used to determine whether the DLL can be unloaded by OLE
  76. STDAPI DllCanUnloadNow(void)
  77. {
  78. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  79. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  80. }
  81. /////////////////////////////////////////////////////////////////////////////
  82. // Returns a class factory to create an object of the requested type
  83. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  84. {
  85. return _Module.GetClassObject(rclsid, riid, ppv);
  86. }
  87. const wchar_t * szUser_Namespace = L"{59849DF9-A256-11D1-A760-00C04FB9603}";
  88. const wchar_t * szMachine_Namespace = L"{4D53F093-A260-11D1-A760-00C04FB9603F}";
  89. const wchar_t * szMachineAppName = L"Application Deployment Editor (Computers)";
  90. const wchar_t * szUserAppName = L"Application Deployment Editor (Users)";
  91. /////////////////////////////////////////////////////////////////////////////
  92. // DllRegisterServer - Adds entries to the system registry
  93. STDAPI DllRegisterServer(void)
  94. {
  95. CSafeReg shk;
  96. CSafeReg shkCLSID;
  97. CSafeReg shkServer;
  98. CSafeReg shkTemp;
  99. HRESULT hr = S_OK;
  100. do
  101. {
  102. CLSID_Temp = CLSID_Snapin;
  103. hr = _Module.RegisterServer(FALSE);
  104. BREAK_ON_FAIL_HRESULT(hr);
  105. // register extension
  106. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns", KEY_WRITE);
  107. BREAK_ON_FAIL_HRESULT(hr);
  108. hr = shkCLSID.Create(szCLSID_Snapin, &shk);
  109. BREAK_ON_FAIL_HRESULT(hr);
  110. hr = shk.SetValue(L"NameString",
  111. REG_SZ,
  112. (CONST BYTE *) szUserAppName,
  113. sizeof(WCHAR) * (lstrlen(szUserAppName)+ 1));
  114. hr = shk.Create(L"NodeTypes", &shkTemp);
  115. BREAK_ON_FAIL_HRESULT(hr);
  116. hr = shkTemp.Create(szUser_Namespace, &shkServer);
  117. BREAK_ON_FAIL_HRESULT(hr);
  118. shkServer.Close();
  119. shkTemp.Close();
  120. shk.Close();
  121. hr = shkCLSID.Create(szCLSID_MachineSnapin, &shk);
  122. BREAK_ON_FAIL_HRESULT(hr);
  123. hr = shk.SetValue(L"NameString",
  124. REG_SZ,
  125. (CONST BYTE *) szMachineAppName,
  126. sizeof(WCHAR) * (lstrlen(szMachineAppName)+ 1));
  127. hr = shk.Create(L"NodeTypes", &shkTemp);
  128. BREAK_ON_FAIL_HRESULT(hr);
  129. hr = shkTemp.Create(szMachine_Namespace, &shkServer);
  130. BREAK_ON_FAIL_HRESULT(hr);
  131. shkServer.Close();
  132. shkTemp.Close();
  133. shk.Close();
  134. shkCLSID.Close();
  135. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes", KEY_WRITE);
  136. BREAK_ON_FAIL_HRESULT(hr);
  137. hr = shkCLSID.Create(szUser_Namespace, &shk);
  138. BREAK_ON_FAIL_HRESULT(hr);
  139. shk.Close();
  140. hr = shkCLSID.Create(szMachine_Namespace, &shk);
  141. BREAK_ON_FAIL_HRESULT(hr);
  142. shk.Close();
  143. WCHAR szGUID[50];
  144. StringFromGUID2 (NODEID_User, szGUID, 50);
  145. hr = shkCLSID.Create(szGUID, &shk);
  146. BREAK_ON_FAIL_HRESULT(hr);
  147. hr = shk.Create(L"Extensions", &shkServer);
  148. BREAK_ON_FAIL_HRESULT(hr);
  149. hr = shkServer.Create(L"NameSpace", &shkTemp);
  150. BREAK_ON_FAIL_HRESULT(hr);
  151. hr = shkTemp.SetValue(szCLSID_Snapin,
  152. REG_SZ,
  153. (CONST BYTE *) szUserAppName,
  154. sizeof(WCHAR) * (lstrlen(szUserAppName)+ 1));
  155. shkTemp.Close();
  156. shkServer.Close();
  157. shk.Close();
  158. StringFromGUID2 (NODEID_Machine, szGUID, 50);
  159. hr = shkCLSID.Create(szGUID, &shk);
  160. BREAK_ON_FAIL_HRESULT(hr);
  161. hr = shk.Create(L"Extensions", &shkServer);
  162. BREAK_ON_FAIL_HRESULT(hr);
  163. hr = shkServer.Create(L"NameSpace", &shkTemp);
  164. BREAK_ON_FAIL_HRESULT(hr);
  165. hr = shkTemp.SetValue(szCLSID_MachineSnapin,
  166. REG_SZ,
  167. (CONST BYTE *) szMachineAppName,
  168. sizeof(WCHAR) * (lstrlen(szMachineAppName)+ 1));
  169. shkTemp.Close();
  170. shkServer.Close();
  171. shk.Close();
  172. shkCLSID.Close();
  173. hr = shkCLSID.Open(HKEY_CLASSES_ROOT, L"CLSID", KEY_WRITE);
  174. BREAK_ON_FAIL_HRESULT(hr);
  175. } while (0);
  176. return hr;
  177. }
  178. /////////////////////////////////////////////////////////////////////////////
  179. // DllUnregisterServer - Removes entries from the system registry
  180. STDAPI DllUnregisterServer(void)
  181. {
  182. CLSID_Temp = CLSID_Snapin;
  183. _Module.UnregisterServer();
  184. HKEY hkey;
  185. CString sz;
  186. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns\\", 0, KEY_WRITE, &hkey);
  187. RegDeleteTree(hkey, (LPOLESTR)((LPCOLESTR)szCLSID_Snapin));
  188. RegCloseKey(hkey);
  189. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes\\", 0, KEY_WRITE, &hkey);
  190. RegDeleteTree(HKEY_LOCAL_MACHINE, (LPOLESTR)((LPCOLESTR)szUser_Namespace));
  191. RegCloseKey(hkey);
  192. WCHAR szGUID[50];
  193. sz = L"Software\\Microsoft\\MMC\\NodeTypes\\";
  194. StringFromGUID2 (NODEID_User, szGUID, 50);
  195. sz += szGUID;
  196. sz += L"\\Extensions\\NameSpace";
  197. RegOpenKeyEx(HKEY_LOCAL_MACHINE, sz, 0, KEY_WRITE, &hkey);
  198. RegDeleteValue(hkey, szCLSID_Snapin);
  199. RegCloseKey(hkey);
  200. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns\\", 0, KEY_WRITE, &hkey);
  201. RegDeleteTree(hkey, (LPOLESTR)((LPCOLESTR)szCLSID_MachineSnapin));
  202. RegCloseKey(hkey);
  203. RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes\\", 0, KEY_WRITE, &hkey);
  204. RegDeleteTree(HKEY_LOCAL_MACHINE, (LPOLESTR)((LPCOLESTR)szMachine_Namespace));
  205. RegCloseKey(hkey);
  206. sz = L"Software\\Microsoft\\MMC\\NodeTypes\\";
  207. StringFromGUID2 (NODEID_Machine, szGUID, 50);
  208. sz += szGUID;
  209. sz += L"\\Extensions\\NameSpace";
  210. RegOpenKeyEx(HKEY_LOCAL_MACHINE, sz, 0, KEY_WRITE, &hkey);
  211. RegDeleteValue(hkey, szCLSID_MachineSnapin);
  212. RegCloseKey(hkey);
  213. return S_OK;
  214. }
  215. //+--------------------------------------------------------------------------
  216. //
  217. // Function: RegisterInterface
  218. //
  219. // Synopsis: Add the registry entries required for an interface.
  220. //
  221. // Arguments: [pshkInterface] - handle to CLSID\Interface key
  222. // [wszInterfaceGUID] - GUID of interface to add
  223. // [wszInterfaceName] - human-readable name of interface
  224. // [wszNumMethods] - number of methods (including inherited)
  225. // [wszProxyCLSID] - GUID of dll containing proxy/stubs
  226. //
  227. // Returns: HRESULT
  228. //
  229. // History: 3-31-1997 DavidMun Created
  230. // 5-09-1997 SteveBl Modified for use with AppMgr
  231. //
  232. //---------------------------------------------------------------------------
  233. HRESULT
  234. RegisterInterface(
  235. CSafeReg *pshkInterface,
  236. LPWSTR wszInterfaceGUID,
  237. LPWSTR wszInterfaceName,
  238. LPWSTR wszNumMethods,
  239. LPWSTR wszProxyCLSID)
  240. {
  241. HRESULT hr = S_OK;
  242. CSafeReg shkIID;
  243. CSafeReg shkNumMethods;
  244. CSafeReg shkProxy;
  245. do
  246. {
  247. hr = pshkInterface->Create(wszInterfaceGUID, &shkIID);
  248. BREAK_ON_FAIL_HRESULT(hr);
  249. hr = shkIID.SetValue(NULL,
  250. REG_SZ,
  251. (CONST BYTE *) wszInterfaceName,
  252. sizeof(WCHAR) * (lstrlen(wszInterfaceName) + 1));
  253. BREAK_ON_FAIL_HRESULT(hr);
  254. hr = shkIID.Create(L"NumMethods", &shkNumMethods);
  255. BREAK_ON_FAIL_HRESULT(hr);
  256. hr = shkNumMethods.SetValue(NULL,
  257. REG_SZ,
  258. (CONST BYTE *)wszNumMethods,
  259. sizeof(WCHAR) * (lstrlen(wszNumMethods) + 1));
  260. BREAK_ON_FAIL_HRESULT(hr);
  261. hr = shkIID.Create(L"ProxyStubClsid32", &shkProxy);
  262. BREAK_ON_FAIL_HRESULT(hr);
  263. hr = shkProxy.SetValue(NULL,
  264. REG_SZ,
  265. (CONST BYTE *)wszProxyCLSID,
  266. sizeof(WCHAR) * (lstrlen(wszProxyCLSID) + 1));
  267. BREAK_ON_FAIL_HRESULT(hr);
  268. } while (0);
  269. return hr;
  270. }