如何从 Windows 中的 C++ 程序执行另一个 exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17703721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:22:24  来源:igfitidea点击:

How to execute another exe from a C++ program in Windows

c++windows

提问by ThePrince

I want my C++ program to execute another .exe, in Windows. How would I do this? I am using Visual C++ 2010.

我希望我的 C++ 程序在 Windows 中执行另一个 .exe。我该怎么做?我正在使用 Visual C++ 2010。

Here is my code

这是我的代码

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    unsigned int input;
    cout << "Enter 1 to execute program." << endl;
    cin >> input;
    if(input == 1) /*execute program here*/;
    return 0;
}

回答by James Stow

This is a solution I found when looking for an answer previously.
It stated that you should always avoid using system() because:

这是我之前在寻找答案时找到的解决方案。
它指出您应该始终避免使用 system() ,因为:

  • It is resource heavy
  • It defeats security -- you don't know you it's a valid command or does the same thing on every system, you could even start up programs you didn't intend to start up. The danger is that when you directly execute a program, it gets the same privileges as your program -- meaning that if, for example, you are running as system administrator then the malicious program you just inadvertently executed is also running as system administrator.
  • Anti virus programs hate it, your program could get flagged as a virus.
  • 资源繁重
  • 它破坏了安全性——你不知道这是一个有效的命令,或者在每个系统上做同样的事情,你甚至可以启动你不打算启动的程序。危险在于,当您直接执行程序时,它会获得与您的程序相同的权限——这意味着,例如,如果您以系统管理员身份运行,那么您无意中执行的恶意程序也会以系统管理员身份运行。
  • 防病毒程序讨厌它,您的程序可能会被标记为病毒。

Instead CreateProcess() can be used.
Createprocess() is used to just start up an .exe and creating a new process for it. The application will run independent from the calling application.

相反,可以使用 CreateProcess()。
Createprocess() 用于启动一个 .exe 并为其创建一个新进程。应用程序将独立于调用应用程序运行。

#include <Windows.h>

void startup(LPCSTR lpApplicationName)
{
    // additional information
    STARTUPINFOA si;
    PROCESS_INFORMATION pi;

    // set the size of the structures
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    // start the program up
    CreateProcessA
    (
        lpApplicationName,   // the path
        argv[1],                // Command line
        NULL,                   // Process handle not inheritable
        NULL,                   // Thread handle not inheritable
        FALSE,                  // Set handle inheritance to FALSE
        CREATE_NEW_CONSOLE,     // Opens file in a separate console
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi           // Pointer to PROCESS_INFORMATION structure
    );
        // Close process and thread handles. 
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
}

回答by StevieG

you can use the systemfunction

你可以使用这个system功能

int result = system("C:\Program Files\Program.exe");

回答by Robert Mason

Use the CreateProcess() Function.

使用 CreateProcess() 函数。

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspxfor details

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx

回答by sedavidw

You can make a call using system

您可以使用 system

system("./some_command")

回答by Dan

I believe this answer should work with different programs, I tested it with Chrome.

我相信这个答案应该适用于不同的程序,我用 Chrome 对其进行了测试。

// open program.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string command = "start chrome https://www.google.com/";
    system(command.c_str());

    return 0;
}