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
| wchar_t* __fastcall TForm5::RunDOScmd(wchar_t* CommandLine)
{
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
unsigned long dwExitCode;
HANDLE PipeInputRead;
HANDLE PipeInputWrite;
HANDLE PipeOutputRead;
HANDLE PipeOutputWrite;
SECURITY_ATTRIBUTES securityattribs =
{sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
ZeroMemory(&siStartupInfo, sizeof(siStartupInfo));
// initialisation de la taille
siStartupInfo.cb = sizeof(siStartupInfo);
// Create pipe for standard output redirection
CreatePipe(&PipeOutputRead, &PipeOutputWrite, &securityattribs, 0);
// Create pipe for standard input redirection.
CreatePipe(&PipeInputRead, &PipeInputWrite, &securityattribs, 0);
siStartupInfo.dwFlags = STARTF_USESTDHANDLES;
siStartupInfo.hStdInput = PipeInputRead;
siStartupInfo.hStdOutput = PipeOutputWrite;
siStartupInfo.hStdError = PipeOutputWrite;
bool pSuccess = CreateProcess(NULL, CommandLine, NULL, NULL, true, 0, NULL, NULL, &siStartupInfo, &piProcessInfo);
if (pSuccess)
{
CloseHandle(piProcessInfo.hThread); // fermer le handle de thread dès qu'il devient inutile
WaitForSingleObject(piProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
if (dwExitCode != STILL_ACTIVE)
CloseHandle(piProcessInfo.hProcess); // fermer le handle de process
}
else
{
CloseHandle(PipeOutputWrite);
CloseHandle(PipeInputRead);
return (L"perdu");
}
CloseHandle(PipeOutputWrite);
CloseHandle(PipeInputRead);
CloseHandle(PipeInputWrite);
// Read output from the child process.
DWORD dwRead;
CHAR chBuf[4096];
String procstdout = "", procstderr = "";
while (ReadFile(PipeOutputRead, chBuf, 4095, &dwRead, NULL) && (dwRead != 0))
{
chBuf[dwRead] = '\0';
procstdout += chBuf;
Edit2->Text = chBuf;
}
while (ReadFile(PipeOutputRead, chBuf, 4095, &dwRead, NULL) && (dwRead != 0))
{
chBuf[dwRead] = '\0';
procstderr += chBuf;
}
CloseHandle(PipeOutputRead);
} |
Partager