int CallProcess(wstring applicationName, wstring command, string* output, DWORD* errorLevel)
{
HANDLE hReadPipe;
HANDLE hWritePipe;
SECURITY_ATTRIBUTES sat;
sat.nLength = sizeof(SECURITY_ATTRIBUTES);
sat.bInheritHandle = true;
sat.lpSecurityDescriptor = NULL;
if( !CreatePipe(&hReadPipe, &hWritePipe, &sat, NULL) )
{
cout << "CreatePipe fail" << endl;
return -1;
}
PROCESS_INFORMATION pinfo;
STARTUPINFO startupinfo;
startupinfo.cb = sizeof(STARTUPINFO);
GetStartupInfo(&startupinfo);
startupinfo.hStdOutput = hWritePipe;
startupinfo.hStdError = hWritePipe;
startupinfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
startupinfo.wShowWindow = SW_HIDE;
wstring orgCommand;
orgCommand.append(L"\"").append( applicationName).append(L"\" ").append(command);
if(!CreateProcess(NULL, (WCHAR *)orgCommand.c_str() , NULL, NULL, TRUE, NULL, NULL, NULL, &startupinfo, &pinfo))
{
cout << "Call Process fail" << endl;
return -1;
}
BOOL processState = GetExitCodeProcess(pinfo.hProcess, errorLevel);
BYTE buffer[1024];
DWORD byteRead;
while(true)
{
DWORD osize, exitCode;
DWORD size =GetFileSize(hReadPipe, &osize);
GetExitCodeProcess( pinfo.hProcess, &exitCode);
if (0!=size)
{
ReadFile(hReadPipe,buffer,1024,&byteRead,NULL);
string temp((char*)buffer,byteRead);
*output += temp;
}
else if(0==size && STILL_ACTIVE ==exitCode )
{
break;
}
Sleep(3000);
}
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
return 0;
}
浙公网安备 33010602011771号