How do I open an .exe from another C++ .exe?

209,485

Solution 1

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. If that doesn't scare you silly, check your pulse.
  • Anti virus programs hate it, your program could get flagged as a virus.

You should use CreateProcess().

You can use Createprocess() to just start up an .exe and creating a new process for it. The application will run independent from the calling application.

Here's an example I used in one of my projects:

#include <windows.h>

VOID startup(LPCTSTR lpApplicationName)
{
   // additional information
   STARTUPINFO 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
  CreateProcess( lpApplicationName,   // the path
    argv[1],        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi             // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
    );
    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

EDIT: The error you are getting is because you need to specify the path of the .exe file not just the name. Openfile.exe probably doesn't exist.

Solution 2

I've had great success with this:

#include <iostream>
#include <windows.h>

int main() {
    ShellExecute(NULL, "open", "path\\to\\file.exe", NULL, NULL, SW_SHOWDEFAULT);
}

If you're interested, the full documentation is here:

http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx.

Solution 3

Try this:

#include <windows.h>

int main ()

{
    system ("start notepad.exe") // As an example. Change [notepad] to any executable file //

    return 0 ;
}

Solution 4

Provide the full path of the file openfile.exe and remember not to put forward slash / in the path such as c:/users/username/etc.... instead of that use c:\\Users\\username\etc (for windows)

May be this will help you.

Solution 5

When executable path has whitespace in system, call

#include<iostream>
using namespace std;
int main()
{
    system("explorer C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe ");
    system("pause");
return 0;
}
Share:
209,485
S.Y
Author by

S.Y

Updated on September 07, 2021

Comments

  • S.Y
    S.Y over 2 years

    What I want to do is open an .exe from another .exe. I really don't know how to do this, so I searched the internet. I tried some suggested methods from the internet, but it didn't work.

    Here's my code:

    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    int main()
    {
        system ("OpenFile.exe");
        system ("pause");
        return 0;
    }
    

    When I run it in DEV C++, it compiles, but I get a error. Can someone please help me?

  • Nathan Tuggy
    Nathan Tuggy almost 9 years
    Could you please edit in an explanation of why this code answers the question? Code-only answers are discouraged, because they don't teach the solution.
  • Mogsdad
    Mogsdad over 8 years
    This doesn't make sense - are you asking what you should do if there is whitespace, or are you trying to explain what someone should do in that case?
  • Admin
    Admin over 8 years
    Yes, i try to explain! On this way, you can use "system", no <windows.h> function .
  • Admin
    Admin almost 8 years
    What's the difference between what you did here and what S.Y did?
  • 244an
    244an about 7 years
    There is a good example in msdn.microsoft.com/en-us/library/windows/desktop/… (at the end of the link in this answer, but easy to miss). And a little tip, if someone is using lpApplicationName with a exe-path that includes spaces, it should not be surrounded with ", only if both exe and command line is in argv[1].
  • Michael Haephrati
    Michael Haephrati over 6 years
    I think you must change the function from void to bool, and to return true/false based on the success of CreateProcess()
  • Michael Haephrati
    Michael Haephrati over 6 years
    CreateProcess() is better than ShellExecute()
  • user8951490
    user8951490 about 6 years
    Does CreateProcess launch a program as a background process (potentially keeping it open after it has finished executing)?
  • Charlie
    Charlie about 6 years
    short and direct, doesnt mean bad
  • Charlie
    Charlie about 6 years
    this is the better solution imo
  • user875234
    user875234 over 5 years
    system's syntax can be a little confusing if you have spaces in your path. It took me a while to craft this system("cmd /C \"\"C:\\Program Files (x86)\\My Cool App\\My Cool App.exe\"\"");. I based it off this answer stackoverflow.com/questions/9964865/…
  • David Lannan
    David Lannan over 5 years
    @Michael Thats a personal opinion - whats the validation? I think the opposite is true for a number of reasons: 1. ShellExecute is an OS function and can be properly protected. 2. ShellExecute is certainly more simplistic and will probably not change too much over time. CreateProcess - you should be using CreateProcessEx to be 'proper'. 3. There are many ambiguities about the use of CreateProcess, and for new users the security and flag settings are non-obvious. Im actually not sure what specific benefit CreateProcess does have? Please explain your rationale.
  • Michael Haephrati
    Michael Haephrati over 5 years
    CreateProcess is also an OS function. See docs.microsoft.com/en-us/windows/desktop/api/processthreadsa‌​pi/…. If you review software projects, I think you will find that CreateProcess is used in the more professional ones I guess because of your control over: LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, and also bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo and lpProcessInformation
  • Michael Haephrati
    Michael Haephrati over 5 years
    @DavidLannan, just had an opportunity to realize why CreateProcess is advanced over ShellExecuteEx and wanted to share that with you. If you wish to run a CMD command and wait for the command to be completed, the best way is to use CreateProcess() and then check the completion of the execution using these lines of code: WaitForSingleObject(ShExecInfo.hProcess, INFINITE); DWORD exitCode = 0; if (GetExitCodeProcess(ShExecInfo.hProcess, &exitCode) && exitCode != STILL_ACTIVE) { }
  • aj.toulan
    aj.toulan over 4 years
    I'm not sure I understand why you've put argv[1] as the second parameter of CreateProcess.
  • Digital Human
    Digital Human about 4 years
    Same here. It's confusing. @iBug can you clarify char argv[1] actually is? I assume command line parameters?
  • Ali Rojas
    Ali Rojas over 3 years
    @Jona how can I do the same in Ubuntu?
  • Azzurro94
    Azzurro94 about 3 years
    you better show how to invoke this function because now i am confused about LPCTSTR and string parameter.
  • Louis Go
    Louis Go about 2 years
    If you want to be compatible with WinExec which accepts exe and arguments in a variable. Put the commandline in second argument and make first argument NULL