Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

windows

exe

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?

like image 824
S.Y Avatar asked Mar 15 '13 15:03

S.Y


People also ask

How do I run an EXE file with another program?

When you type the name of the EXE file you want to open, Windows displays a list of the files it finds. Double-click on the EXE filename to open it. The program starts and displays its own window. Alternatively, right-click the EXE file name and select "Open" from the pop-up menu to start the program.

What is exe C?

An executable file (EXE file) is a computer file that contains an encoded sequence of instructions that the system can execute directly when the user clicks the file icon. Executable files commonly have an EXE file extension, but there are hundreds of other executable file formats.

How do I open an .EXE file?

After you download your desired exe on your Android phone, download and install Inno Setup Extractor from the Google Play Store, use a file browser to locate the exe file, and open that file with the app. Inno Setup Extractor will then extract the exe on your Android phone, and you can check out those files afterward.


4 Answers

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.

like image 139
Jona Avatar answered Nov 08 '22 21:11

Jona


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.

like image 32
Morpheus13 Avatar answered Nov 08 '22 21:11

Morpheus13


Try this:

#include <windows.h>

int main ()

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

    return 0 ;
}
like image 33
ILys Hdm Avatar answered Nov 08 '22 20:11

ILys Hdm


You are getting this error because you are not giving full path. (C:\Users...\file.exe) If you want to remove this error then either give full path or copy that application (you want to open) to the folder where your project(.exe) is present/saved.

#include <windows.h>
using namespace std;
int main()
{
  system ("start C:\\Users\\Folder\\chrome.exe https://www.stackoverflow.com"); //for opening stackoverflow through google chrome , if chorme.exe is in that folder..
  return 0;
}
like image 23
udit043 Avatar answered Nov 08 '22 21:11

udit043