Jean Claud Van Who
Jean Claud Van Who

Reputation: 71

Launching a stand-alone executable from C++

I have a programmable signal generator. It came with a .inf file and a .exe file. Double clicking the .exe file runs the signal generator systems application.

I would like to invoke this executable from a piece of VC++ code. I am using Shell Execute as follows.

std::string app = "C:\gen.exe";
ShellExecute(NULL,"open",NULL,app.c_str(),NULL,SW_SHOWNORMAL);

However, instead of launching the executable, the command pops open a file dialog.

Upon further experimenting with ShellExecute, I noticed that ShellExecute works for executable files in the Program Files directory, but fails when I have stand alone executables such as in the case I mentioned above. Can someone please explain how to launch using VC++ a standalone executable located in a random directory on the disk?

By Standalone executable, I mean that the binary distribution for the application was simply an executable file, and a couple of other files. I can launch the executable by simply double clicking it, however, I cannot launch it using ShellExecute.

Upvotes: 1

Views: 523

Answers (1)

Brian Gradin
Brian Gradin

Reputation: 2275

If you take a look at the documentation for ShellExecute (link), it seems like you might have the reference to app in the wrong parameter. The fourth parameter is the parameters parameter - not the parameter specifying which file to open/run. So basically, this should work:

std::string app = "C:\\gen.exe";
ShellExecute(NULL,"open",app.c_str(),NULL,NULL,SW_SHOWNORMAL);

Alternatively, you could use CreateProcess or system("C:\\gen.exe");. I should point out that none of these options are portable to other OSs, but running a program probably won't be, however you end up doing it, since most OSs differ on how they run programs - or even what programs are.

Upvotes: 2

Related Questions