ProcessFwd9x.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. /*
  11. On Windows9x platforms, this executable is spawned between a parent
  12. process and the child it is invoking to work around a bug. See the
  13. Win32 implementation file for details.
  14. Future Work: This executable must be linked statically against the C
  15. runtime library before being encoded into the library. Building it
  16. in this way may be hard because CMake has limited abilities to build
  17. different targets with different configurations in the same
  18. directory. We may just have to create and encode the executable
  19. once instead of generating it during the build. This would be an
  20. acceptable solution because the forwarding executable should not
  21. change very often and is pretty simple.
  22. */
  23. #ifdef _MSC_VER
  24. #pragma warning (push, 1)
  25. #endif
  26. #include <windows.h>
  27. #include <stdio.h>
  28. void ReportLastError(HANDLE errorPipe);
  29. int main()
  30. {
  31. /* Process startup information for the real child. */
  32. STARTUPINFO si;
  33. PROCESS_INFORMATION pi;
  34. /* The result of waiting for the child to exit. */
  35. DWORD waitResult;
  36. /* The child's process return code. */
  37. DWORD retVal;
  38. /* The command line used to invoke this process. */
  39. LPSTR commandLine = GetCommandLine();
  40. /* Pointer that will be advanced to the beginning of the command
  41. line of the real child process. */
  42. LPSTR cmdLine = commandLine;
  43. /* Handle to the error reporting pipe provided by the parent. This
  44. is parsed off the command line. */
  45. HANDLE errorPipe = 0;
  46. HANDLE errorPipeOrig = 0;
  47. /* Handle to the event the parent uses to tell us to resume the child.
  48. This is parsed off the command line. */
  49. HANDLE resumeEvent = 0;
  50. /* Handle to the event the parent uses to tell us to kill the child.
  51. This is parsed off the command line. */
  52. HANDLE killEvent = 0;
  53. /* Flag for whether to hide window of child process. */
  54. int hideWindow = 0;
  55. /* An array of the handles on which we wait when the child is
  56. running. */
  57. HANDLE waitHandles[2] = {0, 0};
  58. /* Move the pointer past the name of this executable. */
  59. if(*cmdLine == '"')
  60. {
  61. ++cmdLine;
  62. while(*cmdLine && *cmdLine != '"') { ++cmdLine; }
  63. if(*cmdLine) { ++cmdLine; }
  64. }
  65. else
  66. {
  67. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  68. }
  69. /* Parse the error pipe handle. */
  70. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  71. sscanf(cmdLine, "%p", &errorPipeOrig);
  72. /* Parse the resume event handle. */
  73. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  74. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  75. sscanf(cmdLine, "%p", &resumeEvent);
  76. /* Parse the kill event handle. */
  77. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  78. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  79. sscanf(cmdLine, "%p", &killEvent);
  80. /* Parse the hide window flag. */
  81. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  82. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  83. sscanf(cmdLine, "%d", &hideWindow);
  84. /* Skip to the beginning of the command line of the real child. */
  85. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  86. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  87. /* Create a non-inherited copy of the error pipe. We do not want
  88. the child to get it. */
  89. if(DuplicateHandle(GetCurrentProcess(), errorPipeOrig,
  90. GetCurrentProcess(), &errorPipe,
  91. 0, FALSE, DUPLICATE_SAME_ACCESS))
  92. {
  93. /* Have a non-inherited duplicate. Close the inherited one. */
  94. CloseHandle(errorPipeOrig);
  95. }
  96. else
  97. {
  98. /* Could not duplicate handle. Report the error. */
  99. ReportLastError(errorPipeOrig);
  100. return 1;
  101. }
  102. /* Create the subprocess. */
  103. ZeroMemory(&si, sizeof(si));
  104. ZeroMemory(&pi, sizeof(pi));
  105. si.cb = sizeof(si);
  106. si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
  107. si.wShowWindow = hideWindow?SW_HIDE:SW_SHOWDEFAULT;
  108. si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  109. si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  110. si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  111. if(CreateProcess(0, cmdLine, 0, 0, TRUE, CREATE_SUSPENDED, 0, 0, &si, &pi))
  112. {
  113. /* Process created successfully. Close the error reporting pipe
  114. to notify the parent of success. */
  115. CloseHandle(errorPipe);
  116. }
  117. else
  118. {
  119. /* Error creating the process. Report the error to the parent
  120. process through the special error reporting pipe. */
  121. ReportLastError(errorPipe);
  122. return 1;
  123. }
  124. /* Wait for resume or kill event from parent. */
  125. waitHandles[0] = killEvent;
  126. waitHandles[1] = resumeEvent;
  127. waitResult = WaitForMultipleObjects(2, waitHandles, 0, INFINITE);
  128. /* Check what happened. */
  129. if(waitResult == WAIT_OBJECT_0)
  130. {
  131. /* We were asked to kill the child. */
  132. TerminateProcess(pi.hProcess, 255);
  133. WaitForSingleObject(pi.hProcess, INFINITE);
  134. CloseHandle(pi.hProcess);
  135. CloseHandle(pi.hThread);
  136. return 1;
  137. }
  138. else
  139. {
  140. /* We were asked to resume the child. */
  141. ResumeThread(pi.hThread);
  142. CloseHandle(pi.hThread);
  143. }
  144. /* Wait for subprocess to exit or for kill event from parent. */
  145. waitHandles[0] = killEvent;
  146. waitHandles[1] = pi.hProcess;
  147. waitResult = WaitForMultipleObjects(2, waitHandles, 0, INFINITE);
  148. /* Check what happened. */
  149. if(waitResult == WAIT_OBJECT_0)
  150. {
  151. /* We were asked to kill the child. */
  152. TerminateProcess(pi.hProcess, 255);
  153. WaitForSingleObject(pi.hProcess, INFINITE);
  154. CloseHandle(pi.hProcess);
  155. return 1;
  156. }
  157. else
  158. {
  159. /* The child exited. Get the return code. */
  160. GetExitCodeProcess(pi.hProcess, &retVal);
  161. CloseHandle(pi.hProcess);
  162. return retVal;
  163. }
  164. }
  165. void ReportLastError(HANDLE errorPipe)
  166. {
  167. LPVOID lpMsgBuf;
  168. DWORD n;
  169. FormatMessage(
  170. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  171. FORMAT_MESSAGE_FROM_SYSTEM |
  172. FORMAT_MESSAGE_IGNORE_INSERTS,
  173. NULL,
  174. GetLastError(),
  175. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  176. (LPTSTR) &lpMsgBuf,
  177. 0,
  178. NULL
  179. );
  180. WriteFile(errorPipe, lpMsgBuf, strlen(lpMsgBuf)+1, &n, 0);
  181. LocalFree( lpMsgBuf );
  182. }