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
|
{
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = NULL;
HANDLE ReadPipeHandle;
HANDLE WritePipeHandle; // not used here
if (!CreatePipe(&ReadPipeHandle, &WritePipeHandle, &sa, 0))
RaiseLastOSError();
// Create a Console
STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdOutput = WritePipeHandle;
si.hStdError = WritePipeHandle;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
if (!CreateProcess(NULL,
"H:\\Python34\\python.exe H:\\Python34\\Tools\\Scripts\\diff.py",
NULL, NULL, true, 0, NULL, NULL, &si, &pi))
RaiseLastOSError();
// Read from pipe
char Data[1024];
for (; ; ) {
DWORD BytesRead;
DWORD TotalBytes;
DWORD BytesLeft;
// Check for the presence of data in the pipe
if (!PeekNamedPipe(ReadPipeHandle, Data, sizeof(Data), &BytesRead,
&TotalBytes, &BytesLeft))
RaiseLastOSError();
// If there is bytes, read them
if (BytesRead) {
if (!ReadFile(ReadPipeHandle, Data, sizeof(Data) - 1, &BytesRead,
NULL))
RaiseLastOSError();
Data[BytesRead] = '\0';
Memo1->Lines->Add(AnsiString(Data));
}
else {
// Is the console app terminated?
if (WaitForSingleObject(pi.hProcess, 0) == WAIT_OBJECT_0)
break;
}
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(ReadPipeHandle);
CloseHandle(WritePipeHandle);
} |
Partager