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.

374 lines
7.8 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. dllentry.cxx
  5. Abstract:
  6. Entry point for WinInet Internet client DLL
  7. Contents:
  8. WinInetDllEntryPoint
  9. Author:
  10. Richard L Firth (rfirth) 10-Nov-1994
  11. Environment:
  12. Win32 (user-mode) DLL
  13. Revision History:
  14. 10-Nov-1994 rfirth
  15. Created
  16. --*/
  17. #include <wininetp.h>
  18. #include <process.h>
  19. #include <perfdiag.hxx>
  20. #include <shlwapi.h>
  21. #include <advpub.h>
  22. #include <olectl.h>
  23. #ifdef WINHTTP_STATIC_LIBRARY
  24. #error dllentry.cxx should not be built in the static winhttpx.lib!!
  25. #endif
  26. #if defined(__cplusplus)
  27. extern "C" {
  28. #endif
  29. BOOL
  30. WINAPI
  31. DllMain(
  32. IN HINSTANCE DllHandle,
  33. IN DWORD Reason,
  34. IN LPVOID Reserved
  35. );
  36. #if defined(__cplusplus)
  37. }
  38. #endif
  39. //
  40. // global data
  41. //
  42. GLOBAL CCritSec GeneralInitCritSec;
  43. //
  44. // functions
  45. //
  46. BOOL
  47. WINAPI
  48. DllMain(
  49. IN HINSTANCE DllHandle,
  50. IN DWORD Reason,
  51. IN LPVOID Reserved
  52. )
  53. /*++
  54. Routine Description:
  55. Performs global initialization and termination for all protocol modules.
  56. This function only handles process attach and detach which are required for
  57. global initialization and termination, respectively. We disable thread
  58. attach and detach. New threads calling Wininet APIs will get an
  59. INTERNET_THREAD_INFO structure created for them by the first API requiring
  60. this structure
  61. Arguments:
  62. DllHandle - handle of this DLL. Unused
  63. Reason - process attach/detach or thread attach/detach
  64. Reserved - if DLL_PROCESS_ATTACH, NULL means DLL is being dynamically
  65. loaded, else static. For DLL_PROCESS_DETACH, NULL means DLL
  66. is being freed as a consequence of call to FreeLibrary()
  67. else the DLL is being freed as part of process termination
  68. Return Value:
  69. BOOL
  70. Success - TRUE
  71. Failure - FALSE. Failed to initialize
  72. --*/
  73. {
  74. if (Reason != DLL_PROCESS_ATTACH) {
  75. DEBUG_ENTER((DBG_DLL,
  76. Bool,
  77. "DllMain",
  78. "%#x, %s, %#x",
  79. DllHandle,
  80. (Reason == DLL_PROCESS_ATTACH) ? "DLL_PROCESS_ATTACH"
  81. : (Reason == DLL_PROCESS_DETACH) ? "DLL_PROCESS_DETACH"
  82. : (Reason == DLL_THREAD_ATTACH) ? "DLL_THREAD_ATTACH"
  83. : (Reason == DLL_THREAD_DETACH) ? "DLL_THREAD_DETACH"
  84. : "?",
  85. Reserved
  86. ));
  87. }
  88. DWORD error;
  89. //
  90. // perform global dll initialization, if any.
  91. //
  92. switch (Reason) {
  93. case DLL_PROCESS_ATTACH:
  94. INITIALIZE_DEBUG_REGKEY();
  95. INITIALIZE_MEMORY_MANAGER();
  96. GlobalDllHandle = DllHandle;
  97. GlobalPlatformType = PlatformType(&GlobalPlatformVersion5);
  98. if (!GeneralInitCritSec.Init())
  99. {
  100. return FALSE;
  101. }
  102. if (!g_pAsyncCount)
  103. {
  104. g_pAsyncCount = New CAsyncCount();
  105. if (!g_pAsyncCount)
  106. return FALSE;
  107. }
  108. INET_DEBUG_START();
  109. if (!GlobalDllInitialize() || !InternetCreateThreadInfo(TRUE))
  110. {
  111. return FALSE;
  112. }
  113. DEBUG_ENTER((DBG_DLL,
  114. Bool,
  115. "DllMain",
  116. "%#x, %s, %#x",
  117. DllHandle,
  118. (Reason == DLL_PROCESS_ATTACH) ? "DLL_PROCESS_ATTACH"
  119. : (Reason == DLL_PROCESS_DETACH) ? "DLL_PROCESS_DETACH"
  120. : (Reason == DLL_THREAD_ATTACH) ? "DLL_THREAD_ATTACH"
  121. : (Reason == DLL_THREAD_DETACH) ? "DLL_THREAD_DETACH"
  122. : "?",
  123. Reserved
  124. ));
  125. // LOG_EVENT(WINHTTP_INFO, "WinHttp5 loaded");
  126. DEBUG_LEAVE(TRUE);
  127. break;
  128. case DLL_PROCESS_DETACH:
  129. // LOG_EVENT(WINHTTP_INFO, "WinHttp5 unloaded");
  130. //
  131. // signal to all APIs (and any other function that might have an
  132. // interest) that the DLL is being shutdown
  133. //
  134. GlobalDynaUnload = (Reserved == NULL) ? TRUE : FALSE;
  135. InDllCleanup = TRUE;
  136. DEBUG_PRINT(DLL,
  137. INFO,
  138. ("DLL Terminated\n"
  139. ));
  140. DEBUG_LEAVE(TRUE);
  141. if (GlobalDynaUnload) {
  142. if (GlobalDataInitialized) {
  143. GlobalDataTerminate();
  144. }
  145. GlobalDllTerminate();
  146. InternetTerminateThreadInfo();
  147. }
  148. PERF_DUMP();
  149. PERF_END();
  150. //TERMINATE_DEBUG_MEMORY(FALSE);
  151. INET_DEBUG_FINISH();
  152. TERMINATE_DEBUG_REGKEY();
  153. //InternetDestroyThreadInfo();
  154. if (g_pAsyncCount)
  155. {
  156. delete g_pAsyncCount;
  157. g_pAsyncCount = NULL;
  158. }
  159. TERMINATE_MEMORY_MANAGER(TRUE);
  160. GeneralInitCritSec.FreeLock();
  161. break;
  162. case DLL_THREAD_DETACH:
  163. //
  164. // kill the INTERNET_THREAD_INFO
  165. //
  166. DEBUG_LEAVE(TRUE);
  167. InternetDestroyThreadInfo();
  168. break;
  169. case DLL_THREAD_ATTACH:
  170. //
  171. // we do nothing for thread attach - if we need an INTERNET_THREAD_INFO
  172. // then it gets created by the function which realises we need one
  173. //
  174. AllowCAP();
  175. DEBUG_LEAVE(TRUE);
  176. break;
  177. }
  178. return TRUE;
  179. }
  180. //////////////////////////////////////////////////////////////////////////
  181. //
  182. // Autoregistration entry points
  183. //
  184. //////////////////////////////////////////////////////////////////////////
  185. HRESULT RegisterTypeLib()
  186. {
  187. ITypeLib * pTypeLib;
  188. char szPath[MAX_PATH];
  189. OLECHAR wszPath[MAX_PATH];
  190. HRESULT hr;
  191. GetModuleFileName(GlobalDllHandle, szPath, MAX_PATH);
  192. MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
  193. hr = LoadTypeLib(wszPath, &pTypeLib);
  194. if (SUCCEEDED(hr))
  195. {
  196. hr = ::RegisterTypeLib(pTypeLib, wszPath, NULL);
  197. pTypeLib->Release();
  198. }
  199. if (FAILED(hr))
  200. {
  201. hr = SELFREG_E_TYPELIB;
  202. }
  203. return hr;
  204. }
  205. HRESULT UnregisterTypeLib()
  206. {
  207. ITypeLib * pTypeLib;
  208. TLIBATTR * pTLibAttr;
  209. char szPath[MAX_PATH];
  210. OLECHAR wszPath[MAX_PATH];
  211. HRESULT hr = NOERROR;
  212. GetModuleFileName(GlobalDllHandle, szPath, MAX_PATH);
  213. MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
  214. hr = LoadTypeLib(wszPath, &pTypeLib);
  215. if (SUCCEEDED(hr))
  216. {
  217. hr = pTypeLib->GetLibAttr(&pTLibAttr);
  218. if (SUCCEEDED(hr))
  219. {
  220. hr = ::UnRegisterTypeLib(pTLibAttr->guid, pTLibAttr->wMajorVerNum,
  221. pTLibAttr->wMinorVerNum,
  222. pTLibAttr->lcid,
  223. pTLibAttr->syskind);
  224. pTypeLib->ReleaseTLibAttr(pTLibAttr);
  225. }
  226. pTypeLib->Release();
  227. }
  228. return hr;
  229. }
  230. HRESULT CallRegInstall(LPSTR szSection)
  231. {
  232. HRESULT hr = E_FAIL;
  233. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  234. if (hinstAdvPack)
  235. {
  236. REGINSTALL pfnri = (REGINSTALL)GetProcAddress(hinstAdvPack, achREGINSTALL);
  237. if (pfnri)
  238. {
  239. hr = pfnri(GlobalDllHandle, szSection, NULL);
  240. }
  241. FreeLibrary(hinstAdvPack);
  242. }
  243. return hr;
  244. }
  245. STDAPI DllRegisterServer()
  246. {
  247. HRESULT hr;
  248. hr = CallRegInstall("Reg");
  249. if (SUCCEEDED(hr))
  250. {
  251. hr = RegisterTypeLib();
  252. }
  253. return hr;
  254. }
  255. STDAPI DllUnregisterServer()
  256. {
  257. HRESULT hr;
  258. UnregisterTypeLib();
  259. hr = CallRegInstall("Unreg");
  260. return hr;
  261. }