-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathChildProcess.cpp
202 lines (175 loc) · 4.8 KB
/
ChildProcess.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
** CDex - Open Source Digital Audio CD Extractor
**
** Copyright (C) 2006 - 2007 Georgy Berdyshev
** Copyright (C) 1999 Albert L. Faber
**
** http://cdexos.sourceforge.net/
** http://sourceforge.net/projects/cdexos
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "cdex.h"
#include "ChildProcess.h"
#include <io.h>
#include <fcntl.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChildProcess::CChildProcess(BOOL abConsole,
const CUString& strCommandLine,
DWORD aPriorityClass,
HANDLE hIn,
HANDLE hOut,
HANDLE hErr,
BOOL bHideWindow )
{
m_bConsole = abConsole ;
m_strCommandLine = strCommandLine ;
if ( aPriorityClass == IDLE_PRIORITY_CLASS ||
aPriorityClass == NORMAL_PRIORITY_CLASS ||
aPriorityClass == HIGH_PRIORITY_CLASS ||
aPriorityClass == REALTIME_PRIORITY_CLASS )
{
m_dwPriorityClass = aPriorityClass ;
}
else
{
m_dwPriorityClass = NORMAL_PRIORITY_CLASS ;
}
m_hStdin = hIn ;
m_hStdout = hOut ;
m_hStderr = hErr ;
m_hProcess = NULL ;
m_dwExitCode = STILL_ACTIVE ;
m_bHideWindow = bHideWindow;
}
CChildProcess::~CChildProcess()
{
// FIXME: Should we kill our child?
// normally child exits by itself, as we stop sending data,
// but this should not be taken for granted
if ( m_hProcess )
{
CloseHandle ( m_hProcess ) ;
}
}
BOOL CChildProcess::StartProcess()
{
BOOL bConsoleCreated = FALSE ;
BOOL rc = FALSE ;
BOOL bReturn = TRUE;
HWND hChildWindowHandle = NULL;
PROCESS_INFORMATION pi ;
STARTUPINFO sInfo ;
if ( m_bConsole )
{
if ( !m_bHideWindow )
{
// YOU ALREADY HAVE A CONSOLE - DELETE THIS?
bConsoleCreated = AllocConsole();
if (! bConsoleCreated)
{
// FIXME: Error message
bReturn = FALSE ;
}
else
{
// Get Window Handle of Child process console
TCHAR oldConsoleTitle[ 1024 ] = { _T( '\0' ),};
TCHAR newConsoleTitle[ 1024 ] = { _T( '\0' ),};
GetConsoleTitle( oldConsoleTitle, sizeof ( oldConsoleTitle ) );
_sntprintf( newConsoleTitle, sizeof ( newConsoleTitle ), _T( "CDex.ChildProcess.%u" ) , GetCurrentProcessId() );
SetConsoleTitle( newConsoleTitle );
hChildWindowHandle = FindWindow( NULL, newConsoleTitle );
SetConsoleTitle( oldConsoleTitle );
ShowWindow( hChildWindowHandle , SW_NORMAL );
}
}
if ( TRUE == bReturn )
{
if ( ! m_hStdin ) m_hStdin = GetStdHandle ( STD_INPUT_HANDLE ) ;
if ( ! m_hStdout ) m_hStdout = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
if ( ! m_hStderr ) m_hStderr = GetStdHandle ( STD_ERROR_HANDLE ) ;
}
}
if ( TRUE == bReturn )
{
memset( &sInfo, 0, sizeof ( sInfo ) ) ;
sInfo.cb = sizeof(STARTUPINFO);
sInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
sInfo.wShowWindow = m_bHideWindow ? SW_HIDE : SW_NORMAL ;
sInfo.hStdInput = m_hStdin ;
sInfo.hStdOutput = m_hStdout ;
sInfo.hStdError = m_hStderr ;
CUStringConvert strCnv;
rc = ::CreateProcess ( NULL,
strCnv.ToT( m_strCommandLine ),
NULL,
NULL,
TRUE,
m_dwPriorityClass,
NULL,
NULL,
&sInfo,
&pi ) ;
if ( !rc )
{
// FIXME: Error message
bReturn = FALSE ;
}
else
{
m_hProcess = pi.hProcess ;
}
}
if ( bConsoleCreated )
{
FreeConsole();
}
return bReturn;
}
// returns FALSE until Child process is running
BOOL CChildProcess::WaitForFinish(DWORD TimeOut)
{
BOOL rc;
rc = WaitForSingleObject( m_hProcess, TimeOut ) ;
switch ( rc )
{
case WAIT_OBJECT_0:
GetExitCodeProcess( m_hProcess, &m_dwExitCode ) ;
return TRUE ;
default:
return FALSE ;
}
}
// substitutes string 'From' with string 'To' in Child process
// command line
BOOL CChildProcess::SubstituteParameter( const CUString& strFrom, const CUString& strTo)
{
int nPos;
while ( ( nPos = m_strCommandLine.Find( strFrom ) ) >=0 )
{
m_strCommandLine = m_strCommandLine.Left( nPos ) +
strTo +
m_strCommandLine.Right( m_strCommandLine.GetLength() - nPos - strFrom.GetLength() );
}
return TRUE ;
}