如何使用参数调用外部程序?

时间:2009-01-28 01:09:59

标签: c++ c winapi external-application

我想在我的代码中调用一个Windows程序,并在代码本身内确定参数。

我不打算在WinXP环境中调用外部函数或方法,而是调用实际的.exe或批处理/脚本文件。

C或C ++将是首选语言,但如果使用任何其他语言更容易理解,请告诉我(ASM,C#,Python等)。

5 个答案:

答案 0 :(得分:26)

当你调用CreateProcess(),System()等时,如果文件名和/或完全限定的路径有空格,请确保双引号文件名字符串(包括命令程序文件名)否则文件名路径的各个部分将由命令解释器解析为单独的参数。

system("\"d:some path\\program.exe\" \"d:\\other path\\file name.ext\"");

对于Windows,建议使用CreateProcess()。它设置较为混乱,但您可以更好地控制流程的启动方式(如Greg Hewgill所述)。对于快速和脏,您也可以使用WinExec()。 (system()可移植到UNIX)。

启动批处理文件时,您可能需要使用cmd.exe(或command.com)启动。

WinExec("cmd \"d:some path\\program.bat\" \"d:\\other path\\file name.ext\"",SW_SHOW_MINIMIZED);

(或SW_SHOW_NORMAL如果要显示命令窗口。)

Windows应该在系统PATH中找到command.com或cmd.exe,因此不需要完全限定,但如果您想确定可以使用CSIDL_SYSTEM撰写完全限定的文件名(don)不要只使用C:\ Windows \ system32 \ cmd.exe)。

答案 1 :(得分:11)

C ++示例:

char temp[512];
sprintf(temp, "command -%s -%s", parameter1, parameter2);
system((char *)temp);

C#示例:

    private static void RunCommandExample()
    {
        // Don't forget using System.Diagnostics
        Process myProcess = new Process();

        try
        {
            myProcess.StartInfo.FileName = "executabletorun.exe";

            //Do not receive an event when the process exits.
            myProcess.EnableRaisingEvents = false;

            // Parameters
            myProcess.StartInfo.Arguments = "/user testuser /otherparam ok";

            // Modify the following to hide / show the window
            myProcess.StartInfo.CreateNoWindow = false;
            myProcess.StartInfo.UseShellExecute = true;
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;

            myProcess.Start();

        }
        catch (Exception e)
        {
            // Handle error here
        }
    }

答案 2 :(得分:8)

我认为您正在寻找Windows API中的CreateProcess功能。实际上有一系列相关的电话,但这会让你开始。这很容易。

答案 3 :(得分:5)

执行此操作的最简单方法之一是使用system()运行时库函数。它需要一个字符串作为参数(比CreateProcess少得多的参数!)并执行它,好像它是在命令行上键入的一样。 system()也会在返回之前自动等待进程完成。

还有一些限制:

  • 您对已启动流程的stdin和stdout的控制较少
  • 当其他进程正在运行时(例如决定杀死它),你不能做任何其他事情。
  • 您无法获取其他进程的句柄以便以任何方式查询

运行时库还提供了一系列exec*函数(execlexeclpexecleexecvexecvp,更多或更少)源自Unix遗产并提供对流程的更多控制。

在最低级别,在Win32上,所有进程都由CreateProcess函数启动,这为您提供了最大的灵活性。

答案 4 :(得分:0)

简单的c ++示例(在搜索了几个网站后发现)

#include <bits/stdc++.h>
#include <cassert>
#include <exception>
#include <iostream>

int main (const int argc, const char **argv) {
try {
    assert (argc == 2);
    const std::string filename = (const std::string) argv [1];
    const std::string begin = "g++-7 " + filename;
    const std::string end = " -Wall -Werror -Wfatal-errors -O3 -std=c++14 -o a.elf -L/usr/lib/x86_64-linux-gnu";
    const std::string command = begin + end;
    std::cout << "Compiling file using " << command << '\n';

    assert (std::system ((const char *) command.c_str ()) == 0);
    std::cout << "Running file a.elf" << '\n';
    assert (std::system ((const char *) "./a.elf") == 0);

    return 0; }
catch (std::exception const& e) { std::cerr << e.what () << '\n'; std::terminate (); }
catch (...) { std::cerr << "Found an unknown exception." << '\n'; std::terminate (); } }