ThePrince
ThePrince

Reputation: 890

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

I want my C++ program to execute another .exe, in Windows. How would I do this? I am using 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;
}

Upvotes: 20

Views: 104294

Answers (5)

Dan
Dan

Reputation: 51

I believe this answer should work with different programs, I tested it with 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;
}

Upvotes: 3

James Stow
James Stow

Reputation: 461

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

  • 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.

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

Upvotes: 35

StevieG
StevieG

Reputation: 8709

you can use the system function

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

Upvotes: 19

sedavidw
sedavidw

Reputation: 11691

You can make a call using system

system("./some_command")

Upvotes: 5

Robert Mason
Robert Mason

Reputation: 4039

Use the CreateProcess() Function.

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

Upvotes: 12

Related Questions