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.

259 lines
6.8 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. MAINDLL.CPP
  5. Abstract:
  6. Contains DLL entry points. Also has code that controls
  7. when the DLL can be unloaded by tracking the number of
  8. objects and locks as well as routines that support
  9. self registration.
  10. Revision History:
  11. Ghim-Sim Chua (gschua) 04/27/99
  12. - Created
  13. ********************************************************************/
  14. #include "pchealth.h"
  15. #include <objbase.h>
  16. #include <initguid.h>
  17. #include <WBEMGlue.h>
  18. /////////////////////////////////////////////////////////////////////////////
  19. // tracing stuff
  20. #ifdef THIS_FILE
  21. #undef THIS_FILE
  22. #endif
  23. static char __szTraceSourceFile[] = __FILE__;
  24. #define THIS_FILE __szTraceSourceFile
  25. #define TRACE_ID DCID_MAINDLL
  26. HMODULE ghModule;
  27. //============
  28. WCHAR *GUIDSTRING = L"{c52586f0-f805-11d2-b3a7-00c04fa35c1a}";
  29. CLSID CLSID_PCH_WINSOCK;
  30. //Count number of objects and number of locks.
  31. long g_cLock=0;
  32. //
  33. // Keep a global IWbemServices pointer, since we use it frequently and
  34. // it's a little expensive to get.
  35. //
  36. CComPtr<IWbemServices> g_pWbemServices = NULL;
  37. //***************************************************************************
  38. //
  39. // DllGetClassObject
  40. //
  41. // Purpose: Called by Ole when some client wants a class factory. Return
  42. // one only if it is the sort of class this DLL supports.
  43. //
  44. //***************************************************************************
  45. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  46. {
  47. HRESULT hr;
  48. CWbemGlueFactory *pObj;
  49. CLSIDFromString(GUIDSTRING, &CLSID_PCH_WINSOCK);
  50. if (CLSID_PCH_WINSOCK!=rclsid)
  51. return E_FAIL;
  52. pObj=new CWbemGlueFactory();
  53. if (NULL==pObj)
  54. return E_OUTOFMEMORY;
  55. hr=pObj->QueryInterface(riid, ppv);
  56. if (FAILED(hr))
  57. delete pObj;
  58. return hr;
  59. }
  60. //***************************************************************************
  61. //
  62. // DllCanUnloadNow
  63. //
  64. // Purpose: Called periodically by Ole in order to determine if the
  65. // DLL can be freed.
  66. //
  67. // Return: S_OK if there are no objects in use and the class factory
  68. // isn't locked.
  69. //
  70. //***************************************************************************
  71. STDAPI DllCanUnloadNow(void)
  72. {
  73. SCODE sc;
  74. // It is OK to unload if there are no objects or locks on the
  75. // class factory and the framework is done with you.
  76. if ((0L==g_cLock) && CWbemProviderGlue::FrameworkLogoffDLL(L"PCH_WINSOCK"))
  77. {
  78. sc = S_OK;
  79. }
  80. else
  81. {
  82. sc = S_FALSE;
  83. }
  84. return sc;
  85. }
  86. //***************************************************************************
  87. //
  88. // Is4OrMore
  89. //
  90. // Returns true if win95 or any version of NT > 3.51
  91. //
  92. //***************************************************************************
  93. BOOL Is4OrMore(void)
  94. {
  95. OSVERSIONINFO os;
  96. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  97. if(!GetVersionEx(&os))
  98. return FALSE; // should never happen
  99. return os.dwMajorVersion >= 4;
  100. }
  101. //***************************************************************************
  102. //
  103. // DllRegisterServer
  104. //
  105. // Purpose: Called during setup or by regsvr32.
  106. //
  107. // Return: NOERROR if registration successful, error otherwise.
  108. //***************************************************************************
  109. STDAPI DllRegisterServer(void)
  110. {
  111. char szID[128];
  112. WCHAR wcID[128];
  113. char szCLSID[128];
  114. char szModule[MAX_PATH];
  115. char * pName = "";
  116. char * pModel;
  117. HKEY hKey1, hKey2;
  118. ghModule = GetModuleHandle("pchprov");
  119. // TO DO: Using 'Both' is preferable. The framework is designed and written to support
  120. // free threaded code. If you will be writing free-threaded code, uncomment these
  121. // three lines.
  122. if(Is4OrMore())
  123. pModel = "Both";
  124. else
  125. pModel = "Apartment";
  126. // Create the path.
  127. CLSIDFromString(GUIDSTRING, &CLSID_PCH_WINSOCK);
  128. StringFromGUID2(CLSID_PCH_WINSOCK, wcID, 128);
  129. wcstombs(szID, wcID, 128);
  130. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  131. lstrcat(szCLSID, szID);
  132. // Create entries under CLSID
  133. RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  134. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, lstrlen(pName)+1);
  135. RegCreateKey(hKey1,"InprocServer32",&hKey2);
  136. GetModuleFileName(ghModule, szModule, MAX_PATH);
  137. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  138. lstrlen(szModule)+1);
  139. RegSetValueEx(hKey2, "ThreadingModel", 0, REG_SZ,
  140. (BYTE *)pModel, lstrlen(pModel)+1);
  141. CloseHandle(hKey1);
  142. CloseHandle(hKey2);
  143. return NOERROR;
  144. }
  145. //***************************************************************************
  146. //
  147. // DllUnregisterServer
  148. //
  149. // Purpose: Called when it is time to remove the registry entries.
  150. //
  151. // Return: NOERROR if registration successful, error otherwise.
  152. //***************************************************************************
  153. STDAPI DllUnregisterServer(void)
  154. {
  155. char szID[128];
  156. WCHAR wcID[128];
  157. char szCLSID[128];
  158. HKEY hKey;
  159. // Create the path using the CLSID
  160. CLSIDFromString(GUIDSTRING, &CLSID_PCH_WINSOCK);
  161. StringFromGUID2(CLSID_PCH_WINSOCK, wcID, 128);
  162. wcstombs(szID, wcID, 128);
  163. lstrcpy(szCLSID, TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  164. lstrcat(szCLSID, szID);
  165. // First delete the InProcServer subkey.
  166. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  167. if(dwRet == NO_ERROR)
  168. {
  169. RegDeleteKey(hKey, "InProcServer32");
  170. CloseHandle(hKey);
  171. }
  172. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\CLASSES\\CLSID\\"), &hKey);
  173. if(dwRet == NO_ERROR)
  174. {
  175. RegDeleteKey(hKey,szID);
  176. CloseHandle(hKey);
  177. }
  178. return NOERROR;
  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. // Initialize once for each new process.
  190. // Return FALSE to fail DLL load.
  191. bRet = CWbemProviderGlue::FrameworkLoginDLL(L"PCH_WINSOCK");
  192. break;
  193. case DLL_THREAD_ATTACH:
  194. // Do thread-specific initialization.
  195. break;
  196. case DLL_THREAD_DETACH:
  197. // Do thread-specific cleanup.
  198. break;
  199. case DLL_PROCESS_DETACH:
  200. // Perform any necessary cleanup.
  201. break;
  202. }
  203. return bRet; // Sstatus of DLL_PROCESS_ATTACH.
  204. }