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

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


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.


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.


Try this:

#include <windows.h>int main (){    system ("start notepad.exe") // As an example. Change [notepad] to any executable file //    return 0 ;}