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.

235 lines
6.2 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: WBEM 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)1998 Microsoft Corporation, All Rights Reserved
  13. //
  14. //***************************************************************************
  15. #include <stdafx.h>
  16. HMODULE ghModule;
  17. //============
  18. WCHAR *GUIDSTRING = L"{C9839D99-2345-11d2-A334-00C04F86DB06}";
  19. CLSID CLSID_DHCP_SERVER;
  20. //Count number of objects and number of locks.
  21. long g_cLock=0;
  22. //***************************************************************************
  23. //
  24. // DllGetClassObject
  25. //
  26. // Purpose: Called by Ole when some client wants a class factory. Return
  27. // one only if it is the sort of class this DLL supports.
  28. //
  29. //***************************************************************************
  30. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  31. {
  32. HRESULT hr;
  33. CWbemGlueFactory *pObj;
  34. CLSIDFromString(GUIDSTRING, &CLSID_DHCP_SERVER);
  35. if (CLSID_DHCP_SERVER!=rclsid)
  36. return E_FAIL;
  37. pObj=new CWbemGlueFactory();
  38. if (NULL==pObj)
  39. return E_OUTOFMEMORY;
  40. hr=pObj->QueryInterface(riid, ppv);
  41. if (FAILED(hr))
  42. delete pObj;
  43. return hr;
  44. }
  45. //***************************************************************************
  46. //
  47. // DllCanUnloadNow
  48. //
  49. // Purpose: Called periodically by Ole in order to determine if the
  50. // DLL can be freed.
  51. //
  52. // Return: S_OK if there are no objects in use and the class factory
  53. // isn't locked.
  54. //
  55. //***************************************************************************
  56. STDAPI DllCanUnloadNow(void)
  57. {
  58. SCODE sc;
  59. // It is OK to unload if there are no objects or locks on the
  60. // class factory and the framework is done with you.
  61. if ((0L==g_cLock) && CWbemProviderGlue::FrameworkLogoffDLL("DHCP_SERVER"))
  62. {
  63. sc = S_OK;
  64. }
  65. else
  66. {
  67. sc = S_FALSE;
  68. }
  69. return sc;
  70. }
  71. //***************************************************************************
  72. //
  73. // Is4OrMore
  74. //
  75. // Returns true if win95 or any version of NT > 3.51
  76. //
  77. //***************************************************************************
  78. BOOL Is4OrMore(void)
  79. {
  80. OSVERSIONINFO os;
  81. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  82. if(!GetVersionEx(&os))
  83. return FALSE; // should never happen
  84. return os.dwMajorVersion >= 4;
  85. }
  86. //***************************************************************************
  87. //
  88. // DllRegisterServer
  89. //
  90. // Purpose: Called during setup or by regsvr32.
  91. //
  92. // Return: NOERROR if registration successful, error otherwise.
  93. //***************************************************************************
  94. STDAPI DllRegisterServer(void)
  95. {
  96. char szID[128];
  97. WCHAR wcID[128];
  98. char szCLSID[128];
  99. char szModule[MAX_PATH];
  100. char * pName = "";
  101. char * pModel;
  102. HKEY hKey1, hKey2;
  103. // TO DO: Using 'Both' is preferable. The framework is designed and written to support
  104. // free threaded code. If you will be writing free-threaded code, uncomment these
  105. // three lines.
  106. // if(Is4OrMore())
  107. // pModel = "Both";
  108. // else
  109. pModel = "Apartment";
  110. // Create the path.
  111. CLSIDFromString(GUIDSTRING, &CLSID_DHCP_SERVER);
  112. StringFromGUID2(CLSID_DHCP_SERVER, wcID, 128);
  113. wcstombs(szID, wcID, 128);
  114. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  115. lstrcat(szCLSID, szID);
  116. // Create entries under CLSID
  117. RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  118. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, lstrlen(pName)+1);
  119. RegCreateKey(hKey1,"InprocServer32",&hKey2);
  120. GetModuleFileName(ghModule, szModule, MAX_PATH);
  121. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  122. lstrlen(szModule)+1);
  123. RegSetValueEx(hKey2, "ThreadingModel", 0, REG_SZ,
  124. (BYTE *)pModel, lstrlen(pModel)+1);
  125. CloseHandle(hKey1);
  126. CloseHandle(hKey2);
  127. return NOERROR;
  128. }
  129. //***************************************************************************
  130. //
  131. // DllUnregisterServer
  132. //
  133. // Purpose: Called when it is time to remove the registry entries.
  134. //
  135. // Return: NOERROR if registration successful, error otherwise.
  136. //***************************************************************************
  137. STDAPI DllUnregisterServer(void)
  138. {
  139. char szID[128];
  140. WCHAR wcID[128];
  141. char szCLSID[128];
  142. HKEY hKey;
  143. // Create the path using the CLSID
  144. CLSIDFromString(GUIDSTRING, &CLSID_DHCP_SERVER);
  145. StringFromGUID2(CLSID_DHCP_SERVER, wcID, 128);
  146. wcstombs(szID, wcID, 128);
  147. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  148. lstrcat(szCLSID, szID);
  149. // First delete the InProcServer subkey.
  150. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  151. if(dwRet == NO_ERROR)
  152. {
  153. RegDeleteKey(hKey, "InProcServer32");
  154. CloseHandle(hKey);
  155. }
  156. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\CLASSES\\CLSID\\"), &hKey);
  157. if(dwRet == NO_ERROR)
  158. {
  159. RegDeleteKey(hKey,szID);
  160. CloseHandle(hKey);
  161. }
  162. return NOERROR;
  163. }
  164. BOOL WINAPI DllMain ( HINSTANCE hInstDLL, // handle to dll module
  165. DWORD fdwReason, // reason for calling function
  166. LPVOID lpReserved ) // reserved
  167. {
  168. BOOL bRet = TRUE;
  169. // Perform actions based on the reason for calling.
  170. switch( fdwReason )
  171. {
  172. case DLL_PROCESS_ATTACH:
  173. // Initialize once for each new process.
  174. // Return FALSE to fail DLL load.
  175. ghModule = hInstDLL;
  176. bRet = CWbemProviderGlue::FrameworkLoginDLL("DHCP_SERVER");
  177. break;
  178. case DLL_THREAD_ATTACH:
  179. // Do thread-specific initialization.
  180. break;
  181. case DLL_THREAD_DETACH:
  182. // Do thread-specific cleanup.
  183. break;
  184. case DLL_PROCESS_DETACH:
  185. // Perform any necessary cleanup.
  186. break;
  187. }
  188. return bRet; // Sstatus of DLL_PROCESS_ATTACH.
  189. }