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.

214 lines
5.7 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WINMGMT class provider sample code
  6. //
  7. // Purpose: Contains DLL entry points. Also has code that controls
  8. // when the DLL can be unloaded by tracking the number of
  9. // objects and locks as well as routines that support
  10. // self registration.
  11. //
  12. // Copyright (c) 2000 Microsoft Corporation
  13. //
  14. //***************************************************************************
  15. #include <objbase.h>
  16. #include <wbemprov.h>
  17. #include <initguid.h>
  18. #include "debug.h"
  19. #include "wbemmisc.h"
  20. #include "useful.h"
  21. #include "testinfo.h"
  22. #include "sample.h"
  23. HMODULE ghModule;
  24. HANDLE CSMutex;
  25. // {8DD99E84-2B01-4c97-8061-2A3D08E289BB}
  26. DEFINE_GUID(CLSID_classprovider,
  27. 0x8dd99e84, 0x2b01, 0x4c97, 0x80, 0x61, 0x2a, 0x3d, 0x8, 0xe2, 0x89, 0xbb);
  28. //Count number of objects and number of locks.
  29. long g_cObj=0;
  30. long g_cLock=0;
  31. //***************************************************************************
  32. //
  33. // LibMain32
  34. //
  35. // Purpose: Entry point for DLL.
  36. //
  37. // Return: TRUE if OK.
  38. //
  39. //***************************************************************************
  40. extern "C" BOOL WINAPI LibMain32(HINSTANCE hInstance, ULONG ulReason
  41. , LPVOID pvReserved)
  42. {
  43. if (DLL_PROCESS_ATTACH==ulReason)
  44. {
  45. ghModule = hInstance;
  46. CSMutex = CreateMutex(NULL,
  47. FALSE,
  48. NULL);
  49. if (CSMutex == NULL)
  50. {
  51. return(FALSE);
  52. }
  53. }
  54. return(TRUE);
  55. }
  56. //***************************************************************************
  57. //
  58. // DllGetClassObject
  59. //
  60. // Purpose: Called by Ole when some client wants a class factory. Return
  61. // one only if it is the sort of class this DLL supports.
  62. //
  63. //***************************************************************************
  64. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  65. {
  66. HRESULT hr;
  67. CProvFactory *pObj;
  68. if (CLSID_classprovider!=rclsid)
  69. return E_FAIL;
  70. pObj=new CProvFactory();
  71. if (NULL==pObj)
  72. return E_OUTOFMEMORY;
  73. hr=pObj->QueryInterface(riid, ppv);
  74. if (FAILED(hr))
  75. delete pObj;
  76. return hr;
  77. }
  78. //***************************************************************************
  79. //
  80. // DllCanUnloadNow
  81. //
  82. // Purpose: Called periodically by Ole in order to determine if the
  83. // DLL can be freed.
  84. //
  85. // Return: S_OK if there are no objects in use and the class factory
  86. // isn't locked.
  87. //
  88. //***************************************************************************
  89. STDAPI DllCanUnloadNow(void)
  90. {
  91. SCODE sc;
  92. //It is OK to unload if there are no objects or locks on the
  93. // class factory.
  94. #if 0
  95. sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  96. return sc;
  97. #else
  98. //
  99. // We always remain loaded since we carry around result objects for
  100. // the results of previously run tests and do not want to unload
  101. // when there are result objects still held. In the future we will
  102. // want to check if all results are cleared and if so then unload,
  103. // but for now we'll stay loaded forever.
  104. //
  105. return(S_FALSE);
  106. #endif
  107. }
  108. //***************************************************************************
  109. //
  110. // DllRegisterServer
  111. //
  112. // Purpose: Called during setup or by regsvr32.
  113. //
  114. // Return: NOERROR if registration successful, error otherwise.
  115. //***************************************************************************
  116. STDAPI DllRegisterServer(void)
  117. {
  118. char szID[128];
  119. WCHAR wcID[128];
  120. char szCLSID[128];
  121. char szModule[MAX_PATH];
  122. char * pName = "WDM to CIM Mapping Provider";
  123. char * pModel = "Both";
  124. HKEY hKey1, hKey2;
  125. // Create the path.
  126. StringFromGUID2(CLSID_classprovider, wcID, 128);
  127. wcstombs(szID, wcID, 128);
  128. lstrcpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  129. lstrcat(szCLSID, szID);
  130. // Create entries under CLSID
  131. RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  132. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, lstrlen(pName)+1);
  133. RegCreateKey(hKey1,"InprocServer32",&hKey2);
  134. GetModuleFileName(ghModule, szModule, MAX_PATH);
  135. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  136. lstrlen(szModule)+1);
  137. RegSetValueEx(hKey2, "ThreadingModel", 0, REG_SZ,
  138. (BYTE *)pModel, lstrlen(pModel)+1);
  139. RegCloseKey(hKey1);
  140. RegCloseKey(hKey2);
  141. return NOERROR;
  142. }
  143. //***************************************************************************
  144. //
  145. // DllUnregisterServer
  146. //
  147. // Purpose: Called when it is time to remove the registry entries.
  148. //
  149. // Return: NOERROR if registration successful, error otherwise.
  150. //***************************************************************************
  151. STDAPI DllUnregisterServer(void)
  152. {
  153. char szID[128];
  154. WCHAR wcID[128];
  155. char szCLSID[128];
  156. HKEY hKey;
  157. // Create the path using the CLSID
  158. StringFromGUID2(CLSID_classprovider, wcID, 128);
  159. wcstombs(szID, wcID, 128);
  160. lstrcpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  161. lstrcat(szCLSID, szID);
  162. // First delete the InProcServer subkey.
  163. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  164. if(dwRet == NO_ERROR)
  165. {
  166. RegDeleteKey(hKey, "InProcServer32");
  167. RegCloseKey(hKey);
  168. }
  169. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\classes\\CLSID", &hKey);
  170. if(dwRet == NO_ERROR)
  171. {
  172. RegDeleteKey(hKey,szID);
  173. RegCloseKey(hKey);
  174. }
  175. return NOERROR;
  176. }