使用 C++ 中的参数运行可执行文件并获取返回值;

发布于 2024-08-06 08:31:40 字数 81 浏览 7 评论 0原文

如何运行带有从 C++ 程序传递的参数的可执行文件以及如何从中获取返回值?

像这样的东西: c:\myprogram.exe -v

How do you run an executable with parameters passed on it from a C++ program and how do you get the return value from it?

Something like this:
c:\myprogram.exe -v

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

避讳 2024-08-13 08:31:40

便携方式:

 int retCode = system("prog.exe arg1 arg2 arg3");

带有嵌入的引号/空格:

 int retCode = system("prog.exe \"arg 1\" arg2 arg3");

Portable way:

 int retCode = system("prog.exe arg1 arg2 arg3");

With embedded quotes/spaces:

 int retCode = system("prog.exe \"arg 1\" arg2 arg3");
离线来电— 2024-08-13 08:31:40

在 Windows 上,如果您想要对该过程有更多控制,可以使用 CreateProcess 生成进程,WaitForSingleObject 等待其退出,并 GetExitCodeProcess 获取返回码。

这种技术允许您控制子进程的输入和输出、其环境以及有关其运行方式的其他一些细节。

On Windows, if you want a bit more control over the process, you can use CreateProcess to spawn the process, WaitForSingleObject to wait for it to exit, and GetExitCodeProcess to get the return code.

This technique allows you to control the child process's input and output, its environment, and a few other bits and pieces about how it runs.

我最亲爱的 2024-08-13 08:31:40

问题
如何运行带有从 C++ 程序传递的参数的可执行文件?
解决方案
使用 ShellExecuteExSHELLEXECUTEINFO

问题
如何从中获取返回值?
解决方案
使用 GetExitCodeProcessexitCode

需要了解的基本知识
如果您想等到由外部exe处理的进程完成,则需要使用 WaitForSingleObject

bool ClassName::ExecuteExternalExeFileNGetReturnValue(Parameter ...)
{
    DWORD exitCode = 0;
    SHELLEXECUTEINFO ShExecInfo = {0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = _T("open");
    ShExecInfo.lpFile = _T("XXX.exe");        
    ShExecInfo.lpParameters = strParameter.c_str();   
    ShExecInfo.lpDirectory = strEXEPath.c_str();
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL; 
    ShellExecuteEx(&ShExecInfo);

    if(WaitForSingleObject(ShExecInfo.hProcess,INFINITE) == 0){             
        GetExitCodeProcess(ShExecInfo.hProcess, &exitCode);
        if(exitCode != 0){
            return false;
        }else{
            return true;
        }
    }else{
        return false;
    }
}

参考了解更多详情

Issue
How do you run an executable with parameters passed on it from a C++ program?
Solution
Use ShellExecuteEx and SHELLEXECUTEINFO

Issue
How do you get the return value from it?
Solution
Use GetExitCodeProcess and exitCode

Essential things to know
If you want to wait until process ,which is handling by external exe, is finished then need to use WaitForSingleObject

bool ClassName::ExecuteExternalExeFileNGetReturnValue(Parameter ...)
{
    DWORD exitCode = 0;
    SHELLEXECUTEINFO ShExecInfo = {0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = _T("open");
    ShExecInfo.lpFile = _T("XXX.exe");        
    ShExecInfo.lpParameters = strParameter.c_str();   
    ShExecInfo.lpDirectory = strEXEPath.c_str();
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL; 
    ShellExecuteEx(&ShExecInfo);

    if(WaitForSingleObject(ShExecInfo.hProcess,INFINITE) == 0){             
        GetExitCodeProcess(ShExecInfo.hProcess, &exitCode);
        if(exitCode != 0){
            return false;
        }else{
            return true;
        }
    }else{
        return false;
    }
}

Reference to know more detail

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文