1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: ProcessFwd9x.c,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
13 =========================================================================*/
16 On Windows9x platforms, this executable is spawned between a parent
17 process and the child it is invoking to work around a bug. See the
18 Win32 implementation file for details.
20 Future Work: This executable must be linked statically against the C
21 runtime library before being encoded into the library. Building it
22 in this way may be hard because CMake has limited abilities to build
23 different targets with different configurations in the same
24 directory. We may just have to create and encode the executable
25 once instead of generating it during the build. This would be an
26 acceptable solution because the forwarding executable should not
27 change very often and is pretty simple.
31 #pragma warning (push, 1)
36 void ReportLastError(HANDLE errorPipe
);
40 /* Process startup information for the real child. */
42 PROCESS_INFORMATION pi
;
44 /* The result of waiting for the child to exit. */
47 /* The child's process return code. */
50 /* The command line used to invoke this process. */
51 LPSTR commandLine
= GetCommandLine();
53 /* Pointer that will be advanced to the beginning of the command
54 line of the real child process. */
55 LPSTR cmdLine
= commandLine
;
57 /* Handle to the error reporting pipe provided by the parent. This
58 is parsed off the command line. */
60 HANDLE errorPipeOrig
= 0;
62 /* Handle to the event the parent uses to tell us to resume the child.
63 This is parsed off the command line. */
64 HANDLE resumeEvent
= 0;
66 /* Handle to the event the parent uses to tell us to kill the child.
67 This is parsed off the command line. */
70 /* Flag for whether to hide window of child process. */
73 /* An array of the handles on which we wait when the child is
75 HANDLE waitHandles
[2] = {0, 0};
77 /* Move the pointer past the name of this executable. */
81 while(*cmdLine
&& *cmdLine
!= '"') { ++cmdLine
; }
82 if(*cmdLine
) { ++cmdLine
; }
86 while(*cmdLine
&& *cmdLine
!= ' ') { ++cmdLine
; }
89 /* Parse the error pipe handle. */
90 while(*cmdLine
&& *cmdLine
== ' ') { ++cmdLine
; }
91 sscanf(cmdLine
, "%p", &errorPipeOrig
);
93 /* Parse the resume event handle. */
94 while(*cmdLine
&& *cmdLine
!= ' ') { ++cmdLine
; }
95 while(*cmdLine
&& *cmdLine
== ' ') { ++cmdLine
; }
96 sscanf(cmdLine
, "%p", &resumeEvent
);
98 /* Parse the kill event handle. */
99 while(*cmdLine
&& *cmdLine
!= ' ') { ++cmdLine
; }
100 while(*cmdLine
&& *cmdLine
== ' ') { ++cmdLine
; }
101 sscanf(cmdLine
, "%p", &killEvent
);
103 /* Parse the hide window flag. */
104 while(*cmdLine
&& *cmdLine
!= ' ') { ++cmdLine
; }
105 while(*cmdLine
&& *cmdLine
== ' ') { ++cmdLine
; }
106 sscanf(cmdLine
, "%d", &hideWindow
);
108 /* Skip to the beginning of the command line of the real child. */
109 while(*cmdLine
&& *cmdLine
!= ' ') { ++cmdLine
; }
110 while(*cmdLine
&& *cmdLine
== ' ') { ++cmdLine
; }
112 /* Create a non-inherited copy of the error pipe. We do not want
113 the child to get it. */
114 if(DuplicateHandle(GetCurrentProcess(), errorPipeOrig
,
115 GetCurrentProcess(), &errorPipe
,
116 0, FALSE
, DUPLICATE_SAME_ACCESS
))
118 /* Have a non-inherited duplicate. Close the inherited one. */
119 CloseHandle(errorPipeOrig
);
123 /* Could not duplicate handle. Report the error. */
124 ReportLastError(errorPipeOrig
);
128 /* Create the subprocess. */
129 ZeroMemory(&si
, sizeof(si
));
130 ZeroMemory(&pi
, sizeof(pi
));
132 si
.dwFlags
= STARTF_USESTDHANDLES
|STARTF_USESHOWWINDOW
;
133 si
.wShowWindow
= hideWindow
?SW_HIDE
:SW_SHOWDEFAULT
;
134 si
.hStdInput
= GetStdHandle(STD_INPUT_HANDLE
);
135 si
.hStdOutput
= GetStdHandle(STD_OUTPUT_HANDLE
);
136 si
.hStdError
= GetStdHandle(STD_ERROR_HANDLE
);
137 if(CreateProcess(0, cmdLine
, 0, 0, TRUE
, CREATE_SUSPENDED
, 0, 0, &si
, &pi
))
139 /* Process created successfully. Close the error reporting pipe
140 to notify the parent of success. */
141 CloseHandle(errorPipe
);
145 /* Error creating the process. Report the error to the parent
146 process through the special error reporting pipe. */
147 ReportLastError(errorPipe
);
151 /* Wait for resume or kill event from parent. */
152 waitHandles
[0] = killEvent
;
153 waitHandles
[1] = resumeEvent
;
154 waitResult
= WaitForMultipleObjects(2, waitHandles
, 0, INFINITE
);
156 /* Check what happened. */
157 if(waitResult
== WAIT_OBJECT_0
)
159 /* We were asked to kill the child. */
160 TerminateProcess(pi
.hProcess
, 255);
161 WaitForSingleObject(pi
.hProcess
, INFINITE
);
162 CloseHandle(pi
.hProcess
);
163 CloseHandle(pi
.hThread
);
168 /* We were asked to resume the child. */
169 ResumeThread(pi
.hThread
);
170 CloseHandle(pi
.hThread
);
173 /* Wait for subprocess to exit or for kill event from parent. */
174 waitHandles
[0] = killEvent
;
175 waitHandles
[1] = pi
.hProcess
;
176 waitResult
= WaitForMultipleObjects(2, waitHandles
, 0, INFINITE
);
178 /* Check what happened. */
179 if(waitResult
== WAIT_OBJECT_0
)
181 /* We were asked to kill the child. */
182 TerminateProcess(pi
.hProcess
, 255);
183 WaitForSingleObject(pi
.hProcess
, INFINITE
);
184 CloseHandle(pi
.hProcess
);
189 /* The child exited. Get the return code. */
190 GetExitCodeProcess(pi
.hProcess
, &retVal
);
191 CloseHandle(pi
.hProcess
);
196 void ReportLastError(HANDLE errorPipe
)
201 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
202 FORMAT_MESSAGE_FROM_SYSTEM
|
203 FORMAT_MESSAGE_IGNORE_INSERTS
,
206 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
211 WriteFile(errorPipe
, lpMsgBuf
, strlen(lpMsgBuf
)+1, &n
, 0);
212 LocalFree( lpMsgBuf
);