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.

287 lines
8.1 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WMI Framework Instance provider
  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)1999 Microsoft Corporation, All Rights Reserved
  13. //
  14. //***************************************************************************
  15. #undef UNICODE
  16. #undef _UNICODE
  17. #include "private.h"
  18. #include "nlbsnic.h"
  19. #include "maindll.tmh"
  20. void __stdcall InitializeTraceing(void);
  21. void __stdcall DeinitializeTraceing(void);
  22. HMODULE ghModule;
  23. //============
  24. WCHAR *GUIDSTRING = L"{4c97e0a8-c5ea-40fd-960d-7d6c987be0a6}";
  25. CLSID CLSID_NLBSNIC;
  26. extern BOOL g_UpdateConfigurationEnabled;
  27. HANDLE g_hEventLog = NULL; // Handle for the local event log
  28. //***************************************************************************
  29. //
  30. // DllGetClassObject
  31. //
  32. // Purpose: Called by Ole when some client wants a class factory. Return
  33. // one only if it is the sort of class this DLL supports.
  34. //
  35. //***************************************************************************
  36. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  37. {
  38. HRESULT hr;
  39. CWbemGlueFactory *pObj;
  40. CLSIDFromString(GUIDSTRING, &CLSID_NLBSNIC);
  41. if (CLSID_NLBSNIC!=rclsid)
  42. return E_FAIL;
  43. pObj=new CWbemGlueFactory();
  44. if (NULL==pObj)
  45. return E_OUTOFMEMORY;
  46. hr=pObj->QueryInterface(riid, ppv);
  47. if (FAILED(hr))
  48. delete pObj;
  49. return hr;
  50. }
  51. //***************************************************************************
  52. //
  53. // DllCanUnloadNow
  54. //
  55. // Purpose: Called periodically by Ole in order to determine if the
  56. // DLL can be freed.
  57. //
  58. // Return: S_OK if there are no objects in use and the class factory
  59. // isn't locked.
  60. //
  61. //***************************************************************************
  62. STDAPI DllCanUnloadNow(void)
  63. {
  64. SCODE sc;
  65. // It is OK to unload if there are no objects or locks on the
  66. // class factory and the framework is done with you.
  67. if ( CWbemProviderGlue::FrameworkLogoffDLL(L"NLBSNIC")
  68. && NlbConfigurationUpdate::CanUnloadNow())
  69. {
  70. sc = S_OK;
  71. }
  72. else
  73. {
  74. sc = S_FALSE;
  75. }
  76. return sc;
  77. }
  78. //***************************************************************************
  79. //
  80. // Is4OrMore
  81. //
  82. // Returns true if win95 or any version of NT > 3.51
  83. //
  84. //***************************************************************************
  85. BOOL Is4OrMore(void)
  86. {
  87. OSVERSIONINFO os;
  88. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  89. if(!GetVersionEx(&os))
  90. return FALSE; // should never happen
  91. return os.dwMajorVersion >= 4;
  92. }
  93. //***************************************************************************
  94. //
  95. // DllRegisterServer
  96. //
  97. // Purpose: Called during setup or by regsvr32.
  98. //
  99. // Return: NOERROR if registration successful, error otherwise.
  100. //***************************************************************************
  101. STDAPI DllRegisterServer(void)
  102. {
  103. char szID[128];
  104. WCHAR wcID[128];
  105. char szCLSID[128];
  106. char szModule[MAX_PATH];
  107. char * pName = "";
  108. char * pModel;
  109. HKEY hKey1, hKey2;
  110. // TO DO: Using 'Both' is preferable. The framework is designed and written to support
  111. // free threaded code. If you will be writing free-threaded code, uncomment these
  112. // three lines.
  113. if(g_UpdateConfigurationEnabled && Is4OrMore())
  114. pModel = "Both";
  115. else
  116. pModel = "Apartment";
  117. // Create the path.
  118. CLSIDFromString(GUIDSTRING, &CLSID_NLBSNIC);
  119. StringFromGUID2(CLSID_NLBSNIC, wcID, ASIZE(wcID));
  120. wcstombs(szID, wcID, sizeof(szID));
  121. (void) StringCbCopy(szCLSID, sizeof(szCLSID), TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  122. (void) StringCbCat(szCLSID, sizeof(szCLSID), szID);
  123. // Create entries under CLSID
  124. RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  125. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, lstrlen(pName)+1);
  126. RegCreateKey(hKey1,"InprocServer32",&hKey2);
  127. GetModuleFileName(ghModule, szModule, MAX_PATH);
  128. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  129. lstrlen(szModule)+1);
  130. RegSetValueEx(hKey2, "ThreadingModel", 0, REG_SZ,
  131. (BYTE *)pModel, lstrlen(pModel)+1);
  132. CloseHandle(hKey1);
  133. CloseHandle(hKey2);
  134. return NOERROR;
  135. }
  136. //***************************************************************************
  137. //
  138. // DllUnregisterServer
  139. //
  140. // Purpose: Called when it is time to remove the registry entries.
  141. //
  142. // Return: NOERROR if registration successful, error otherwise.
  143. //***************************************************************************
  144. STDAPI DllUnregisterServer(void)
  145. {
  146. char szID[128];
  147. WCHAR wcID[128];
  148. char szCLSID[128];
  149. HKEY hKey;
  150. // Create the path using the CLSID
  151. CLSIDFromString(GUIDSTRING, &CLSID_NLBSNIC);
  152. StringFromGUID2(CLSID_NLBSNIC, wcID, ASIZE(wcID));
  153. wcstombs(szID, wcID, sizeof(szID));
  154. (void) StringCbCopy(szCLSID, sizeof(szCLSID), TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  155. (void) StringCbCat(szCLSID, sizeof(szCLSID), szID);
  156. // First delete the InProcServer subkey.
  157. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  158. if(dwRet == NO_ERROR)
  159. {
  160. RegDeleteKey(hKey, "InProcServer32");
  161. CloseHandle(hKey);
  162. }
  163. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\CLASSES\\CLSID\\"), &hKey);
  164. if(dwRet == NO_ERROR)
  165. {
  166. RegDeleteKey(hKey,szID);
  167. CloseHandle(hKey);
  168. }
  169. return NOERROR;
  170. }
  171. //***************************************************************************
  172. //
  173. // DllMain
  174. //
  175. // Purpose: Called by the operating system when processes and threads are
  176. // initialized and terminated, or upon calls to the LoadLibrary
  177. // and FreeLibrary functions
  178. //
  179. // Return: TRUE if load was successful, else FALSE
  180. //***************************************************************************
  181. BOOL APIENTRY DllMain ( HINSTANCE hInstDLL, // handle to dll module
  182. DWORD fdwReason, // reason for calling function
  183. LPVOID lpReserved ) // reserved
  184. {
  185. BOOL bRet = TRUE;
  186. // Perform actions based on the reason for calling.
  187. switch( fdwReason )
  188. {
  189. case DLL_PROCESS_ATTACH:
  190. // TO DO: Consider adding DisableThreadLibraryCalls().
  191. // Initialize once for each new process.
  192. // Return FALSE to fail DLL load.
  193. ghModule = hInstDLL;
  194. bRet = CWbemProviderGlue::FrameworkLoginDLL(L"NLBSNIC");
  195. if (bRet)
  196. {
  197. //
  198. // Enable WMI event tracing
  199. //
  200. WPP_INIT_TRACING(L"Microsoft\\NLB\\NLBMPROV");
  201. //
  202. // Initialize for event logging
  203. //
  204. g_hEventLog = RegisterEventSourceW(NULL, CVY_NAME);
  205. }
  206. break;
  207. case DLL_THREAD_ATTACH:
  208. // Do thread-specific initialization.
  209. break;
  210. case DLL_THREAD_DETACH:
  211. // Do thread-specific cleanup.
  212. break;
  213. case DLL_PROCESS_DETACH:
  214. // Perform any necessary cleanup.
  215. MyNlbsNicSet.DelayedDeinitialize();
  216. //
  217. // Disable WMI event tracing
  218. //
  219. WPP_CLEANUP();
  220. //
  221. // Close handle to the event log
  222. //
  223. if (g_hEventLog != NULL)
  224. {
  225. (void) DeregisterEventSource(g_hEventLog);
  226. g_hEventLog = NULL;
  227. }
  228. break;
  229. }
  230. return bRet; // Sstatus of DLL_PROCESS_ATTACH.
  231. }