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.

276 lines
7.7 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation.
  4. //
  5. // File: WLBSMain.CPP
  6. //
  7. // Module: WLBS Instance provider code
  8. //
  9. // Purpose: Contains DLL entry points. Also containts code that controls
  10. // when the DLL can be unloaded by tracking the number of
  11. // components and sever locks as well as routines that support
  12. // self registration.
  13. //
  14. // History:
  15. //
  16. //***************************************************************************
  17. #include "WLBS_Provider.h"
  18. #include <objbase.h>
  19. #include "ControlWrapper.h"
  20. #include "WLBS_DllMain.tmh" // for event tracing
  21. #define THIS_PROVIDERS_NAME L"Microsoft NLB Instance Provider"
  22. //CLSID for provider
  23. // {FB223274-D72E-11d2-A420-00C04F68FE28}
  24. static const GUID CLSID_WLBSProvider =
  25. { 0xfb223274, 0xd72e, 0x11d2, { 0xa4, 0x20, 0x0, 0xc0, 0x4f, 0x68, 0xfe, 0x28 } };
  26. static HMODULE g_hModule = NULL; //DLL Module Handle
  27. CWlbsControlWrapper* g_pWlbsControl = NULL;
  28. //Track number of objects and number of locks.
  29. long g_cComponents = 0; //Count of active components
  30. long g_cServerLocks = 0; //Count of server locks
  31. //***************************************************************************
  32. //
  33. // DllMain
  34. //
  35. // Purpose: Entry point for DLL.
  36. //
  37. // Return: TRUE if OK.
  38. //
  39. //***************************************************************************
  40. BOOL WINAPI DllMain(
  41. HINSTANCE a_hInstance, // handle to DLL module
  42. DWORD a_fdwReason, // reason for calling function
  43. LPVOID /* a_lpvReserved */ // reserved
  44. )
  45. {
  46. if (a_fdwReason == DLL_PROCESS_ATTACH) {
  47. //set the global module handle
  48. g_hModule = a_hInstance;
  49. //
  50. // Enable tracing
  51. //
  52. WPP_INIT_TRACING(L"Microsoft\\NLB");
  53. }
  54. if (a_fdwReason == DLL_PROCESS_DETACH) {
  55. if( g_pWlbsControl != NULL ) {
  56. delete g_pWlbsControl;
  57. }
  58. //
  59. // Disable tracing
  60. //
  61. WPP_CLEANUP();
  62. }
  63. return TRUE;
  64. }
  65. //***************************************************************************
  66. //
  67. // DllGetClassObject
  68. //
  69. // Purpose: Called by OLE when some client wants a class factory. Return
  70. // one only if it is the sort of class this DLL supports.
  71. //
  72. //***************************************************************************
  73. STDAPI DllGetClassObject(REFCLSID a_rclsid, REFIID a_riid, PPVOID a_ppv)
  74. {
  75. HRESULT hr;
  76. CWLBSClassFactory *pWLBSFacObj;
  77. if ( a_rclsid != CLSID_WLBSProvider )
  78. return CLASS_E_CLASSNOTAVAILABLE;
  79. pWLBSFacObj = new CWLBSClassFactory();
  80. if ( pWLBSFacObj == NULL )
  81. return E_OUTOFMEMORY;
  82. hr=pWLBSFacObj->QueryInterface( a_riid, a_ppv );
  83. pWLBSFacObj->Release();
  84. return hr;
  85. }
  86. //***************************************************************************
  87. //
  88. // DllCanUnloadNow
  89. //
  90. // Purpose: Called periodically by Ole in order to determine if the
  91. // DLL can be freed.
  92. //
  93. // Return: S_OK if there are no objects in use and the class factory
  94. // isn't locked.
  95. //
  96. //***************************************************************************
  97. STDAPI DllCanUnloadNow(void)
  98. {
  99. SCODE SCode;
  100. //It is OK to unload if there are no objects or locks on the
  101. // class factory.
  102. SCode = (g_cComponents == 0L && g_cServerLocks == 0L) ? S_OK : S_FALSE;
  103. // Do not let this provider unload implicitly. The API is maintaining a
  104. // cache that must persist in order for proper operation.
  105. // return S_FALSE;
  106. return SCode;
  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. WCHAR wcID[128];
  119. WCHAR szCLSID[128];
  120. WCHAR szModule[MAX_PATH];
  121. const WCHAR * pName = THIS_PROVIDERS_NAME;
  122. const WCHAR * pThreadingModel = L"Apartment";
  123. HKEY hKey1, hKey2;
  124. LONG nRet;
  125. // Create the path.
  126. StringFromGUID2(CLSID_WLBSProvider, wcID, 128);
  127. wcscpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  128. wcscat(szCLSID, wcID);
  129. // Create entries under CLSID
  130. nRet = RegCreateKey( HKEY_LOCAL_MACHINE, szCLSID, &hKey1 );
  131. nRet = RegSetValueEx( hKey1,
  132. NULL,
  133. 0,
  134. REG_SZ,
  135. (BYTE *)pName,
  136. (wcslen(pName)+1) * sizeof(WCHAR) );
  137. nRet = RegCreateKey( hKey1, L"InprocServer32", &hKey2 );
  138. GetModuleFileName( g_hModule, szModule, MAX_PATH );
  139. nRet = RegSetValueEx( hKey2,
  140. NULL,
  141. 0,
  142. REG_SZ,
  143. (BYTE *)szModule,
  144. (wcslen(szModule)+1) * sizeof(WCHAR) );
  145. nRet = RegSetValueEx( hKey2,
  146. L"ThreadingModel",
  147. 0,
  148. REG_SZ,
  149. (BYTE *)pThreadingModel,
  150. (wcslen(pThreadingModel)+1) * sizeof(WCHAR) );
  151. nRet = RegCloseKey(hKey1);
  152. nRet = RegCloseKey(hKey2);
  153. // 12/11/00 JosephJ
  154. // We used to compile the MOF file here. However, this has
  155. // moved to indirect compilation via an entry for wlbsprov.mof in
  156. // \nt\MergedComponents\SetupInfs\wbemoc.inx
  157. //
  158. #if 0
  159. /* shouse 6.20.00 - Adding MOF compile support to assure that the WLBS MOF gets compiled when necessary. */
  160. IMofCompiler * pMofComp = NULL;
  161. WBEM_COMPILE_STATUS_INFO Info;
  162. WCHAR szMOFPath[MAX_PATH];
  163. HRESULT hrStatus = S_OK;
  164. /* shouse 6.20.00 - Zero the memory contents. */
  165. ZeroMemory((VOID *)&Info, sizeof(WBEM_COMPILE_STATUS_INFO));
  166. ZeroMemory((VOID *)szMOFPath, sizeof(szMOFPath));
  167. /* shouse 6.20.00 - Query the system root directory. */
  168. if (!GetWindowsDirectory(szMOFPath, MAX_PATH)) {
  169. hrStatus = GetLastError();
  170. TRACE_ERROR1("GetWindowsDirectory failed: %d\n", hrStatus);
  171. return hrStatus;
  172. }
  173. /* shouse 6.20.00 - Concatenate the windows system path with the MOF path. */
  174. lstrcat(szMOFPath, L"\\system32\\wbem\\wlbsprov.mof");
  175. /* shouse 6.20.00 - Create an instance of the MOF compiler. */
  176. if ((hrStatus = CoCreateInstance(CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER, IID_IMofCompiler, (LPVOID *)&pMofComp)) != S_OK) {
  177. TRACE_ERROR1("CoCreateInstance failed: %d\n", hrStatus);
  178. return hrStatus;
  179. }
  180. /* shouse 6.20.00 - Compile the WLBS provider. */
  181. if ((hrStatus = pMofComp->CompileFile((LPWSTR)szMOFPath, NULL, NULL, NULL, NULL, 0, 0, 0, &Info)) != WBEM_S_NO_ERROR) {
  182. TRACE_ERROR2("CompileFile failed: %d:0x%x\n", hrStatus, Info.hRes);
  183. return hrStatus;
  184. }
  185. /* shouse 6.20.00 - Release the MOF compiler. */
  186. pMofComp->Release();
  187. #endif // 0
  188. return NOERROR;
  189. }
  190. //***************************************************************************
  191. //
  192. // DllUnregisterServer
  193. //
  194. // Purpose: Called when it is time to remove the registry entries.
  195. //
  196. // Return: NOERROR if registration successful, error otherwise.
  197. //***************************************************************************
  198. STDAPI DllUnregisterServer(void)
  199. {
  200. WCHAR szCLSID[128];
  201. WCHAR wcID[128];
  202. HKEY hKey;
  203. // Create the path using the CLSID
  204. StringFromGUID2(CLSID_WLBSProvider, wcID, 128);
  205. wcscpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  206. wcscat(szCLSID, wcID);
  207. // First delete the InProcServer subkey.
  208. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  209. if(dwRet == NO_ERROR)
  210. {
  211. RegDeleteKey(hKey, L"InProcServer32");
  212. RegCloseKey(hKey);
  213. }
  214. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\classes\\CLSID", &hKey);
  215. if(dwRet == NO_ERROR)
  216. {
  217. RegDeleteKey(hKey,wcID);
  218. RegCloseKey(hKey);
  219. }
  220. return NOERROR;
  221. }