| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : winprocess.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#include "winprocess.h" |
| 26 |
#ifdef __WXMSW__ |
| 27 |
#include "wx/filefn.h" |
| 28 |
|
| 29 |
#include <memory> |
| 30 |
|
| 31 |
/*static*/ |
| 32 |
WinProcess* WinProcess::Execute(const wxString& cmd, wxString& errMsg, const wxString& workingDir) |
| 33 |
{ |
| 34 |
SECURITY_ATTRIBUTES saAttr; |
| 35 |
BOOL fSuccess; |
| 36 |
|
| 37 |
wxString wd(workingDir); |
| 38 |
if(workingDir.IsEmpty()) { |
| 39 |
wd = wxGetCwd(); |
| 40 |
} |
| 41 |
|
| 42 |
// Set the bInheritHandle flag so pipe handles are inherited. |
| 43 |
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 44 |
saAttr.bInheritHandle = TRUE; |
| 45 |
saAttr.lpSecurityDescriptor = NULL; |
| 46 |
|
| 47 |
// The steps for redirecting child process's STDOUT: |
| 48 |
// 1. Save current STDOUT, to be restored later. |
| 49 |
// 2. Create anonymous pipe to be STDOUT for child process. |
| 50 |
// 3. Set STDOUT of the parent process to be write handle to |
| 51 |
// the pipe, so it is inherited by the child process. |
| 52 |
// 4. Create a noninheritable duplicate of the read handle and |
| 53 |
// close the inheritable read handle. |
| 54 |
|
| 55 |
WinProcess* prc = new WinProcess(); |
| 56 |
|
| 57 |
// Save the handle to the current STDOUT. |
| 58 |
prc->hSaveStdout = GetStdHandle(STD_OUTPUT_HANDLE); |
| 59 |
|
| 60 |
// Create a pipe for the child process's STDOUT. |
| 61 |
if(!CreatePipe(&prc->hChildStdoutRd, &prc->hChildStdoutWr, &saAttr, 0)) { |
| 62 |
delete prc; |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
// Set a write handle to the pipe to be STDOUT. |
| 67 |
if(!SetStdHandle(STD_OUTPUT_HANDLE, prc->hChildStdoutWr)) { |
| 68 |
delete prc; |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
// Create noninheritable read handle and close the inheritable read handle. |
| 73 |
fSuccess = DuplicateHandle(GetCurrentProcess(), prc->hChildStdoutRd, GetCurrentProcess(), &prc->hChildStdoutRdDup, |
| 74 |
0, FALSE, DUPLICATE_SAME_ACCESS); |
| 75 |
if(!fSuccess) { |
| 76 |
delete prc; |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
CloseHandle(prc->hChildStdoutRd); |
| 80 |
|
| 81 |
// The steps for redirecting child process's STDIN: |
| 82 |
// 1. Save current STDIN, to be restored later. |
| 83 |
// 2. Create anonymous pipe to be STDIN for child process. |
| 84 |
// 3. Set STDIN of the parent to be the read handle to the |
| 85 |
// pipe, so it is inherited by the child process. |
| 86 |
// 4. Create a noninheritable duplicate of the write handle, |
| 87 |
// and close the inheritable write handle. |
| 88 |
|
| 89 |
// Save the handle to the current STDIN. |
| 90 |
prc->hSaveStdin = GetStdHandle(STD_INPUT_HANDLE); |
| 91 |
|
| 92 |
// Create a pipe for the child process's STDIN. |
| 93 |
if(!CreatePipe(&prc->hChildStdinRd, &prc->hChildStdinWr, &saAttr, 0)) { |
| 94 |
delete prc; |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
// Set a read handle to the pipe to be STDIN. |
| 98 |
if(!SetStdHandle(STD_INPUT_HANDLE, prc->hChildStdinRd)) { |
| 99 |
delete prc; |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
// Duplicate the write handle to the pipe so it is not inherited. |
| 103 |
fSuccess = DuplicateHandle(GetCurrentProcess(), prc->hChildStdinWr, GetCurrentProcess(), &prc->hChildStdinWrDup, 0, |
| 104 |
FALSE, // not inherited |
| 105 |
DUPLICATE_SAME_ACCESS); |
| 106 |
if(!fSuccess) { |
| 107 |
delete prc; |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
CloseHandle(prc->hChildStdinWr); |
| 111 |
|
| 112 |
// Execute the child process |
| 113 |
STARTUPINFO siStartInfo; |
| 114 |
|
| 115 |
// Set up members of STARTUPINFO structure. |
| 116 |
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); |
| 117 |
siStartInfo.cb = sizeof(STARTUPINFO); |
| 118 |
siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; |
| 119 |
siStartInfo.hStdInput = prc->hChildStdinRd; |
| 120 |
siStartInfo.hStdOutput = prc->hChildStdoutWr; |
| 121 |
siStartInfo.hStdError = prc->hChildStdoutWr; |
| 122 |
|
| 123 |
// Set the window to hide |
| 124 |
siStartInfo.wShowWindow = SW_HIDE; |
| 125 |
BOOL ret = CreateProcess(NULL, |
| 126 |
#if wxVERSION_NUMBER < 2900 |
| 127 |
(WCHAR*)cmd.GetData(), |
| 128 |
#else |
| 129 |
cmd.wchar_str(), // shell line execution command |
| 130 |
#endif |
| 131 |
NULL, // process security attributes |
| 132 |
NULL, // primary thread security attributes |
| 133 |
TRUE, // handles are inherited |
| 134 |
CREATE_NO_WINDOW, // creation flags |
| 135 |
NULL, // use parent's environment |
| 136 |
wd.c_str(), // CD to tmp dir |
| 137 |
&siStartInfo, // STARTUPINFO pointer |
| 138 |
&prc->piProcInfo); // receives PROCESS_INFORMATION |
| 139 |
if(ret) { |
| 140 |
prc->dwProcessId = prc->piProcInfo.dwProcessId; |
| 141 |
} else { |
| 142 |
int err = GetLastError(); |
| 143 |
wxUnusedVar(err); |
| 144 |
delete prc; |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
|
| 148 |
// After process creation, restore the saved STDIN and STDOUT. |
| 149 |
if(!SetStdHandle(STD_INPUT_HANDLE, prc->hSaveStdin)) { |
| 150 |
delete prc; |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
if(!SetStdHandle(STD_OUTPUT_HANDLE, prc->hSaveStdout)) { |
| 154 |
|
| 155 |
delete prc; |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
return prc; |
| 159 |
} |
| 160 |
|
| 161 |
WinProcess::WinProcess() |
| 162 |
{ |
| 163 |
hChildStdinRd = NULL; |
| 164 |
hChildStdoutWr = NULL; |
| 165 |
hChildStdinWrDup = NULL; |
| 166 |
hChildStdoutRdDup = NULL; |
| 167 |
piProcInfo.hProcess = NULL; |
| 168 |
piProcInfo.hThread = NULL; |
| 169 |
} |
| 170 |
|
| 171 |
WinProcess::~WinProcess() {} |
| 172 |
|
| 173 |
bool WinProcess::Read(wxString& buff) |
| 174 |
{ |
| 175 |
DWORD dwRead; |
| 176 |
DWORD dwMode; |
| 177 |
DWORD dwTimeout; |
| 178 |
char* chBuf = new char[65536 + 1]; // 64K should be sufficient buffer |
| 179 |
memset(chBuf, 0, 65536 + 1); |
| 180 |
|
| 181 |
std::unique_ptr<char> sp(chBuf); |
| 182 |
|
| 183 |
// Make the pipe to non-blocking mode |
| 184 |
dwMode = PIPE_READMODE_BYTE | PIPE_NOWAIT; |
| 185 |
dwTimeout = 1000; |
| 186 |
SetNamedPipeHandleState(hChildStdoutRdDup, &dwMode, NULL, |
| 187 |
&dwTimeout); // Timeout of 30 seconds |
| 188 |
if(ReadFile(hChildStdoutRdDup, chBuf, 65536, &dwRead, NULL) || dwRead == 0) { |
| 189 |
chBuf[dwRead / sizeof(char)] = 0; |
| 190 |
// printf("%s\n", chBuf); |
| 191 |
buff = wxString(chBuf, wxConvUTF8); |
| 192 |
if(buff.IsEmpty() && dwRead > 0) { |
| 193 |
// conversion failed |
| 194 |
buff = wxString::From8BitData(chBuf); |
| 195 |
} |
| 196 |
return true; |
| 197 |
} |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
bool WinProcess::Write(const wxString& buff) |
| 202 |
{ |
| 203 |
DWORD dwMode; |
| 204 |
char chBuf[4097]; |
| 205 |
|
| 206 |
wxString tmpCmd = buff; |
| 207 |
tmpCmd = tmpCmd.Trim().Trim(false); |
| 208 |
tmpCmd += wxT("\n"); |
| 209 |
|
| 210 |
strcpy(chBuf, tmpCmd.mb_str()); |
| 211 |
|
| 212 |
// Make the pipe to non-blocking mode |
| 213 |
dwMode = PIPE_READMODE_BYTE | PIPE_NOWAIT; |
| 214 |
SetNamedPipeHandleState(hChildStdinWrDup, &dwMode, NULL, |
| 215 |
NULL); // Timeout of 30 seconds |
| 216 |
DWORD dwWritten; |
| 217 |
if(!WriteFile(hChildStdinWrDup, chBuf, (unsigned long)strlen(chBuf), &dwWritten, NULL)) |
| 218 |
return false; |
| 219 |
return true; |
| 220 |
} |
| 221 |
|
| 222 |
bool WinProcess::IsAlive() |
| 223 |
{ |
| 224 |
DWORD dwExitCode; |
| 225 |
if(GetExitCodeProcess(piProcInfo.hProcess, &dwExitCode)) { |
| 226 |
if(dwExitCode == STILL_ACTIVE) |
| 227 |
return true; |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
void WinProcess::Cleanup() |
| 233 |
{ |
| 234 |
if(IsAlive()) |
| 235 |
TerminateProcess(piProcInfo.hProcess, 255); |
| 236 |
|
| 237 |
CloseHandle(hChildStdinRd); |
| 238 |
CloseHandle(hChildStdoutWr); |
| 239 |
CloseHandle(hChildStdinWrDup); |
| 240 |
CloseHandle(hChildStdoutRdDup); |
| 241 |
CloseHandle(piProcInfo.hProcess); |
| 242 |
CloseHandle(piProcInfo.hThread); |
| 243 |
|
| 244 |
hChildStdinRd = NULL; |
| 245 |
hChildStdoutWr = NULL; |
| 246 |
hChildStdinWrDup = NULL; |
| 247 |
hChildStdoutRdDup = NULL; |
| 248 |
piProcInfo.hProcess = NULL; |
| 249 |
piProcInfo.hThread = NULL; |
| 250 |
} |
| 251 |
|
| 252 |
#endif //__WXMSW__ |