ProcessFwd9x.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. /*
  14. On Windows9x platforms, this executable is spawned between a parent
  15. process and the child it is invoking to work around a bug. See the
  16. Win32 implementation file for details.
  17. */
  18. #ifdef _MSC_VER
  19. #pragma warning (push, 1)
  20. #endif
  21. #include <windows.h>
  22. #include <stdio.h>
  23. int main()
  24. {
  25. /* Process startup information for the real child. */
  26. STARTUPINFO si;
  27. PROCESS_INFORMATION pi;
  28. /* The result of waiting for the child to exit. */
  29. DWORD waitResult;
  30. /* The child's process return code. */
  31. DWORD retVal;
  32. /* The command line used to invoke this process. */
  33. LPSTR commandLine = GetCommandLine();
  34. /* Pointer that will be advanced to the beginning of the command
  35. line of the real child process. */
  36. LPSTR cmdLine = commandLine;
  37. /* Handle to the error reporting pipe provided by the parent. This
  38. is parsed off the command line. */
  39. HANDLE errorPipe = 0;
  40. /* Handle to the event the parent uses to tell us to kill the child.
  41. This is parsed off the command line. */
  42. HANDLE killEvent = 0;
  43. /* An array of the handles on which we wait when the child is
  44. running. */
  45. HANDLE waitHandles[2] = {0, 0};
  46. /* Move the pointer past the name of this executable. */
  47. if(*cmdLine == '"')
  48. {
  49. ++cmdLine;
  50. while(*cmdLine && *cmdLine != '"') { ++cmdLine; }
  51. if(*cmdLine) { ++cmdLine; }
  52. }
  53. else
  54. {
  55. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  56. }
  57. /* Parse the error pipe handle. */
  58. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  59. sscanf(cmdLine, "%p", &errorPipe);
  60. /* Parse the kill event handle. */
  61. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  62. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  63. sscanf(cmdLine, "%p", &killEvent);
  64. /* Skip to the beginning of the command line of the real child. */
  65. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  66. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  67. /* Create the subprocess. */
  68. ZeroMemory(&si, sizeof(si));
  69. ZeroMemory(&pi, sizeof(pi));
  70. si.cb = sizeof(si);
  71. si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
  72. si.wShowWindow = SW_SHOWDEFAULT;
  73. si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  74. si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  75. si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  76. if(!CreateProcess(0, cmdLine, 0, 0, TRUE, 0, 0, 0, &si, &pi))
  77. {
  78. /* Error creating the process. Report the error to the parent
  79. process through the special error reporting pipe. */
  80. LPVOID lpMsgBuf;
  81. DWORD n;
  82. FormatMessage(
  83. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  84. FORMAT_MESSAGE_FROM_SYSTEM |
  85. FORMAT_MESSAGE_IGNORE_INSERTS,
  86. NULL,
  87. GetLastError(),
  88. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  89. (LPTSTR) &lpMsgBuf,
  90. 0,
  91. NULL
  92. );
  93. WriteFile(errorPipe, lpMsgBuf, strlen(lpMsgBuf)+1, &n, 0);
  94. LocalFree( lpMsgBuf );
  95. return 1;
  96. }
  97. CloseHandle(pi.hThread);
  98. /* Wait for subprocess to exit or for kill event from parent. */
  99. waitHandles[0] = killEvent;
  100. waitHandles[1] = pi.hProcess;
  101. waitResult = WaitForMultipleObjects(2, waitHandles, 0, INFINITE);
  102. /* Check what happened. */
  103. if(waitResult == WAIT_OBJECT_0)
  104. {
  105. /* We were asked to kill the child. */
  106. TerminateProcess(pi.hProcess, 255);
  107. WaitForSingleObject(pi.hProcess, INFINITE);
  108. CloseHandle(pi.hProcess);
  109. return 1;
  110. }
  111. else if(GetExitCodeProcess(pi.hProcess, &retVal))
  112. {
  113. /* The child exited and we could get the return code. */
  114. CloseHandle(pi.hProcess);
  115. return retVal;
  116. }
  117. else
  118. {
  119. /* The child exited and we could not get the return code. Report
  120. the problem to the parent process. */
  121. DWORD n;
  122. const char* msg = "Failed to get process return code.";
  123. WriteFile(errorPipe, msg, strlen(msg)+1, &n, 0);
  124. CloseHandle(pi.hProcess);
  125. return -1;
  126. }
  127. }