【问题标题】:How to execute another exe from a C++ program in Windows如何在 Windows 中从 C++ 程序执行另一个 exe
【发布时间】:2013-07-17 15:17:23
【问题描述】:

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

这是我的代码

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

【问题讨论】:

标签: c++ windows


【解决方案1】:

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

  • 资源繁重
  • 它破坏了安全性——你不知道这是一个有效的命令,或者在每个系统上做同样的事情,你甚至可以启动你不打算启动的程序。危险在于,当您直接执行一个程序时,它会获得与您的程序相同的权限——这意味着,例如,如果您以系统管理员身份运行,那么您刚刚无意中执行的恶意程序也会以系统管理员身份运行。
  • 反病毒程序讨厌它,您的程序可能会被标记为病毒。

可以使用 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);
}

【讨论】:

  • 有时“argv”变量可能超出范围。我通常只是为另一个全局变量分配了 argv 的值并使用它来代替
【解决方案2】:

你可以使用system函数

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

【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

      您可以使用system拨打电话

      system("./some_command")
      

      【讨论】:

        【解决方案5】:

        我相信这个答案应该适用于不同的程序,我用 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;
        }
        

        【讨论】:

          【解决方案6】:

          ^argv[] 确实作为启动函数的参数。它需要作为以{ GetCommandLineA(); } 声明的聚合传入,而不是在使用函数时,而是在源代码的早期,以便实际初始化内存。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-01-15
            • 2014-06-10
            • 2020-10-20
            • 2013-03-04
            • 1970-01-01
            • 2011-07-24
            • 1970-01-01
            相关资源
            最近更新 更多