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
|
void TParseFile::ExecuteCommand(AnsiString strCmd)
{
const int BUFSIZE = 256;
AnsiString strRes, procstdout = "", procstderr = "";
char *CommandLine;
unsigned long dwExitCode;
CHAR chBuf[BUFSIZE];
DWORD dwRead;
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
HANDLE PipeInputRead, PipeInputWrite, PipeOutputRead, PipeOutputWrite;
SECURITY_ATTRIBUTES securityattribs = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
ZeroMemory(&siStartupInfo, sizeof(siStartupInfo));
CreatePipe(&PipeOutputRead, &PipeOutputWrite, &securityattribs, 0);
CreatePipe(&PipeInputRead, &PipeInputWrite, &securityattribs, 0);
siStartupInfo.cb = sizeof(siStartupInfo);
// Redirection vers un fichier temp désactivée
//strCmd = strCmd + " > " + ResultTempFile;
CommandLine = strCmd.c_str();
// Masquer la fenêtre, mais ne récupére par le résultat
/*
siStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
siStartupInfo.hStdInput = PipeInputRead;
siStartupInfo.hStdOutput = PipeOutputWrite;
siStartupInfo.hStdError = PipeOutputWrite;
siStartupInfo.wShowWindow = SW_HIDE;
*/
// Afficher la fenêtre et récupérer le résultat
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);
WaitForSingleObject(piProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
if (dwExitCode != STILL_ACTIVE)
CloseHandle(piProcessInfo.hProcess); // fermer le handle de process
CloseHandle(PipeOutputWrite);
CloseHandle(PipeInputRead);
CloseHandle(PipeInputWrite);
while (ReadFile(PipeOutputRead, chBuf, BUFSIZE-1, &dwRead, NULL) && (dwRead != 0))
{
chBuf[dwRead] = '\0';
procstdout += chBuf;
strRes = chBuf;
}
while (ReadFile(PipeOutputRead, chBuf, BUFSIZE-1, &dwRead, NULL) && (dwRead != 0))
{
chBuf[dwRead] = '\0';
procstderr += chBuf;
}
CloseHandle(PipeOutputRead);
}
else
{
MessageBox(NULL, CommandLine, "Command Line Error", MB_OK|MB_ICONSTOP);
CloseHandle(PipeOutputWrite);
CloseHandle(PipeInputRead);
}
} |
Partager