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.

283 lines
7.1 KiB

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Server.cpp
  4. //
  5. // Module: WMI high performance provider sample code
  6. //
  7. // Generic COM server framework, adapted for the BasicHiPerf provider
  8. // sample. This module contains nothing specific to the BasicHiPerf
  9. // provider except what is defined in the section bracketed by the
  10. // CLSID SPECIFIC comments below.
  11. //
  12. // History:
  13. // raymcc 25-Nov-97 Created.
  14. // raymcc 18-Feb-98 Updated for NT5 Beta 2 version.
  15. // a-dcrews 12-Jan-99 Adapted for BasicHiPerf.dll
  16. //
  17. //
  18. // Copyright (c) 1997-2001 Microsoft Corporation, All rights reserved
  19. //
  20. ////////////////////////////////////////////////////////////////////////
  21. #include "precomp.h"
  22. #include <time.h>
  23. #include <initguid.h>
  24. #include <autoptr.h>
  25. /////////////////////////////////////////////////////////////////////////////
  26. //
  27. // BEGIN CLSID SPECIFIC SECTION
  28. //
  29. //
  30. #include "wbemprov.h"
  31. #include "Provider.h"
  32. #include "Factory.h"
  33. #define IMPLEMENTED_CLSID CLSID_HiPerfCooker_v1
  34. #define SERVER_REGISTRY_COMMENT L"WMI High Performance Cooker"
  35. #define CPP_CLASS_NAME CHiPerfProvider
  36. #define INTERFACE_CAST (IWbemHiPerfProvider*)
  37. // {B0A2AB46-F612-4469-BEC4-7AB038BC476C}
  38. DEFINE_GUID(IMPLEMENTED_CLSID,
  39. 0xb0a2ab46, 0xf612, 0x4469, 0xbe, 0xc4, 0x7a, 0xb0, 0x38, 0xbc, 0x47, 0x6c);
  40. //
  41. // END CLSID SPECIFIC SECTION
  42. //
  43. /////////////////////////////////////////////////////////////////////////////
  44. HINSTANCE g_hInstance;
  45. long g_lLocks = 0;
  46. long g_lObjects = 0;
  47. //***************************************************************************
  48. //
  49. // DllMain
  50. //
  51. // Dll entry point.
  52. //
  53. // PARAMETERS:
  54. //
  55. // HINSTANCE hinstDLL The handle to our DLL.
  56. // DWORD dwReason DLL_PROCESS_ATTACH on load,
  57. // DLL_PROCESS_DETACH on shutdown,
  58. // DLL_THREAD_ATTACH/DLL_THREAD_DETACH otherwise.
  59. // LPVOID lpReserved Reserved
  60. //
  61. // RETURN VALUES:
  62. //
  63. // TRUE is successful, FALSE if a fatal error occured.
  64. // NT behaves very ugly if FALSE is returned.
  65. //
  66. //***************************************************************************
  67. BOOL WINAPI DllMain(
  68. HINSTANCE hinstDLL,
  69. DWORD dwReason,
  70. LPVOID lpReserved
  71. )
  72. {
  73. if (dwReason == DLL_PROCESS_ATTACH)
  74. {
  75. g_hInstance = hinstDLL;
  76. }
  77. return TRUE;
  78. }
  79. //***************************************************************************
  80. //
  81. // DllGetClassObject
  82. //
  83. // Standard OLE In-Process Server entry point to return an class factory
  84. // instance.
  85. //
  86. // PARAMETERS:
  87. //
  88. // RETURNS:
  89. //
  90. // S_OK Success
  91. // E_NOINTERFACE An interface other that IClassFactory was asked for
  92. // E_OUTOFMEMORY
  93. // E_FAILED Initialization failed, or an unsupported clsid was
  94. // asked for.
  95. //
  96. //***************************************************************************
  97. STDAPI DllGetClassObject(
  98. REFCLSID rclsid,
  99. REFIID riid,
  100. LPVOID * ppv
  101. )
  102. {
  103. CClassFactory *pClassFactory = NULL;
  104. HRESULT hRes;
  105. // Verify the caller is asking for our type of object
  106. // ===================================================
  107. if (IMPLEMENTED_CLSID == rclsid)
  108. {
  109. // Create the class factory
  110. // ========================
  111. pClassFactory = new CClassFactory;
  112. if (!pClassFactory)
  113. return E_OUTOFMEMORY;
  114. hRes = pClassFactory->QueryInterface(riid, ppv);
  115. if (FAILED(hRes))
  116. {
  117. delete pClassFactory;
  118. return hRes;
  119. }
  120. hRes = S_OK;
  121. }
  122. else
  123. hRes = CLASS_E_CLASSNOTAVAILABLE;
  124. return hRes;
  125. }
  126. //***************************************************************************
  127. //
  128. // DllCanUnloadNow
  129. //
  130. // Standard OLE entry point for server shutdown request. Allows shutdown
  131. // only if no outstanding objects or locks are present.
  132. //
  133. // RETURN VALUES:
  134. //
  135. // S_OK May unload now.
  136. // S_FALSE May not.
  137. //
  138. //***************************************************************************
  139. STDAPI DllCanUnloadNow(void)
  140. {
  141. HRESULT hRes = S_FALSE;
  142. if (0 == g_lLocks && 0 == g_lObjects)
  143. hRes = S_OK;
  144. return hRes;
  145. }
  146. //***************************************************************************
  147. //
  148. // DllRegisterServer
  149. //
  150. // Standard OLE entry point for registering the server.
  151. //
  152. // RETURN VALUES:
  153. //
  154. // S_OK Registration was successful
  155. // E_FAIL Registration failed.
  156. //
  157. //***************************************************************************
  158. STDAPI DllRegisterServer(void)
  159. {
  160. WCHAR * Path = new WCHAR[1024];
  161. wmilib::auto_buffer<WCHAR> rm1_(Path);
  162. WCHAR * pGuidStr = 0;
  163. WCHAR * KeyPath = new WCHAR[1024];
  164. wmilib::auto_buffer<WCHAR> rm2_(KeyPath);
  165. if (0 == Path || 0 == KeyPath)
  166. {
  167. return E_OUTOFMEMORY;
  168. }
  169. // Get the dll's filename
  170. // ======================
  171. GetModuleFileNameW(g_hInstance, Path, 1024);
  172. // Convert CLSID to string.
  173. // ========================
  174. StringFromCLSID(IMPLEMENTED_CLSID, &pGuidStr);
  175. swprintf(KeyPath, L"Software\\Classes\\CLSID\\\\%s", pGuidStr);
  176. // Place it in registry.
  177. // CLSID\\CLSID_Nt5PerProvider_v1 : <no_name> : "name"
  178. // \\CLSID_Nt5PerProvider_v1\\InProcServer32 : <no_name> : "path to DLL"
  179. // : ThreadingModel : "both"
  180. // ==============================================================
  181. HKEY hKey;
  182. LONG lRes = RegCreateKeyW(HKEY_LOCAL_MACHINE, KeyPath, &hKey);
  183. if (lRes)
  184. return E_FAIL;
  185. wchar_t *pName = SERVER_REGISTRY_COMMENT;
  186. RegSetValueExW(hKey, 0, 0, REG_SZ, (const BYTE *) pName, wcslen(pName) * 2 + 2);
  187. HKEY hSubkey;
  188. lRes = RegCreateKey(hKey, _TEXT("InprocServer32"), &hSubkey);
  189. RegSetValueExW(hSubkey, 0, 0, REG_SZ, (const BYTE *) Path, wcslen(Path) * 2 + 2);
  190. RegSetValueExW(hSubkey, L"ThreadingModel", 0, REG_SZ, (const BYTE *) L"Both", wcslen(L"Both") * 2 + 2);
  191. RegCloseKey(hSubkey);
  192. RegCloseKey(hKey);
  193. CoTaskMemFree(pGuidStr);
  194. return S_OK;
  195. }
  196. //***************************************************************************
  197. //
  198. // DllUnregisterServer
  199. //
  200. // Standard OLE entry point for unregistering the server.
  201. //
  202. // RETURN VALUES:
  203. //
  204. // S_OK Unregistration was successful
  205. // E_FAIL Unregistration failed.
  206. //
  207. //***************************************************************************
  208. STDAPI DllUnregisterServer(void)
  209. {
  210. wchar_t *pGuidStr = 0;
  211. HKEY hKey;
  212. wchar_t KeyPath[256];
  213. StringFromCLSID(IMPLEMENTED_CLSID, &pGuidStr);
  214. swprintf(KeyPath, L"Software\\Classes\\CLSID\\\\%s", pGuidStr);
  215. // Delete InProcServer32 subkey.
  216. // =============================
  217. LONG lRes = RegOpenKeyW(HKEY_LOCAL_MACHINE, KeyPath, &hKey);
  218. if (lRes)
  219. return E_FAIL;
  220. RegDeleteKeyW(hKey, L"InprocServer32");
  221. RegCloseKey(hKey);
  222. // Delete CLSID GUID key.
  223. // ======================
  224. lRes = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Classes\\CLSID", &hKey);
  225. if (lRes)
  226. return E_FAIL;
  227. RegDeleteKeyW(hKey, pGuidStr);
  228. RegCloseKey(hKey);
  229. CoTaskMemFree(pGuidStr);
  230. return S_OK;
  231. }