summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/ProcessFwd9x.c
blob: 7c03a68227d53301686a2c3550d76ac02bc59e30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*=========================================================================

Program:   KWSys - Kitware System Library
Module:    $RCSfile$
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
See http://www.cmake.org/HTML/Copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even 
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

/*
  On Windows9x platforms, this executable is spawned between a parent
  process and the child it is invoking to work around a bug.  See the
  Win32 implementation file for details.
*/

#ifdef _MSC_VER
#pragma warning (push, 1)
#endif
#include <windows.h>
#include <stdio.h>

int main()
{
  /* Process startup information for the real child.  */
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  
  /* The result of waiting for the child to exit.  */
  DWORD waitResult;
  
  /* The child's process return code.  */
  DWORD retVal;

  /* The command line used to invoke this process.  */
  LPSTR commandLine = GetCommandLine();

  /* Pointer that will be advanced to the beginning of the command
     line of the real child process.  */
  LPSTR cmdLine = commandLine;
  
  /* Handle to the error reporting pipe provided by the parent.  This
     is parsed off the command line.  */
  HANDLE errorPipe = 0;

  /* Handle to the event the parent uses to tell us to kill the child.
     This is parsed off the command line.  */
  HANDLE killEvent = 0;
  
  /* Flag for whether to hide window of child process.  */
  int hideWindow = 0;
  
  /* An array of the handles on which we wait when the child is
     running.  */
  HANDLE waitHandles[2] = {0, 0};
  
  /* Move the pointer past the name of this executable.  */
  if(*cmdLine == '"')
    {
    ++cmdLine;
    while(*cmdLine && *cmdLine != '"') { ++cmdLine; }
    if(*cmdLine) { ++cmdLine; }
    }
  else
    {
    while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
    }

  /* Parse the error pipe handle.  */
  while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  sscanf(cmdLine, "%p", &errorPipe);

  /* Parse the kill event handle.  */
  while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  sscanf(cmdLine, "%p", &killEvent);
  
  /* Parse the hide window flag.  */
  while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }
  sscanf(cmdLine, "%d", &hideWindow);
  
  /* Skip to the beginning of the command line of the real child.  */
  while(*cmdLine && *cmdLine != ' ') { ++cmdLine; }
  while(*cmdLine && *cmdLine == ' ') { ++cmdLine; }

  /* Create the subprocess.  */
  ZeroMemory(&si, sizeof(si));
  ZeroMemory(&pi, sizeof(pi));
  si.cb = sizeof(si);
  si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
  si.wShowWindow = hideWindow?SW_HIDE:SW_SHOWDEFAULT;
  si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  if(!CreateProcess(0, cmdLine, 0, 0, TRUE, 0, 0, 0, &si, &pi))
    {
    /* Error creating the process.  Report the error to the parent
       process through the special error reporting pipe.  */
    LPVOID lpMsgBuf;
    DWORD n;
    FormatMessage( 
      FORMAT_MESSAGE_ALLOCATE_BUFFER | 
      FORMAT_MESSAGE_FROM_SYSTEM | 
      FORMAT_MESSAGE_IGNORE_INSERTS,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPTSTR) &lpMsgBuf,
      0,
      NULL 
      );
    WriteFile(errorPipe, lpMsgBuf, strlen(lpMsgBuf)+1, &n, 0);
    LocalFree( lpMsgBuf );
    return 1;
    }
  CloseHandle(pi.hThread);  

  /* Wait for subprocess to exit or for kill event from parent.  */
  waitHandles[0] = killEvent;
  waitHandles[1] = pi.hProcess;
  waitResult = WaitForMultipleObjects(2, waitHandles, 0, INFINITE);

  /* Check what happened.  */
  if(waitResult == WAIT_OBJECT_0)
    {
    /* We were asked to kill the child.  */
    TerminateProcess(pi.hProcess, 255);
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    return 1;
    }
  else if(GetExitCodeProcess(pi.hProcess, &retVal))
    {
    /* The child exited and we could get the return code.  */
    CloseHandle(pi.hProcess);
    return retVal;
    }
  else
    {
    /* The child exited and we could not get the return code.  Report
       the problem to the parent process.  */
    DWORD n;
    const char* msg = "Failed to get process return code.";
    WriteFile(errorPipe, msg, strlen(msg)+1, &n, 0);
    CloseHandle(pi.hProcess);
    return -1;
    }
}