直接上代碼吧,有用過CSDN論壇說的WinExec()和system()等方法試過,好像都不太行,另外記得以前shellexec()也可以獲取程序返回值的,但是看了下函數好像沒有接收返回值的參數,只好用底下這種形式,用WaitForSingleObject()等待程序結束后用GetExitCodeProcess()獲取程序返回值
#include "stdafx.h" #include <Windows.h> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { STARTUPINFOW si; PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); ZeroMemory(&si, sizeof(si)); si.cb = sizeof(STARTUPINFOW); TCHAR cmd[256] = _T("D:\\dev\\YozoUCloud\\setup1.0.3 build437.exe"); BOOL working = ::CreateProcess(NULL, cmd, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi); if (working == 0) { DWORD error = GetLastError(); cout << "CreateProcess Error : " << error << endl; getchar(); return 0; } WaitForSingleObject(pi.hProcess, INFINITE); unsigned long Result; GetExitCodeProcess(pi.hProcess, &Result); cout << "Exit Code : " << Result << endl; getchar(); return 0; }