ProcessFwd9x.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <windows.h>
  19. #include <stdio.h>
  20. int main()
  21. {
  22. /* Process startup information for the real child. */
  23. STARTUPINFO si;
  24. PROCESS_INFORMATION pi;
  25. /* The result of waiting for the child to exit. */
  26. DWORD waitResult;
  27. /* The child's process return code. */
  28. DWORD retVal;
  29. /* The command line used to invoke this process. */
  30. LPSTR commandLine = GetCommandLine();
  31. /* Pointer that will be advanced to the beginning of the command
  32. line of the real child process. */
  33. LPSTR cmdLine = commandLine;
  34. /* Handle to the error reporting pipe provided by the parent. This
  35. is parsed off the command line. */
  36. HANDLE errorPipe = 0;
  37. /* Handle to the event the parent uses to tell us to kill the child.
  38. This is parsed off the command line. */
  39. HANDLE killEvent = 0;
  40. /* An array of the handles on which we wait when the child is
  41. running. */
  42. HANDLE waitHandles[2] = {0, 0};
  43. /* Move the pointer past the name of this executable. */
  44. if(*cmdLine == '"')
  45. {
  46. ++cmdLine;
  47. while(*cmdLine && *cmdLine != '"') { ++cmdLine; }
  48. if(*cmdLine) { ++cmdLine; }
  49. }
  50. else
  51. {
  52. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  53. }
  54. /* Parse the error pipe handle. */
  55. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  56. sscanf(cmdLine, "%d", &errorPipe);
  57. /* Parse the kill event handle. */
  58. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  59. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  60. sscanf(cmdLine, "%d", &killEvent);
  61. /* Skip to the beginning of the command line of the real child. */
  62. while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  63. while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  64. /* Create the subprocess. */
  65. ZeroMemory(&si, sizeof(si));
  66. ZeroMemory(&pi, sizeof(pi));
  67. si.cb = sizeof(si);
  68. si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
  69. si.wShowWindow = SW_SHOWDEFAULT;
  70. si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  71. si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  72. si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  73. if(!CreateProcess(0, cmdLine, 0, 0, TRUE, 0, 0, 0, &si, &pi))
  74. {
  75. /* Error creating the process. Report the error to the parent
  76. process through the special error reporting pipe. */
  77. LPVOID lpMsgBuf;
  78. DWORD n;
  79. FormatMessage(
  80. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  81. FORMAT_MESSAGE_FROM_SYSTEM |
  82. FORMAT_MESSAGE_IGNORE_INSERTS,
  83. NULL,
  84. GetLastError(),
  85. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  86. (LPTSTR) &lpMsgBuf,
  87. 0,
  88. NULL
  89. );
  90. WriteFile(errorPipe, lpMsgBuf, strlen(lpMsgBuf)+1, &n, 0);
  91. LocalFree( lpMsgBuf );
  92. return 1;
  93. }
  94. CloseHandle(pi.hThread);
  95. /* Wait for subprocess to exit or for kill event from parent. */
  96. waitHandles[0] = killEvent;
  97. waitHandles[1] = pi.hProcess;
  98. waitResult = WaitForMultipleObjects(2, waitHandles, 0, INFINITE);
  99. /* Check what happened. */
  100. if(waitResult == WAIT_OBJECT_0)
  101. {
  102. /* We were asked to kill the child. */
  103. TerminateProcess(pi.hProcess, -1);
  104. WaitForSingleObject(pi.hProcess, INFINITE);
  105. CloseHandle(pi.hProcess);
  106. return 1;
  107. }
  108. else if(GetExitCodeProcess(pi.hProcess, &retVal))
  109. {
  110. /* The child exited and we could get the return code. */
  111. CloseHandle(pi.hProcess);
  112. return retVal;
  113. }
  114. else
  115. {
  116. /* The child exited and we could not get the return code. Report
  117. the problem to the parent process. */
  118. DWORD n;
  119. const char* msg = "Failed to get process return code.";
  120. WriteFile(errorPipe, msg, strlen(msg)+1, &n, 0);
  121. CloseHandle(pi.hProcess);
  122. return -1;
  123. }
  124. }