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.

310 lines
9.0 KiB

  1. // This is a part of the Microsoft Management Console.
  2. // Copyright (C) 1995-1996 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Management Console and related
  7. // electronic documentation provided with the interfaces.
  8. // You will need the NT SUR Beta 2 SDK or VC 4.2 in order to build this
  9. // project. This is because you will need MIDL 3.00.15 or higher and new
  10. // headers and libs. If you have VC 4.2 installed, then everything should
  11. // already be configured correctly.
  12. // Note: Proxy/Stub Information
  13. // To build a separate proxy/stub DLL,
  14. // run nmake -f Snapinps.mak in the project directory.
  15. #include "precomp.hxx"
  16. #include "safereg.hxx"
  17. #include "amsp.h"
  18. #define BREAK_ON_FAIL_HRESULT(hr) if (FAILED(hr)) break
  19. #define PSBUFFER_STR L"AppManagementBuffer"
  20. #define THREADING_STR L"Apartment"
  21. HRESULT
  22. RegisterInterface(
  23. CSafeReg *pshkInterface,
  24. LPWSTR wszInterfaceGUID,
  25. LPWSTR wszInterfaceName,
  26. LPWSTR wszNumMethods,
  27. LPWSTR wszProxyCLSID);
  28. CComModule _Module;
  29. BEGIN_OBJECT_MAP(ObjectMap)
  30. OBJECT_ENTRY(CLSID_Snapin, CComponentDataImpl)
  31. END_OBJECT_MAP()
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37. class CSnapinApp : public CWinApp
  38. {
  39. public:
  40. virtual BOOL InitInstance();
  41. virtual int ExitInstance();
  42. };
  43. CSnapinApp theApp;
  44. HINSTANCE ghInstance;
  45. BOOL CSnapinApp::InitInstance()
  46. {
  47. ghInstance = m_hInstance;
  48. _Module.Init(ObjectMap, m_hInstance);
  49. return CWinApp::InitInstance();
  50. }
  51. int CSnapinApp::ExitInstance()
  52. {
  53. _Module.Term();
  54. DEBUG_VERIFY_INSTANCE_COUNT(CSnapin);
  55. DEBUG_VERIFY_INSTANCE_COUNT(CComponentDataImpl);
  56. return CWinApp::ExitInstance();
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. // Used to determine whether the DLL can be unloaded by OLE
  60. STDAPI DllCanUnloadNow(void)
  61. {
  62. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  63. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. // Returns a class factory to create an object of the requested type
  67. STDAPI amspDllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
  68. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  69. {
  70. if (IsEqualCLSID(rclsid, CLSID_Snapin))
  71. {
  72. return _Module.GetClassObject(rclsid, riid, ppv);
  73. }
  74. return amspDllGetClassObject(rclsid, riid, ppv);
  75. }
  76. const wchar_t * szGPT_Snapin = L"{2C8C9b20-96AD-11d0-8C54-121767000000}";
  77. const wchar_t * szGPT_Namespace = L"{A6B4EEBC-B681-11d0-9484-080036B11A03}";
  78. const wchar_t * szAppName = L"Application Manager";
  79. /////////////////////////////////////////////////////////////////////////////
  80. // DllRegisterServer - Adds entries to the system registry
  81. STDAPI DllRegisterServer(void)
  82. {
  83. CSafeReg shk;
  84. CSafeReg shkCLSID;
  85. CSafeReg shkServer;
  86. CSafeReg shkTemp;
  87. HRESULT hr = S_OK;
  88. do
  89. {
  90. hr = _Module.RegisterServer(FALSE);
  91. BREAK_ON_FAIL_HRESULT(hr);
  92. // register extension
  93. /*
  94. extern const CLSID CLSID_Snapin;
  95. extern const wchar_t * szCLSID_Snapin;
  96. extern const GUID cNodeType;
  97. extern const wchar_t* cszNodeType;
  98. */
  99. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\SnapIns", KEY_WRITE);
  100. BREAK_ON_FAIL_HRESULT(hr);
  101. hr = shkCLSID.Create(szGPT_Snapin, &shk);
  102. BREAK_ON_FAIL_HRESULT(hr);
  103. hr = shk.Create(L"RequiredExtensions", &shkServer);
  104. BREAK_ON_FAIL_HRESULT(hr);
  105. hr = shkServer.Create(szCLSID_Snapin, &shkTemp);
  106. BREAK_ON_FAIL_HRESULT(hr);
  107. shkTemp.Close();
  108. shkServer.Close();
  109. shk.Close();
  110. hr = shkCLSID.Create(szCLSID_Snapin, &shk);
  111. BREAK_ON_FAIL_HRESULT(hr);
  112. hr = shk.SetValue(L"NameString",
  113. REG_SZ,
  114. (CONST BYTE *) szAppName,
  115. sizeof(WCHAR) * (lstrlen(szAppName)+ 1));
  116. hr = shk.Create(L"NodeTypes", &shkTemp);
  117. BREAK_ON_FAIL_HRESULT(hr);
  118. hr = shkTemp.Create(szGPT_Namespace, &shkServer);
  119. BREAK_ON_FAIL_HRESULT(hr);
  120. shkTemp.Close();
  121. shkServer.Close();
  122. shk.Close();
  123. shkCLSID.Close();
  124. hr = shkCLSID.Open(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\MMC\\NodeTypes", KEY_WRITE);
  125. BREAK_ON_FAIL_HRESULT(hr);
  126. hr = shkCLSID.Create(szGPT_Namespace, &shk);
  127. BREAK_ON_FAIL_HRESULT(hr);
  128. hr = shk.Create(L"Extensions", &shkServer);
  129. BREAK_ON_FAIL_HRESULT(hr);
  130. hr = shkServer.Create(L"NameSpace", &shkTemp);
  131. BREAK_ON_FAIL_HRESULT(hr);
  132. hr = shkTemp.SetValue(szCLSID_Snapin,
  133. REG_SZ,
  134. (CONST BYTE *) szAppName,
  135. sizeof(WCHAR) * (lstrlen(szAppName)+ 1));
  136. shkTemp.Close();
  137. shkServer.Close();
  138. shk.Close();
  139. shkCLSID.Close();
  140. hr = shkCLSID.Open(HKEY_CLASSES_ROOT, L"CLSID", KEY_WRITE);
  141. BREAK_ON_FAIL_HRESULT(hr);
  142. //
  143. // Create the CLSID entry for the private interface proxy/stub
  144. // dll (which just points back to this dll).
  145. //
  146. WCHAR wszModuleFilename[MAX_PATH];
  147. LONG lr = GetModuleFileName(ghInstance, wszModuleFilename, MAX_PATH);
  148. if (!lr)
  149. {
  150. break;
  151. }
  152. hr = shkCLSID.Create(GUID_IAPPMANAGERACTIONS_STR, &shk);
  153. BREAK_ON_FAIL_HRESULT(hr);
  154. hr = shk.SetValue(NULL,
  155. REG_SZ,
  156. (CONST BYTE *) PSBUFFER_STR,
  157. sizeof(PSBUFFER_STR));
  158. BREAK_ON_FAIL_HRESULT(hr);
  159. hr = shk.Create(L"InprocServer32", &shkServer);
  160. BREAK_ON_FAIL_HRESULT(hr);
  161. hr = shkServer.SetValue(NULL,
  162. REG_SZ,
  163. (CONST BYTE *) wszModuleFilename,
  164. sizeof(WCHAR) * (lstrlen(wszModuleFilename) + 1));
  165. BREAK_ON_FAIL_HRESULT(hr);
  166. hr = shkServer.SetValue(L"ThreadingModel",
  167. REG_SZ,
  168. (CONST BYTE *) THREADING_STR,
  169. sizeof(THREADING_STR));
  170. BREAK_ON_FAIL_HRESULT(hr);
  171. shk.Close();
  172. hr = shk.Open(HKEY_CLASSES_ROOT, L"Interface", KEY_WRITE);
  173. BREAK_ON_FAIL_HRESULT(hr);
  174. hr = RegisterInterface(&shk,
  175. GUID_IAPPMANAGERACTIONS_STR,
  176. IAPPMANAGERACTIONS_STR,
  177. NUM_IAPPMANAGERACTIONS_METHODS,
  178. GUID_IAPPMANAGERACTIONS_STR);
  179. } while (0);
  180. return hr;
  181. }
  182. /////////////////////////////////////////////////////////////////////////////
  183. // DllUnregisterServer - Removes entries from the system registry
  184. STDAPI DllUnregisterServer(void)
  185. {
  186. _Module.UnregisterServer();
  187. return S_OK;
  188. }
  189. //+--------------------------------------------------------------------------
  190. //
  191. // Function: RegisterInterface
  192. //
  193. // Synopsis: Add the registry entries required for an interface.
  194. //
  195. // Arguments: [pshkInterface] - handle to CLSID\Interface key
  196. // [wszInterfaceGUID] - GUID of interface to add
  197. // [wszInterfaceName] - human-readable name of interface
  198. // [wszNumMethods] - number of methods (including inherited)
  199. // [wszProxyCLSID] - GUID of dll containing proxy/stubs
  200. //
  201. // Returns: HRESULT
  202. //
  203. // History: 3-31-1997 DavidMun Created
  204. // 5-09-1997 SteveBl Modified for use with AppMgr
  205. //
  206. //---------------------------------------------------------------------------
  207. HRESULT
  208. RegisterInterface(
  209. CSafeReg *pshkInterface,
  210. LPWSTR wszInterfaceGUID,
  211. LPWSTR wszInterfaceName,
  212. LPWSTR wszNumMethods,
  213. LPWSTR wszProxyCLSID)
  214. {
  215. HRESULT hr = S_OK;
  216. CSafeReg shkIID;
  217. CSafeReg shkNumMethods;
  218. CSafeReg shkProxy;
  219. do
  220. {
  221. hr = pshkInterface->Create(wszInterfaceGUID, &shkIID);
  222. BREAK_ON_FAIL_HRESULT(hr);
  223. hr = shkIID.SetValue(NULL,
  224. REG_SZ,
  225. (CONST BYTE *) wszInterfaceName,
  226. sizeof(WCHAR) * (lstrlen(wszInterfaceName) + 1));
  227. BREAK_ON_FAIL_HRESULT(hr);
  228. hr = shkIID.Create(L"NumMethods", &shkNumMethods);
  229. BREAK_ON_FAIL_HRESULT(hr);
  230. hr = shkNumMethods.SetValue(NULL,
  231. REG_SZ,
  232. (CONST BYTE *)wszNumMethods,
  233. sizeof(WCHAR) * (lstrlen(wszNumMethods) + 1));
  234. BREAK_ON_FAIL_HRESULT(hr);
  235. hr = shkIID.Create(L"ProxyStubClsid32", &shkProxy);
  236. BREAK_ON_FAIL_HRESULT(hr);
  237. hr = shkProxy.SetValue(NULL,
  238. REG_SZ,
  239. (CONST BYTE *)wszProxyCLSID,
  240. sizeof(WCHAR) * (lstrlen(wszProxyCLSID) + 1));
  241. BREAK_ON_FAIL_HRESULT(hr);
  242. } while (0);
  243. return hr;
  244. }