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.

206 lines
4.0 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1994 **/
  4. /**********************************************************************/
  5. /*
  6. olehack.cxx
  7. This module contains the code that will allow single threaded ole apps
  8. to run in the Gibraltar service.
  9. FILE HISTORY:
  10. Johnl 22-Sep-1994 Created
  11. */
  12. #include "w3p.hxx"
  13. #include <objbase.h> // for CoInitialize/Uninitialize
  14. HANDLE hOleHackEvent = NULL;
  15. HANDLE hOleHackThread = NULL;
  16. //
  17. // Private constants.
  18. //
  19. //
  20. // Time to wait for the ole thread to exit
  21. //
  22. #define WAIT_FOR_OLE_THREAD (30 * 1000)
  23. //
  24. // Private prototypes
  25. //
  26. DWORD
  27. OleHackThread(
  28. PVOID pv
  29. );
  30. DWORD
  31. InitializeOleHack(
  32. VOID
  33. )
  34. /*++
  35. Routine Description:
  36. Creates a main OLE thread so older OLE controls (including VB4) work
  37. Return Value:
  38. NO_ERROR on success
  39. --*/
  40. {
  41. DWORD idThread;
  42. //
  43. // Create the exit event
  44. //
  45. hOleHackEvent = IIS_CREATE_EVENT(
  46. "hOleHackEvent",
  47. &hOleHackEvent,
  48. TRUE,
  49. FALSE
  50. );
  51. if ( !hOleHackEvent )
  52. {
  53. DBGPRINTF(( DBG_CONTEXT,
  54. "Unable to create OleHack event[err %d]\n", GetLastError()));
  55. return GetLastError();
  56. }
  57. //
  58. // Create the main Ole thread
  59. //
  60. hOleHackThread = CreateThread( NULL,
  61. 0,
  62. (LPTHREAD_START_ROUTINE) OleHackThread,
  63. NULL,
  64. 0,
  65. &idThread );
  66. if ( !hOleHackThread )
  67. {
  68. DWORD err = GetLastError();
  69. DBGPRINTF(( DBG_CONTEXT,
  70. "Unable to create OleHack thread[err %d]\n", err));
  71. CloseHandle( hOleHackEvent );
  72. return err;
  73. }
  74. return NO_ERROR;
  75. }
  76. DWORD
  77. OleHackThread(
  78. PVOID pv
  79. )
  80. /*++
  81. Routine Description:
  82. Message loop thread for OLE controls
  83. --*/
  84. {
  85. HRESULT hr;
  86. MSG msg;
  87. DWORD ret;
  88. DBGPRINTF(( DBG_CONTEXT,
  89. "[OleHackThread] entered, thread ID %x\n",
  90. GetCurrentThreadId() ));
  91. hr = CoInitialize( NULL );
  92. if ( FAILED( hr ) )
  93. {
  94. DBGPRINTF(( DBG_CONTEXT,
  95. "[OleHackThread] CoInitialize failed with error %lx\n",
  96. hr ));
  97. return 0;
  98. }
  99. while ( TRUE )
  100. {
  101. ret = MsgWaitForMultipleObjects( 1,
  102. &hOleHackEvent,
  103. FALSE,
  104. INFINITE,
  105. QS_ALLINPUT );
  106. if ( ret == WAIT_OBJECT_0 )
  107. {
  108. break;
  109. }
  110. while ( PeekMessage( &msg,
  111. NULL,
  112. 0,
  113. 0,
  114. PM_REMOVE ))
  115. {
  116. DispatchMessage( &msg );
  117. }
  118. }
  119. DBGPRINTF(( DBG_CONTEXT,
  120. "[OleHackThread] loop exited, calling CoUnitialize\n" ));
  121. CoUninitialize();
  122. DBGPRINTF(( DBG_CONTEXT,
  123. "[OleHackThread] Exiting thread\n" ));
  124. return 0;
  125. }
  126. VOID
  127. TerminateOleHack(
  128. VOID
  129. )
  130. /*++
  131. Routine Description:
  132. Terminates the ole thread
  133. --*/
  134. {
  135. //
  136. // Make sure we've been initialized
  137. //
  138. if ( !hOleHackEvent )
  139. return;
  140. //
  141. // Signal the thread then wait for it to exit
  142. //
  143. DBG_REQUIRE( SetEvent( hOleHackEvent ));
  144. if ( WAIT_TIMEOUT == WaitForSingleObject( hOleHackThread,
  145. WAIT_FOR_OLE_THREAD ))
  146. {
  147. DBGPRINTF(( DBG_CONTEXT,
  148. "[OleHackTerminate] Warning - WaitForSingleObject timed out\n" ));
  149. }
  150. DBG_REQUIRE( CloseHandle( hOleHackEvent ) );
  151. DBG_REQUIRE( CloseHandle( hOleHackThread ));
  152. }