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.

235 lines
5.9 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WBEM Method 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)1998 Microsoft Corporation, All Rights Reserved
  13. //
  14. //***************************************************************************
  15. #include <objbase.h>
  16. #include <initguid.h>
  17. #include "methprov.h"
  18. HMODULE ghModule;
  19. DEFINE_GUID(CLSID_useridprovider,0x44BB1D18, 0x0FD7, 0x11d3, 0xB3, 0x66, 0x0, 0x10, 0x5a, 0x1f, 0x47, 0x3a);
  20. //Count number of objects and number of locks.
  21. long g_cObj=0;
  22. long g_cLock=0;
  23. //***************************************************************************
  24. //
  25. // LibMain32
  26. //
  27. // Purpose: Entry point for DLL.
  28. //
  29. // Return: TRUE if OK.
  30. //
  31. //***************************************************************************
  32. BOOL WINAPI DllMain (
  33. IN HINSTANCE hInstance,
  34. IN ULONG ulReason,
  35. LPVOID pvReserved
  36. )
  37. {
  38. switch (ulReason)
  39. {
  40. case DLL_PROCESS_DETACH:
  41. return TRUE;
  42. case DLL_THREAD_DETACH:
  43. return TRUE;
  44. case DLL_PROCESS_ATTACH:
  45. {
  46. if(ghModule == NULL)
  47. ghModule = hInstance;
  48. }
  49. return TRUE;
  50. case DLL_THREAD_ATTACH:
  51. {
  52. }
  53. return TRUE;
  54. }
  55. return TRUE;
  56. }
  57. //***************************************************************************
  58. //
  59. // DllGetClassObject
  60. //
  61. // Purpose: Called by Ole when some client wants a class factory. Return
  62. // one only if it is the sort of class this DLL supports.
  63. //
  64. //***************************************************************************
  65. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  66. {
  67. HRESULT hr;
  68. CProvFactory *pObj;
  69. if (CLSID_useridprovider!=rclsid)
  70. return E_FAIL;
  71. pObj=new CProvFactory();
  72. if (NULL==pObj)
  73. return E_OUTOFMEMORY;
  74. hr=pObj->QueryInterface(riid, ppv);
  75. if (FAILED(hr))
  76. delete pObj;
  77. return hr;
  78. }
  79. //***************************************************************************
  80. //
  81. // DllCanUnloadNow
  82. //
  83. // Purpose: Called periodically by Ole in order to determine if the
  84. // DLL can be freed.
  85. //
  86. // Return: S_OK if there are no objects in use and the class factory
  87. // isn't locked.
  88. //
  89. //***************************************************************************
  90. STDAPI DllCanUnloadNow(void)
  91. {
  92. SCODE sc;
  93. //It is OK to unload if there are no objects or locks on the
  94. // class factory.
  95. sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  96. return sc;
  97. }
  98. //***************************************************************************
  99. //
  100. // Is4OrMore
  101. //
  102. // Returns true if win95 or any version of NT > 3.51
  103. //
  104. //***************************************************************************
  105. BOOL Is4OrMore(void)
  106. {
  107. OSVERSIONINFO os;
  108. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  109. if(!GetVersionEx(&os))
  110. return FALSE; // should never happen
  111. return os.dwMajorVersion >= 4;
  112. }
  113. //***************************************************************************
  114. //
  115. // DllRegisterServer
  116. //
  117. // Purpose: Called during setup or by regsvr32.
  118. //
  119. // Return: NOERROR if registration successful, error otherwise.
  120. //***************************************************************************
  121. STDAPI DllRegisterServer(void)
  122. {
  123. char szID[128];
  124. WCHAR wcID[128];
  125. char szCLSID[128];
  126. char szModule[MAX_PATH];
  127. char * pName = "WBEM Method Provider Test";
  128. char * pModel;
  129. HKEY hKey1, hKey2;
  130. // Normally we want to use "Both" as the threading model since
  131. // the DLL is free threaded, but NT 3.51 Ole doesnt work unless
  132. // the model is "Aparment"
  133. if(Is4OrMore())
  134. pModel = "Both";
  135. else
  136. pModel = "Apartment";
  137. // Create the path.
  138. StringFromGUID2(CLSID_useridprovider, wcID, 128);
  139. wcstombs(szID, wcID, 128);
  140. lstrcpy(szCLSID, TEXT("CLSID\\"));
  141. lstrcat(szCLSID, szID);
  142. // Create entries under CLSID
  143. RegCreateKey(HKEY_CLASSES_ROOT, szCLSID, &hKey1);
  144. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, lstrlen(pName)+1);
  145. RegCreateKey(hKey1,"InprocServer32",&hKey2);
  146. GetModuleFileName(ghModule, szModule, MAX_PATH);
  147. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  148. lstrlen(szModule)+1);
  149. RegSetValueEx(hKey2, "ThreadingModel", 0, REG_SZ,
  150. (BYTE *)pModel, lstrlen(pModel)+1);
  151. CloseHandle(hKey1);
  152. CloseHandle(hKey2);
  153. return NOERROR;
  154. }
  155. //***************************************************************************
  156. //
  157. // DllUnregisterServer
  158. //
  159. // Purpose: Called when it is time to remove the registry entries.
  160. //
  161. // Return: NOERROR if registration successful, error otherwise.
  162. //***************************************************************************
  163. STDAPI DllUnregisterServer(void)
  164. {
  165. char szID[128];
  166. WCHAR wcID[128];
  167. char szCLSID[128];
  168. HKEY hKey;
  169. // Create the path using the CLSID
  170. StringFromGUID2(CLSID_useridprovider, wcID, 128);
  171. wcstombs(szID, wcID, 128);
  172. lstrcpy(szCLSID, TEXT("CLSID\\"));
  173. lstrcat(szCLSID, szID);
  174. // First delete the InProcServer subkey.
  175. DWORD dwRet = RegOpenKey(HKEY_CLASSES_ROOT, szCLSID, &hKey);
  176. if(dwRet == NO_ERROR)
  177. {
  178. RegDeleteKey(hKey, "InProcServer32");
  179. CloseHandle(hKey);
  180. }
  181. dwRet = RegOpenKey(HKEY_CLASSES_ROOT, "CLSID", &hKey);
  182. if(dwRet == NO_ERROR)
  183. {
  184. RegDeleteKey(hKey,szID);
  185. CloseHandle(hKey);
  186. }
  187. return NOERROR;
  188. }