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.

210 lines
5.6 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 <initguid.h>
  17. #include "sample.h"
  18. #include "wbemmisc.h"
  19. HMODULE ghModule;
  20. HANDLE CdmMutex;
  21. // {AC42F9A6-9945-426f-9199-86F7257365D4}
  22. DEFINE_GUID(CLSID_classprovider,
  23. 0xac42f9a6, 0x9945, 0x426f, 0x91, 0x99, 0x86, 0xf7, 0x25, 0x73, 0x65, 0xd4);
  24. //Count number of objects and number of locks.
  25. long g_cObj=0;
  26. long g_cLock=0;
  27. //***************************************************************************
  28. //
  29. // LibMain32
  30. //
  31. // Purpose: Entry point for DLL.
  32. //
  33. // Return: TRUE if OK.
  34. //
  35. //***************************************************************************
  36. extern "C" BOOL WINAPI LibMain32(HINSTANCE hInstance, ULONG ulReason
  37. , LPVOID pvReserved)
  38. {
  39. if (DLL_PROCESS_ATTACH==ulReason)
  40. {
  41. ghModule = hInstance;
  42. CdmMutex = CreateMutex(NULL,
  43. FALSE,
  44. NULL);
  45. if (CdmMutex == NULL)
  46. {
  47. return(FALSE);
  48. }
  49. }
  50. return(TRUE);
  51. }
  52. //***************************************************************************
  53. //
  54. // DllGetClassObject
  55. //
  56. // Purpose: Called by Ole when some client wants a class factory. Return
  57. // one only if it is the sort of class this DLL supports.
  58. //
  59. //***************************************************************************
  60. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  61. {
  62. HRESULT hr;
  63. CProvFactory *pObj;
  64. if (CLSID_classprovider!=rclsid)
  65. return E_FAIL;
  66. pObj=new CProvFactory();
  67. if (NULL==pObj)
  68. return E_OUTOFMEMORY;
  69. hr=pObj->QueryInterface(riid, ppv);
  70. if (FAILED(hr))
  71. delete pObj;
  72. return hr;
  73. }
  74. //***************************************************************************
  75. //
  76. // DllCanUnloadNow
  77. //
  78. // Purpose: Called periodically by Ole in order to determine if the
  79. // DLL can be freed.
  80. //
  81. // Return: S_OK if there are no objects in use and the class factory
  82. // isn't locked.
  83. //
  84. //***************************************************************************
  85. STDAPI DllCanUnloadNow(void)
  86. {
  87. SCODE sc;
  88. //It is OK to unload if there are no objects or locks on the
  89. // class factory.
  90. #if 0
  91. sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  92. return sc;
  93. #else
  94. //
  95. // We always remain loaded since we carry around result objects for
  96. // the results of previously run tests and do not want to unload
  97. // when there are result objects still held. In the future we will
  98. // want to check if all results are cleared and if so then unload,
  99. // but for now we'll stay loaded forever.
  100. //
  101. return(S_FALSE);
  102. #endif
  103. }
  104. //***************************************************************************
  105. //
  106. // DllRegisterServer
  107. //
  108. // Purpose: Called during setup or by regsvr32.
  109. //
  110. // Return: NOERROR if registration successful, error otherwise.
  111. //***************************************************************************
  112. STDAPI DllRegisterServer(void)
  113. {
  114. char szID[128];
  115. WCHAR wcID[128];
  116. char szCLSID[128];
  117. char szModule[MAX_PATH];
  118. char * pName = "CDMPROV Test Class Provider";
  119. char * pModel = "Both";
  120. HKEY hKey1, hKey2;
  121. // Create the path.
  122. StringFromGUID2(CLSID_classprovider, wcID, 128);
  123. wcstombs(szID, wcID, 128);
  124. lstrcpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  125. lstrcat(szCLSID, szID);
  126. // Create entries under CLSID
  127. RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  128. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, lstrlen(pName)+1);
  129. RegCreateKey(hKey1,"InprocServer32",&hKey2);
  130. GetModuleFileName(ghModule, szModule, MAX_PATH);
  131. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  132. lstrlen(szModule)+1);
  133. RegSetValueEx(hKey2, "ThreadingModel", 0, REG_SZ,
  134. (BYTE *)pModel, lstrlen(pModel)+1);
  135. RegCloseKey(hKey1);
  136. RegCloseKey(hKey2);
  137. return NOERROR;
  138. }
  139. //***************************************************************************
  140. //
  141. // DllUnregisterServer
  142. //
  143. // Purpose: Called when it is time to remove the registry entries.
  144. //
  145. // Return: NOERROR if registration successful, error otherwise.
  146. //***************************************************************************
  147. STDAPI DllUnregisterServer(void)
  148. {
  149. char szID[128];
  150. WCHAR wcID[128];
  151. char szCLSID[128];
  152. HKEY hKey;
  153. // Create the path using the CLSID
  154. StringFromGUID2(CLSID_classprovider, wcID, 128);
  155. wcstombs(szID, wcID, 128);
  156. lstrcpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  157. lstrcat(szCLSID, szID);
  158. // First delete the InProcServer subkey.
  159. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  160. if(dwRet == NO_ERROR)
  161. {
  162. RegDeleteKey(hKey, "InProcServer32");
  163. RegCloseKey(hKey);
  164. }
  165. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\classes\\CLSID", &hKey);
  166. if(dwRet == NO_ERROR)
  167. {
  168. RegDeleteKey(hKey,szID);
  169. RegCloseKey(hKey);
  170. }
  171. return NOERROR;
  172. }