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.

255 lines
6.9 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. #include <FWcommon.h>
  17. #include <objbase.h>
  18. #include <initguid.h>
  19. void __stdcall InitializeTraceing(void);
  20. void __stdcall DeinitializeTraceing(void);
  21. HMODULE ghModule;
  22. //============
  23. WCHAR *GUIDSTRING = L"{4c97e0a8-c5ea-40fd-960d-7d6c987be0a6}";
  24. CLSID CLSID_NLBSNIC;
  25. extern BOOL g_UpdateConfigurationEnabled;
  26. //Count number of objects and number of locks.
  27. long g_cLock=0;
  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 ((0L==g_cLock) && CWbemProviderGlue::FrameworkLogoffDLL(L"NLBSNIC"))
  68. {
  69. sc = S_OK;
  70. }
  71. else
  72. {
  73. sc = S_FALSE;
  74. }
  75. return sc;
  76. }
  77. //***************************************************************************
  78. //
  79. // Is4OrMore
  80. //
  81. // Returns true if win95 or any version of NT > 3.51
  82. //
  83. //***************************************************************************
  84. BOOL Is4OrMore(void)
  85. {
  86. OSVERSIONINFO os;
  87. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  88. if(!GetVersionEx(&os))
  89. return FALSE; // should never happen
  90. return os.dwMajorVersion >= 4;
  91. }
  92. //***************************************************************************
  93. //
  94. // DllRegisterServer
  95. //
  96. // Purpose: Called during setup or by regsvr32.
  97. //
  98. // Return: NOERROR if registration successful, error otherwise.
  99. //***************************************************************************
  100. STDAPI DllRegisterServer(void)
  101. {
  102. char szID[128];
  103. WCHAR wcID[128];
  104. char szCLSID[128];
  105. char szModule[MAX_PATH];
  106. char * pName = "";
  107. char * pModel;
  108. HKEY hKey1, hKey2;
  109. // TO DO: Using 'Both' is preferable. The framework is designed and written to support
  110. // free threaded code. If you will be writing free-threaded code, uncomment these
  111. // three lines.
  112. if(g_UpdateConfigurationEnabled && Is4OrMore())
  113. pModel = "Both";
  114. else
  115. pModel = "Apartment";
  116. // Create the path.
  117. CLSIDFromString(GUIDSTRING, &CLSID_NLBSNIC);
  118. StringFromGUID2(CLSID_NLBSNIC, wcID, 128);
  119. wcstombs(szID, wcID, 128);
  120. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  121. lstrcat(szCLSID, szID);
  122. // Create entries under CLSID
  123. RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  124. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, lstrlen(pName)+1);
  125. RegCreateKey(hKey1,"InprocServer32",&hKey2);
  126. GetModuleFileName(ghModule, szModule, MAX_PATH);
  127. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  128. lstrlen(szModule)+1);
  129. RegSetValueEx(hKey2, "ThreadingModel", 0, REG_SZ,
  130. (BYTE *)pModel, lstrlen(pModel)+1);
  131. CloseHandle(hKey1);
  132. CloseHandle(hKey2);
  133. return NOERROR;
  134. }
  135. //***************************************************************************
  136. //
  137. // DllUnregisterServer
  138. //
  139. // Purpose: Called when it is time to remove the registry entries.
  140. //
  141. // Return: NOERROR if registration successful, error otherwise.
  142. //***************************************************************************
  143. STDAPI DllUnregisterServer(void)
  144. {
  145. char szID[128];
  146. WCHAR wcID[128];
  147. char szCLSID[128];
  148. HKEY hKey;
  149. // Create the path using the CLSID
  150. CLSIDFromString(GUIDSTRING, &CLSID_NLBSNIC);
  151. StringFromGUID2(CLSID_NLBSNIC, wcID, 128);
  152. wcstombs(szID, wcID, 128);
  153. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  154. lstrcat(szCLSID, szID);
  155. // First delete the InProcServer subkey.
  156. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  157. if(dwRet == NO_ERROR)
  158. {
  159. RegDeleteKey(hKey, "InProcServer32");
  160. CloseHandle(hKey);
  161. }
  162. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\CLASSES\\CLSID\\"), &hKey);
  163. if(dwRet == NO_ERROR)
  164. {
  165. RegDeleteKey(hKey,szID);
  166. CloseHandle(hKey);
  167. }
  168. return NOERROR;
  169. }
  170. //***************************************************************************
  171. //
  172. // DllMain
  173. //
  174. // Purpose: Called by the operating system when processes and threads are
  175. // initialized and terminated, or upon calls to the LoadLibrary
  176. // and FreeLibrary functions
  177. //
  178. // Return: TRUE if load was successful, else FALSE
  179. //***************************************************************************
  180. BOOL APIENTRY DllMain ( HINSTANCE hInstDLL, // handle to dll module
  181. DWORD fdwReason, // reason for calling function
  182. LPVOID lpReserved ) // reserved
  183. {
  184. BOOL bRet = TRUE;
  185. // Perform actions based on the reason for calling.
  186. switch( fdwReason )
  187. {
  188. case DLL_PROCESS_ATTACH:
  189. // TO DO: Consider adding DisableThreadLibraryCalls().
  190. // Initialize once for each new process.
  191. // Return FALSE to fail DLL load.
  192. ghModule = hInstDLL;
  193. bRet = CWbemProviderGlue::FrameworkLoginDLL(L"NLBSNIC");
  194. break;
  195. case DLL_THREAD_ATTACH:
  196. // Do thread-specific initialization.
  197. break;
  198. case DLL_THREAD_DETACH:
  199. // Do thread-specific cleanup.
  200. break;
  201. case DLL_PROCESS_DETACH:
  202. // Perform any necessary cleanup.
  203. break;
  204. }
  205. return bRet; // Sstatus of DLL_PROCESS_ATTACH.
  206. }