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.

238 lines
6.5 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. #include <strsafe.h>
  22. #define THIS_PROVIDERS_NAME L"Microsoft NLB Instance Provider"
  23. //CLSID for provider
  24. // {FB223274-D72E-11d2-A420-00C04F68FE28}
  25. static const GUID CLSID_WLBSProvider =
  26. { 0xfb223274, 0xd72e, 0x11d2, { 0xa4, 0x20, 0x0, 0xc0, 0x4f, 0x68, 0xfe, 0x28 } };
  27. static HMODULE g_hModule = NULL; //DLL Module Handle
  28. CWlbsControlWrapper* g_pWlbsControl = NULL;
  29. //Track number of objects and number of locks.
  30. long g_cComponents = 0; //Count of active components
  31. long g_cServerLocks = 0; //Count of server locks
  32. //***************************************************************************
  33. //
  34. // DllMain
  35. //
  36. // Purpose: Entry point for DLL.
  37. //
  38. // Return: TRUE if OK.
  39. //
  40. //***************************************************************************
  41. BOOL WINAPI DllMain(
  42. HINSTANCE a_hInstance, // handle to DLL module
  43. DWORD a_fdwReason, // reason for calling function
  44. LPVOID /* a_lpvReserved */ // reserved
  45. )
  46. {
  47. if (a_fdwReason == DLL_PROCESS_ATTACH) {
  48. //set the global module handle
  49. g_hModule = a_hInstance;
  50. //
  51. // Enable tracing
  52. //
  53. WPP_INIT_TRACING(L"Microsoft\\NLB");
  54. }
  55. if (a_fdwReason == DLL_PROCESS_DETACH) {
  56. if( g_pWlbsControl != NULL ) {
  57. delete g_pWlbsControl;
  58. }
  59. //
  60. // Disable tracing
  61. //
  62. WPP_CLEANUP();
  63. }
  64. return TRUE;
  65. }
  66. //***************************************************************************
  67. //
  68. // DllGetClassObject
  69. //
  70. // Purpose: Called by OLE when some client wants a class factory. Return
  71. // one only if it is the sort of class this DLL supports.
  72. //
  73. //***************************************************************************
  74. STDAPI DllGetClassObject(REFCLSID a_rclsid, REFIID a_riid, PPVOID a_ppv)
  75. {
  76. HRESULT hr;
  77. CWLBSClassFactory *pWLBSFacObj;
  78. if ( a_rclsid != CLSID_WLBSProvider )
  79. return CLASS_E_CLASSNOTAVAILABLE;
  80. pWLBSFacObj = new CWLBSClassFactory();
  81. if ( pWLBSFacObj == NULL )
  82. return E_OUTOFMEMORY;
  83. hr=pWLBSFacObj->QueryInterface( a_riid, a_ppv );
  84. pWLBSFacObj->Release();
  85. return hr;
  86. }
  87. //***************************************************************************
  88. //
  89. // DllCanUnloadNow
  90. //
  91. // Purpose: Called periodically by Ole in order to determine if the
  92. // DLL can be freed.
  93. //
  94. // Return: S_OK if there are no objects in use and the class factory
  95. // isn't locked.
  96. //
  97. //***************************************************************************
  98. STDAPI DllCanUnloadNow(void)
  99. {
  100. SCODE SCode;
  101. //It is OK to unload if there are no objects or locks on the
  102. // class factory.
  103. SCode = (g_cComponents == 0L && g_cServerLocks == 0L) ? S_OK : S_FALSE;
  104. // Do not let this provider unload implicitly. The API is maintaining a
  105. // cache that must persist in order for proper operation.
  106. // return S_FALSE;
  107. return SCode;
  108. }
  109. //***************************************************************************
  110. //
  111. // DllRegisterServer
  112. //
  113. // Purpose: Called during setup or by regsvr32.
  114. //
  115. // Return: NOERROR if registration successful, error otherwise.
  116. //***************************************************************************
  117. STDAPI DllRegisterServer(void)
  118. {
  119. WCHAR wcID[128];
  120. WCHAR szCLSID[128];
  121. WCHAR szModule[MAX_PATH];
  122. const WCHAR * pName = THIS_PROVIDERS_NAME;
  123. const WCHAR * pThreadingModel = L"Apartment";
  124. HKEY hKey1, hKey2;
  125. LONG nRet;
  126. // Create the path.
  127. StringFromGUID2(CLSID_WLBSProvider, wcID, 128);
  128. StringCbCopy(szCLSID, sizeof(szCLSID), TEXT("Software\\classes\\CLSID\\"));
  129. StringCbCat(szCLSID, sizeof(szCLSID), wcID);
  130. // Create entries under CLSID
  131. nRet = RegCreateKey( HKEY_LOCAL_MACHINE, szCLSID, &hKey1 );
  132. nRet = RegSetValueEx( hKey1,
  133. NULL,
  134. 0,
  135. REG_SZ,
  136. (BYTE *)pName,
  137. (wcslen(pName)+1) * sizeof(WCHAR) );
  138. nRet = RegCreateKey( hKey1, L"InprocServer32", &hKey2 );
  139. GetModuleFileName( g_hModule, szModule, MAX_PATH );
  140. // GetModuleFileName will NOT null terminate the string if the file name was >= MAX_PATH characters
  141. szModule[MAX_PATH-1] = 0;
  142. nRet = RegSetValueEx( hKey2,
  143. NULL,
  144. 0,
  145. REG_SZ,
  146. (BYTE *)szModule,
  147. (wcslen(szModule)+1) * sizeof(WCHAR) );
  148. nRet = RegSetValueEx( hKey2,
  149. L"ThreadingModel",
  150. 0,
  151. REG_SZ,
  152. (BYTE *)pThreadingModel,
  153. (wcslen(pThreadingModel)+1) * sizeof(WCHAR) );
  154. nRet = RegCloseKey(hKey1);
  155. nRet = RegCloseKey(hKey2);
  156. return NOERROR;
  157. }
  158. //***************************************************************************
  159. //
  160. // DllUnregisterServer
  161. //
  162. // Purpose: Called when it is time to remove the registry entries.
  163. //
  164. // Return: NOERROR if registration successful, error otherwise.
  165. //***************************************************************************
  166. STDAPI DllUnregisterServer(void)
  167. {
  168. WCHAR szCLSID[128];
  169. WCHAR wcID[128];
  170. HKEY hKey;
  171. // Create the path using the CLSID
  172. StringFromGUID2(CLSID_WLBSProvider, wcID, 128);
  173. StringCbCopy(szCLSID, sizeof(szCLSID), TEXT("Software\\classes\\CLSID\\"));
  174. StringCbCat(szCLSID, sizeof(szCLSID), wcID);
  175. // First delete the InProcServer subkey.
  176. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  177. if(dwRet == NO_ERROR)
  178. {
  179. RegDeleteKey(hKey, L"InProcServer32");
  180. RegCloseKey(hKey);
  181. }
  182. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, L"Software\\classes\\CLSID", &hKey);
  183. if(dwRet == NO_ERROR)
  184. {
  185. RegDeleteKey(hKey,wcID);
  186. RegCloseKey(hKey);
  187. }
  188. return NOERROR;
  189. }