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
|
bool GetConsoleOutput(String sCommandLine, TStringList *Output, TStringList *Errors)
{
STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInfo;
SECURITY_ATTRIBUTES SecurityAttr;
HANDLE PipeOutputRead, PipeOutputWrite, PipeErrorsRead, PipeErrorsWrite;
bool bSucceed, bResult;
char Buffer[255];
DWORD dwNumberOfBytesRead;
TMemoryStream *Stream;
//Initialisierung ProcessInfo
ZeroMemory( &ProcessInfo, sizeof(PROCESS_INFORMATION));
//Initialisierung SecurityAttr
ZeroMemory( &SecurityAttr, sizeof(SECURITY_ATTRIBUTES));
SecurityAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttr.bInheritHandle = TRUE;
SecurityAttr.lpSecurityDescriptor = NULL;
//Pipes erzeugen
if (!CreatePipe(&PipeOutputRead, &PipeOutputWrite, &SecurityAttr, 0))
return false;
if (!CreatePipe(&PipeErrorsRead, &PipeErrorsWrite, &SecurityAttr, 0))
return false;
//Initialisierung StartupInfo
ZeroMemory( &StartupInfo, sizeof(STARTUPINFO));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.hStdInput = 0;
StartupInfo.hStdOutput = PipeOutputWrite;
StartupInfo.hStdError = PipeErrorsWrite;
StartupInfo.wShowWindow = SW_HIDE;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if(CreateProcess(NULL, sCommandLine.c_str(), NULL, NULL,
TRUE, CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInfo))
{
bResult = true;
//Write-Pipes schließen
CloseHandle(PipeOutputWrite);
CloseHandle(PipeErrorsWrite);
//Ausgabe Read-Pipe auslesen
Stream = new TMemoryStream;
try
{
while (true)
{
bSucceed = ReadFile(PipeOutputRead, Buffer, 255, &dwNumberOfBytesRead, NULL);
if (!bSucceed) break;
Stream->Write(Buffer, dwNumberOfBytesRead);
}
Stream->Position = 0;
Output->LoadFromStream(Stream);
}
__finally
{
delete Stream;
}
CloseHandle(PipeOutputRead);
//Fehler Read-Pipe auslesen
Stream = new TMemoryStream;
try
{
while (true)
{
bSucceed = ReadFile(PipeErrorsRead, Buffer, 255, &dwNumberOfBytesRead, NULL);
if (!bSucceed) break;
Stream->Write(Buffer, dwNumberOfBytesRead);
}
Stream->Position = 0;
Errors->LoadFromStream(Stream);
}
__finally
{
delete Stream;
}
CloseHandle(PipeErrorsRead);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
}
else
{
bResult = false;
CloseHandle(PipeOutputRead);
CloseHandle(PipeOutputWrite);
CloseHandle(PipeErrorsRead);
CloseHandle(PipeErrorsWrite);
}
return bResult;
}
|