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.

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