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.

347 lines
9.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: ComAPIs.cxx (16 bit target)
  7. //
  8. // Contents: CompObj APIs
  9. //
  10. // Functions:
  11. //
  12. // History: 16-Dec-93 JohannP Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include <headers.cxx>
  16. #pragma hdrstop
  17. #include <ole2sp.h>
  18. #include <olecoll.h>
  19. #include <map_kv.h>
  20. #include <stdlib.h>
  21. #include "map_htsk.h"
  22. #include "etask.hxx"
  23. #include "call32.hxx"
  24. #include "apilist.hxx"
  25. // Opmodes should be removed
  26. // They don't seem to be necessary any more
  27. DECLARE_INFOLEVEL(thk1);
  28. DECLARE_INFOLEVEL(Stack1);
  29. CMapHandleEtask NEAR v_mapToEtask(MEMCTX_SHARED);
  30. #if defined(_CHICAGO_)
  31. Etask getask = {0,0,0,0,0,0,0,0};
  32. extern IMalloc FAR* v_pMallocShared; // is not addrefed
  33. BOOL SetupSharedAllocator(Etask FAR& etask);
  34. STDAPI_(BOOL) RemoveEtask(HTASK FAR& hTask, Etask FAR& etask);
  35. #endif // _CHICAGO_
  36. //+---------------------------------------------------------------------------
  37. //
  38. // Function: LibMain, public
  39. //
  40. // Synopsis: DLL initialization function
  41. //
  42. // Arguments: [hinst] - Instance handle
  43. // [wDataSeg] - Current DS
  44. // [cbHeapSize] - Heap size for the DLL
  45. // [lpszCmdLine] - Command line information
  46. //
  47. // Returns: One for success, zero for failure
  48. //
  49. // History: 21-Feb-94 DrewB Created
  50. //
  51. //----------------------------------------------------------------------------
  52. #if DBG == 1
  53. static char achInfoLevel[32];
  54. #ifdef _CHICAGO_
  55. BOOL fSSOn = TRUE;
  56. #endif // _CHICAGO_
  57. #endif
  58. extern "C" int CALLBACK LibMain(HINSTANCE hinst,
  59. WORD wDataSeg,
  60. WORD cbHeapSize,
  61. LPSTR lpszCmdLine)
  62. {
  63. #if DBG == 1
  64. if (GetProfileString("olethk32", "InfoLevel", "3", achInfoLevel,
  65. sizeof(achInfoLevel)) > 0)
  66. {
  67. thk1InfoLevel = strtoul(achInfoLevel, NULL, 0);
  68. }
  69. #ifdef _CHICAGO_
  70. if (GetProfileString("CairOLE InfoLevels",
  71. "Stack", "3", achInfoLevel,
  72. sizeof(achInfoLevel)) > 0)
  73. {
  74. Stack1InfoLevel = strtoul(achInfoLevel, NULL, 0);
  75. }
  76. #endif // _CHICAGO_
  77. #endif
  78. thkDebugOut((DEB_DLLS16, "CompObj16: LibMain called on Process (%X) \n", GetCurrentProcess() ));
  79. #if defined(_CHICAGO_)
  80. //
  81. // The Chicago debugger doesn't like hinst not being wired.
  82. //
  83. GlobalWire(hinst);
  84. #else
  85. // NOTE: on WIN95 Call32Initialize is called by DLLEntryPoint
  86. if (!Call32Initialize())
  87. {
  88. return 0;
  89. }
  90. #endif
  91. #ifdef _DEBUG
  92. v_mapToEtask.AssertValid();
  93. #endif
  94. UNREFERENCED(cbHeapSize);
  95. // Leave our DS unlocked when we're not running
  96. UnlockData( 0 );
  97. #if defined(_CHIC_INIT_IN_LIBMAIN_)
  98. if (SetupSharedAllocator(getask) == FALSE)
  99. {
  100. return FALSE;
  101. }
  102. #endif // _CHIC_INIT_IN_LIBMAIN_
  103. thkDebugOut((DEB_DLLS16, "CompObj16: LibMain called on Process (%X) Exitype (%ld)\n", GetCurrentProcess() ));
  104. return TRUE;
  105. }
  106. //+---------------------------------------------------------------------------
  107. //
  108. // Function: WEP, public
  109. //
  110. // Synopsis: Windows Exit Point routine, for receiving DLL unload
  111. // notification
  112. //
  113. // Arguments: [nExitType] - Type of exit occurring
  114. //
  115. // Returns: One for success, zero for failure
  116. //
  117. // History: 21-Feb-94 DrewB Created
  118. //
  119. // Note: Does nothing on WIN95. Call32Unitialize is called in
  120. // DllEntryPoint when load count goes to zero.
  121. //
  122. //----------------------------------------------------------------------------
  123. extern "C" int CALLBACK WEP(int nExitType)
  124. {
  125. thkDebugOut((DEB_DLLS16, "CompObj16: WEP called on Process (%X) Exitype (%ld)\n", GetCurrentProcess(),nExitType));
  126. #if defined(_CHICAGO_)
  127. #if defined(_CHIC_INIT_IN_LIBMAIN_)
  128. if (v_pMallocShared != NULL)
  129. {
  130. getask.m_pMallocShared->Release();
  131. getask.m_pMallocShared = NULL;
  132. v_pMallocShared = NULL;
  133. thkDebugOut((DEB_DLLS16, "CompObj16: WEP v_pMallocShared to NULL (%X) \n",GetCurrentProcess()));
  134. }
  135. #endif // _CHIC_INIT_IN_LIBMAIN_
  136. #else
  137. HTASK htask;
  138. Etask etask;
  139. if (LookupEtask(htask, etask))
  140. {
  141. //
  142. // There is an etask. Check to see if the etask for this task has
  143. // its init count set to ETASK_FAKE_INIT. If it does, we cheated
  144. // and called CoInitialize on the processes behalf, but it never
  145. // called CoUninitialize(). Some apps that only make storage calls
  146. // demonstrate this behaviour. If it is ETASK_FAKE_INIT, then we
  147. // are going to call CoUninitialize on the apps behalf.
  148. //
  149. if (etask.m_inits == ETASK_FAKE_INIT)
  150. {
  151. //
  152. // We are going to set the m_inits == 1, since we called it
  153. // once. Then we are going to call our very own CoUninitialize()
  154. // to let it handle the rest of the cleanup.
  155. //
  156. etask.m_inits = 1;
  157. thkVerify(SetEtask(htask, etask));
  158. CoUninitialize();
  159. }
  160. }
  161. //
  162. // Now uninit the thunk layer
  163. //
  164. Call32Uninitialize();
  165. #endif
  166. thkDebugOut((DEB_DLLS16, "CompObj16: WEP called on Process (%X) done\n", GetCurrentProcess()));
  167. return 1;
  168. }
  169. #if defined(_CHICAGO_)
  170. //+---------------------------------------------------------------------------
  171. //
  172. // Function: DllEntryPoint, public
  173. //
  174. // Synopsis: Chicago's 16-bit DLL Load/Unload Entry Point.
  175. //
  176. // Arguments: [dwReason] - 1 DLL load, 0 DLL unload
  177. // [hInst] - hInstance
  178. // [wDS] - Data Segment
  179. // [wHeapSize] - Heap Size
  180. // [dwReserved1] - Reserved
  181. // [wReserved2] - Reserved
  182. //
  183. // Returns: True for success, False for failure
  184. //
  185. // Remarks: This DLL must be marked as 4.0 for Chicago to call this entry
  186. // point. DllEntryPoint is called between LibMain and WEP. See
  187. // etask.hxx for information of it's elements.
  188. //
  189. // History: 18-Jan-95 JohannP Created
  190. //
  191. // Note: The shared allocator gets initialized first time this function
  192. // gets called with DLL_LOAD. The gets removed when the dll gets
  193. // unloaded.
  194. //
  195. //----------------------------------------------------------------------------
  196. #define DLL_LOAD 1
  197. #define DLL_UNLOAD 0
  198. extern "C" BOOL FAR PASCAL
  199. __export DllEntryPoint(DWORD dwReason,WORD hInst,WORD wDS,
  200. WORD wHeapSize,DWORD dwReserved1,WORD wReserved2)
  201. {
  202. HTASK htask;
  203. Etask etask;
  204. thkDebugOut((DEB_DLLS16, "CompObj16: DllEntryPoint called on Process (%X) Reason: (%ld)\n", GetCurrentProcess(),dwReason));
  205. if (dwReason == DLL_LOAD) // DLL refcount increment
  206. {
  207. etask.m_htask = 0;
  208. etask.m_Dllinits = 0;
  209. // if not init call and init 32 bit
  210. if ( !LookupEtask(htask, etask)
  211. || etask.m_htask != GetCurrentProcess() )
  212. {
  213. // Set up the shared allocator.
  214. if (SetupSharedAllocator(etask) == FALSE)
  215. {
  216. thkAssert(v_pMallocShared != NULL && "CompObj: DllEntryPoint: pMallocShared not initialized");
  217. return FALSE;
  218. }
  219. // Initialize the interop call mechanism
  220. if (!Call32Initialize())
  221. {
  222. thkDebugOut((DEB_DLLS16, "CompObj16: DllEntryPoint Call32Initialize failed\n"));
  223. return FALSE;
  224. }
  225. etask.m_Dllinits = 1;
  226. etask.m_htask = GetCurrentProcess();
  227. etask.m_pMallocSBlock = NULL;
  228. etask.m_pMallocPrivate = NULL;
  229. etask.m_pid = 0;
  230. etask.m_inits = ETASK_NO_INIT;
  231. etask.m_oleinits = 0;
  232. etask.m_reserved = 0;
  233. etask.m_pDlls = NULL;
  234. etask.m_pMapToServerCO = NULL;
  235. etask.m_pMapToHandlerCO = NULL;
  236. etask.m_pArraySH = NULL;
  237. etask.m_pCThrd = NULL;
  238. etask.m_hwndClip = NULL;
  239. etask.m_hwndDde = NULL;
  240. etask.m_punkState = NULL;
  241. SetEtask(htask, etask);
  242. // this should be simplified
  243. // verify the initialzation
  244. etask.m_Dllinits = 0;
  245. etask.m_htask = 0;
  246. if ( !LookupEtask(htask, etask)
  247. || etask.m_Dllinits == 0
  248. || etask.m_htask != GetCurrentProcess() )
  249. {
  250. thkDebugOut((DEB_DLLS16, "CompObj16: DllEntryPoint Etask not initialized; Process (%X) done (%ld)\n", GetCurrentProcess(),dwReason));
  251. thkAssert(0 && "CompObj: DllEntryPoint Etask not initialized.");
  252. return FALSE;
  253. }
  254. thkDebugOut((DEB_DLLS16, "CompObj16: Added Etask\n"));
  255. }
  256. else
  257. {
  258. etask.m_Dllinits++;
  259. if (!SetEtask(htask, etask))
  260. {
  261. thkAssert(0 && "CompObj16: Etask could not be updated.");
  262. }
  263. else
  264. {
  265. thkDebugOut((DEB_DLLS16, "CompObj16: Updated Etask\n"));
  266. }
  267. }
  268. }
  269. else if (dwReason == DLL_UNLOAD) // dll refcount decrement
  270. {
  271. if (LookupEtask(htask, etask))
  272. {
  273. if (etask.m_Dllinits > 0)
  274. {
  275. etask.m_Dllinits--;
  276. if ( etask.m_Dllinits == 0 )
  277. {
  278. Call32Uninitialize();
  279. thkDebugOut((DEB_DLLS16, "CompObj16: Removing Etask\n"));
  280. thkVerify(RemoveEtask(htask,etask));
  281. }
  282. else
  283. {
  284. if (!SetEtask(htask, etask))
  285. {
  286. thkAssert(0 && "CompObj16: Etask could not be updated.");
  287. }
  288. else
  289. {
  290. thkDebugOut((DEB_DLLS16, "CompObj16: Updated Etask\n"));
  291. }
  292. }
  293. }
  294. else
  295. {
  296. thkAssert(0 && "Dllinit count invalid");
  297. }
  298. }
  299. else
  300. {
  301. thkAssert(0 && "DllEntryPoint on CompObj - no etask");
  302. }
  303. }
  304. else
  305. {
  306. thkAssert(0 && "Chicago BUG - Someone changed DllEntryPoint protocol");
  307. }
  308. thkDebugOut((DEB_DLLS16, "CompObj16: DllEntryPoint on Process (%X) done (%ld)\n", GetCurrentProcess(),dwReason));
  309. return TRUE;
  310. }
  311. #endif