パラメータを指定して外部プログラムを呼び出すにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/486087

質問

コード自体内で決定されたパラメーターを使用して、コード内で Windows プログラムを呼び出したいと考えています。

外部の関数やメソッドを呼び出すのではなく、WinXP 環境内の実際の .exe またはバッチ/スクリプト ファイルを呼び出すつもりです。

C または C++ が推奨言語ですが、他の言語 (ASM、C#、Python など) でより簡単に実行できる場合はお知らせください。

役に立ちましたか?

解決

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 (単に C:\Windows\system32\cmd.exe を使用しないでください)。

他のヒント

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
        }
    }

私はあなたがのCreateProcess WindowsのAPIの機能。そこ関連の呼び出しの家族が実際にあるが、これは、あなたが開始されます。それは非常に簡単です。

これを行う最も簡単な方法の 1 つは、 system() ランタイムライブラリ関数。単一の文字列をパラメータとして受け取ります(パラメータは CreateProcess!) を実行し、コマンド ラインに入力されたかのように実行します。 system() また、プロセスが終了するまで自動的に待機してから戻ります。

また、次のような制限もあります。

  • 起動されたプロセスの stdin と stdout をあまり制御できません
  • 他のプロセスの実行中は他のことをすることはできません(プロセスを強制終了するかどうかを決定するなど)
  • 何らかの方法でクエリを実行するために他のプロセスのハンドルを取得することはできません

ランタイム ライブラリには、次のファミリーも提供されます。 exec* 機能 (execl, execlp, execle, execv, execvp, 、多かれ少なかれ)、これらは Unix の伝統から派生しており、プロセスをより詳細に制御できます。

最低レベルでは、Win32 ではすべてのプロセスが CreateProcess 最も柔軟な機能を提供します。

単純な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 (); } }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top