C++获取程序返回值

直接上代码吧,有用过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;
}

 

posted @ 2020-08-19 10:04  Suarezz  阅读(1302)  评论(0编辑  收藏  举报