문제

나는 전화 윈도우 프로그램 내 코드와 매개 변수 결정에 코드 자체입니다.

내가 찾는 게 아니에요 전화를 외부 기능이나 방법이 아니지만,실제.exe 또는 배치/스크립트 파일 내 WinXP 환경입니다.

C 또는 C++것이 선호하는 언어로하지만 이 경우가 더 쉽게 다른 어떤 언어를 알려주(ASM,C#,Python,etc.).

도움이 되었습니까?

해결책

CreateProcess (), System () 등을 호출 할 때 파일 이름 (S) 및/또는 완전한 경로에 공간이있는 경우 파일 이름 문자열 (명령 프로그램 파일 이름 포함)을 두 번 인용해야합니다. 파일 이름 경로의 명령 통역사는 별도의 인수로 구문 분석됩니다.

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

Windows의 경우 CreateProcess ()를 사용하는 것이 좋습니다. Messier 설정이 있지만 프로세스가 시작되는 방식에 대해 더 많은 제어 기능을 제공합니다 (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는 시스템 경로에서 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에서 기능. 실제로 관련 전화 가족이 있지만 시작하게됩니다. 아주 쉽습니다.

가장 간단한 방법 중 하나 이를 위해 사용하는 것입 system() 런타임 라이브러리 함수입니다.그것은 하나의 문자열을 매개 변수로(적은 많은 매개 변수보다 CreateProcess!) 하고 실행하는 것처럼에서 입력한 명령 라인입니다. system() 또한 자동적으로 대기 위한 프로세스를 완료하기 전에 그것을 반환합니다.

거기에는 또한 제한 사항:

  • 당신이 덜어 stdin 과 stdout 의 출시 과정
  • 당신은 아무것도 할 수 없는 동안 다른 다른 프로세스를 실행하(와 같은 결정을 죽일)
  • 당신이 얻을 수 없는 손잡이를 다른 프로세스하기 위해서 쿼리에는 어떤 방법

런타임 라이브러리는 또한 제공합니다 가족의 exec* 능(execl, execlp, execle, execv, execvp, 더 많거나 적은)파생되는 유닉스에서 문화 유산 및 제공 프로세스에 대한 제어.

가장 낮은 수준에서,on 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