1. 程式人生 > >VC啟動一個程式並等待執行結束

VC啟動一個程式並等待執行結束

在VC外掛裡執行了ARM ADS的編譯指令armlink和fromelf.

大致這樣的

WinExec("armlink .....", SW_HIDE);

WinExec("fromelf .....", SW_HIDE);

因為fromelf要用到armlink的輸出檔案,但不執行正確,一起跑提示fromelf找不到armlink輸出的那個檔案。

看來WinExec()是非同步執行armlink等命令了。肯定是建立程序去執行的,但是WinExec沒有等待子程序退出就返回了.

試過WinExec("armlink ....   && fromelf ....", SW_HIDE);但是最終還是要用C訪問fromelf的生成的檔案。

所以只能用CreateProcess()了

PROCESS_INFORMATION ProcessInfo;

STARTUPINFO StartupInfo; //入口引數

ZeroMemory(&StartupInfo, sizeof(StartupInfo));

StartupInfo.cb = sizeof StartupInfo ; //分配大小

if(CreateProcess("armlink", NULL,

NULL,NULL,FALSE,0,NULL,

NULL,&StartupInfo,&ProcessInfo))

{

WaitForSingleObject(ProcessInfo.hProcess,INFINITE);

CloseHandle(ProcessInfo.hThread);

CloseHandle(ProcessInfo.hProcess);

}

後來查到也可以用ShellExecuteEx()

SHELLEXECUTEINFO ShExecInfo = {0};

ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;

ShExecInfo.hwnd = NULL;

ShExecInfo.lpVerb = NULL;

ShExecInfo.lpFile = "armlink";

ShExecInfo.lpParameters = "。。。。。";

ShExecInfo.lpDirectory = NULL;

ShExecInfo.nShow = SW_HIDE;

ShExecInfo.hInstApp = NULL;

ShellExecuteEx(&ShExecInfo);

WaitForSingleObject(ShExecInfo.hProcess,INFINITE);