2007年12月5日星期三

CreateProcess 能够返回错误码

STARTUPINFO si = { sizeof(STARTUPINFO) };

si.dwFlags = STARTF_USESHOWWINDOW;

si.wShowWindow = SW_HIDE;

PROCESS_INFORMATION pi;

CreateProcess(NULL, _T("app.exe"), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

if (showwait)
{
while( WaitForSingleObject(pi.hProcess, 5000)!=WAIT_OBJECT_0 )
{
if ( str.Find(_T("tool")) == -1 ) //str is not the execution of tool -wait
{
WinExec(CPublic::tool + "-wait4",SW_SHOWNORMAL);
}
}
}
else
{
WaitForSingleObject(pi.hProcess, INFINITE);
}

DWORD exit_code;

GetExitCodeProcess(pi.hProcess, &exit_code);

// TODO: check for errors

// TODO: free handles

我的代码:

BOOL CPublic::ProcessFiles(CString App, CString Command, BOOL showwait)
{
BOOL bProcess = false;
try
{
LPCTSTR lpApplicationName = NULL;
LPTSTR lpCommandLine;

Command = (LPCTSTR)(App + CString(" ") + Command);
//lpApplicationName = "\"" +App +"\"";
lpCommandLine = (char*)(LPCSTR) Command;

//TCHAR * lpCommandLine = new TCHAR();
//lpCommandLine = _tcsdup(TEXT("\"" +App + "\" " + Command));

STARTUPINFO si = { sizeof(STARTUPINFO) };

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

PROCESS_INFORMATION pi;

CreateProcess(lpApplicationName, lpCommandLine, NULL, NULL, FALSE, 0, NULL, (LPCTSTR)InstallDir, &si, &pi);
//CString str = GetCommandLine();
//AfxMessageBox(str);
if (showwait)
{
while( WaitForSingleObject(pi.hProcess, 5000)!=WAIT_OBJECT_0 )
{
if ( App.Find(_T("tool")) == -1 ) //str is not the execution of tool -wait
{
CPublic::ProcessMessages();
WinExec((LPCTSTR)(tool + CString("-wait4")),SW_SHOWNORMAL);
CPublic::ProcessMessages();

}

}

}
else
{
//WaitForSingleObject(pi.hProcess, INFINITE);
while( WaitForSingleObject(pi.hProcess, 500)!=WAIT_OBJECT_0 )
{
CPublic::ProcessMessages();


}

}
CPublic::ProcessMessages();
DWORD exit_code;

if ( GetExitCodeProcess(pi.hProcess, &exit_code) )
bProcess = true;


////////////////////////////////////////////////////////////////
//
// * edit by zengzn@gamil.com on 11.04.2008
//
// * "exit_code" is now known:
// 0: application success ran in win.
// 10: application terminates itself.
// 1: user terminate the process.
// ....
// * so we can judge that if a process was crashed or killed
// by user or self terminated
//
////////////////////////////////////////////////////////////////
//CString s;
//s.Format("%d",exit_code);
//AfxMessageBox(s);

if (exit_code != 10 && exit_code != 0)
bProcess = false;


// TODO: free handles

}catch(CException &e){
CString sMeldungsText;
char sErrorText[256];
e.GetErrorMessage(sErrorText,255);
sMeldungsText.Format("%s: %s%s","ProcessFiles()","ATAGAB/AKASSE -MakeTagab: Es kam zu einem allgemeinen Fehler at: ",sErrorText);
AfxMessageBox((LPCTSTR)sMeldungsText);

}
return bProcess;

}

没有评论: