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.

420 lines
8.6 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. #include <DLOle.h>
  24. #ifdef WINHTTP_STATIC_LIBRARY
  25. #error dllentry.cxx should not be built in the static winhttpx.lib!!
  26. #endif
  27. #if defined(__cplusplus)
  28. extern "C" {
  29. #endif
  30. BOOL
  31. WINAPI
  32. DllMain(
  33. IN HINSTANCE DllHandle,
  34. IN DWORD Reason,
  35. IN LPVOID Reserved
  36. );
  37. #if defined(__cplusplus)
  38. }
  39. #endif
  40. //
  41. // global data
  42. //
  43. GLOBAL CCritSec GeneralInitCritSec;
  44. //
  45. // functions
  46. //
  47. VOID AutoProxySvcDetach(VOID);
  48. VOID AutoProxySvcUnload(VOID);
  49. BOOL
  50. WINAPI
  51. DllMain(
  52. IN HINSTANCE DllHandle,
  53. IN DWORD Reason,
  54. IN LPVOID Reserved
  55. )
  56. /*++
  57. Routine Description:
  58. Performs global initialization and termination for all protocol modules.
  59. This function only handles process attach and detach which are required for
  60. global initialization and termination, respectively. We disable thread
  61. attach and detach. New threads calling Wininet APIs will get an
  62. INTERNET_THREAD_INFO structure created for them by the first API requiring
  63. this structure
  64. Arguments:
  65. DllHandle - handle of this DLL. Unused
  66. Reason - process attach/detach or thread attach/detach
  67. Reserved - if DLL_PROCESS_ATTACH, NULL means DLL is being dynamically
  68. loaded, else static. For DLL_PROCESS_DETACH, NULL means DLL
  69. is being freed as a consequence of call to FreeLibrary()
  70. else the DLL is being freed as part of process termination
  71. Return Value:
  72. BOOL
  73. Success - TRUE
  74. Failure - FALSE. Failed to initialize
  75. --*/
  76. {
  77. //
  78. // perform global dll initialization, if any.
  79. //
  80. switch (Reason) {
  81. case DLL_PROCESS_ATTACH:
  82. INITIALIZE_DEBUG_REGKEY();
  83. INITIALIZE_MEMORY_MANAGER();
  84. GlobalDllHandle = DllHandle;
  85. GlobalPlatformType = PlatformType(&GlobalPlatformVersion5);
  86. if (!GeneralInitCritSec.Init())
  87. {
  88. return FALSE;
  89. }
  90. if (!g_pAsyncCount)
  91. {
  92. g_pAsyncCount = New CAsyncCount();
  93. if (!g_pAsyncCount)
  94. return FALSE;
  95. }
  96. if( !DLOleInitialize())
  97. {
  98. return FALSE;
  99. }
  100. INET_DEBUG_START();
  101. CTracer::s_CritSectionTraceInit.Init();
  102. CTracer::GlobalTraceInit();
  103. if (!GlobalDllInitialize() || !InternetCreateThreadInfo(TRUE))
  104. {
  105. return FALSE;
  106. }
  107. {
  108. DEBUG_ENTER((DBG_DLL,
  109. Bool,
  110. "DllMain",
  111. "%#x, %s, %#x",
  112. DllHandle,
  113. "DLL_PROCESS_ATTACH",
  114. Reserved
  115. ));
  116. // LOG_EVENT(WINHTTP_INFO, "WinHttp loaded");
  117. DEBUG_LEAVE(TRUE);
  118. }
  119. break;
  120. case DLL_PROCESS_DETACH:
  121. // LOG_EVENT(WINHTTP_INFO, "WinHttp unloaded");
  122. //
  123. // signal to all APIs (and any other function that might have an
  124. // interest) that the DLL is being shutdown
  125. //
  126. GlobalDynaUnload = (Reserved == NULL) ? TRUE : FALSE;
  127. InDllCleanup = TRUE;
  128. {
  129. DEBUG_ENTER((DBG_DLL,
  130. Bool,
  131. "DllMain",
  132. "%#x, %s, %#x",
  133. DllHandle,
  134. "DLL_PROCESS_DETACH",
  135. Reserved
  136. ));
  137. DEBUG_PRINT(DLL,
  138. INFO,
  139. ("DLL Terminated\n"
  140. ));
  141. DEBUG_LEAVE(TRUE);
  142. }
  143. AutoProxySvcDetach();
  144. AutoProxySvcUnload();
  145. if (GlobalDynaUnload) {
  146. if (GlobalDataInitialized) {
  147. GlobalDataTerminate();
  148. }
  149. GlobalDllTerminate();
  150. InternetTerminateThreadInfo();
  151. }
  152. PERF_DUMP();
  153. PERF_END();
  154. //TERMINATE_DEBUG_MEMORY(FALSE);
  155. INET_DEBUG_FINISH();
  156. TERMINATE_DEBUG_REGKEY();
  157. CTracer::GlobalTraceTerm();
  158. //InternetDestroyThreadInfo();
  159. if (g_pAsyncCount)
  160. {
  161. delete g_pAsyncCount;
  162. g_pAsyncCount = NULL;
  163. }
  164. #ifdef USE_ROCKALL
  165. // RENO 35983: skip Rockall destructor if process is shutting down.
  166. if (GlobalDynaUnload)
  167. #endif
  168. {
  169. TERMINATE_MEMORY_MANAGER(TRUE);
  170. }
  171. GeneralInitCritSec.FreeLock();
  172. break;
  173. case DLL_THREAD_DETACH:
  174. //
  175. // kill the INTERNET_THREAD_INFO
  176. //
  177. {
  178. DEBUG_ENTER((DBG_DLL,
  179. Bool,
  180. "DllMain",
  181. "%#x, %s, %#x",
  182. DllHandle,
  183. "DLL_THREAD_DETACH",
  184. Reserved
  185. ));
  186. DEBUG_LEAVE(TRUE);
  187. }
  188. InternetDestroyThreadInfo();
  189. MEMORY_MANAGER_ON_THREAD_DETACH();
  190. break;
  191. case DLL_THREAD_ATTACH:
  192. //
  193. // we do nothing for thread attach - if we need an INTERNET_THREAD_INFO
  194. // then it gets created by the function which realises we need one
  195. //
  196. {
  197. DEBUG_ENTER((DBG_DLL,
  198. Bool,
  199. "DllMain",
  200. "%#x, %s, %#x",
  201. DllHandle,
  202. "DLL_THREAD_ATTACH",
  203. Reserved
  204. ));
  205. AllowCAP();
  206. DEBUG_LEAVE(TRUE);
  207. }
  208. break;
  209. }
  210. return TRUE;
  211. }
  212. //////////////////////////////////////////////////////////////////////////
  213. //
  214. // Autoregistration entry points
  215. //
  216. //////////////////////////////////////////////////////////////////////////
  217. HRESULT RegisterTypeLib()
  218. {
  219. ITypeLib * pTypeLib;
  220. char szPath[MAX_PATH+1];
  221. OLECHAR wszPath[MAX_PATH+1];
  222. HRESULT hr;
  223. GetModuleFileName(GlobalDllHandle, szPath, sizeof(szPath)-1); // leave room for null char
  224. szPath[sizeof(szPath)-1] = '\0'; // guarantee null termination
  225. MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
  226. hr = DL(LoadTypeLib)(wszPath, &pTypeLib);
  227. if (SUCCEEDED(hr))
  228. {
  229. hr = DL(RegisterTypeLib)(pTypeLib, wszPath, NULL);
  230. pTypeLib->Release();
  231. }
  232. if (FAILED(hr))
  233. {
  234. hr = SELFREG_E_TYPELIB;
  235. }
  236. return hr;
  237. }
  238. HRESULT UnregisterTypeLib()
  239. {
  240. ITypeLib * pTypeLib;
  241. TLIBATTR * pTLibAttr;
  242. char szPath[MAX_PATH+1];
  243. OLECHAR wszPath[MAX_PATH+1];
  244. HRESULT hr = NOERROR;
  245. GetModuleFileName(GlobalDllHandle, szPath, sizeof(szPath)-1); // leave room for null char
  246. szPath[sizeof(szPath)-1] = '\0'; // guarantee null termination
  247. MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH);
  248. hr = DL(LoadTypeLib)(wszPath, &pTypeLib);
  249. if (SUCCEEDED(hr))
  250. {
  251. hr = pTypeLib->GetLibAttr(&pTLibAttr);
  252. if (SUCCEEDED(hr))
  253. {
  254. hr = DL(UnRegisterTypeLib)(pTLibAttr->guid, pTLibAttr->wMajorVerNum,
  255. pTLibAttr->wMinorVerNum,
  256. pTLibAttr->lcid,
  257. pTLibAttr->syskind);
  258. pTypeLib->ReleaseTLibAttr(pTLibAttr);
  259. }
  260. pTypeLib->Release();
  261. }
  262. return hr;
  263. }
  264. HRESULT CallRegInstall(LPSTR szSection)
  265. {
  266. HRESULT hr = E_FAIL;
  267. HINSTANCE hinstAdvPack = LoadLibrary(TEXT("ADVPACK.DLL"));
  268. if (hinstAdvPack)
  269. {
  270. REGINSTALL pfnri = (REGINSTALL)GetProcAddress(hinstAdvPack, achREGINSTALL);
  271. if (pfnri)
  272. {
  273. hr = pfnri(GlobalDllHandle, szSection, NULL);
  274. }
  275. FreeLibrary(hinstAdvPack);
  276. }
  277. return hr;
  278. }
  279. STDAPI DllRegisterServer()
  280. {
  281. HRESULT hr;
  282. if (!DelayLoad( &g_moduleOleAut32))
  283. {
  284. return E_OUTOFMEMORY;
  285. }
  286. hr = CallRegInstall("Reg");
  287. if (SUCCEEDED(hr))
  288. {
  289. hr = RegisterTypeLib();
  290. }
  291. return hr;
  292. }
  293. STDAPI DllUnregisterServer()
  294. {
  295. HRESULT hr;
  296. if (!DelayLoad( &g_moduleOleAut32))
  297. {
  298. return E_OUTOFMEMORY;
  299. }
  300. UnregisterTypeLib();
  301. hr = CallRegInstall("Unreg");
  302. return hr;
  303. }