shellexecute()

Jul 8, 2010 at 4:34am
Can anyone give me an example of how to use shellexecute?
Jul 8, 2010 at 4:45am
Jul 8, 2010 at 4:54am
What about launching a program though? I want it to execute a program
Jul 8, 2010 at 4:59am
Using the documentation, I would guess you do something like this:
ShellExecute(NULL, "open", "program.exe", NULL, NULL, SW_SHOWDEFAULT);
I have no idea if this works though.
Jul 8, 2010 at 5:00am
How does it know the path the the executable though?
Jul 8, 2010 at 5:03am
It doesn't. You have to give it to it.
Jul 8, 2010 at 5:04am
Where do I specify that? in the program.exe spot?
Jul 8, 2010 at 5:35am
Also what is the best way to execute a program?
Jul 8, 2010 at 2:23pm
you asked one question in two place :
http://www.cplusplus.com/forum/beginner/25858/
Jul 9, 2010 at 10:24am
You can omit the full path to the file and have just the filename, if and only if, the specified file is in a directory defined in the environment variable %PATH%. This includes anything in %windir% (windows\system32). So you could do ShellExecute(NULL, "open", "notepad.exe", NULL, NULL, SW_SHOWDEFAULT); or calc.exe, etc. Anything not in a dir in the PATH, you will need to specify the entire path. You can also add a directory to PATH from your program, but you shouldn't do that unless it's really necessary. When it comes to programs, they should only change the system as much as is absolutely necessary to fulfill their purpose. In other words, you (as the programmer) don't mess with the user's system or configuration, etc, unless you have to, and if you can do it without modifying anything, then do. The less invasive your program is the better.

Anyway, back to the main subject...
As for your question of what's the best way to execute an external program, I assume you have no intention of making your program cross-platform, since executing an external program is pretty much platform dependent, so ShellExecute is probably the best way to do it in Windows. You could also use a system() call, but I think that native API calls are more efficient, which is what ShellExecute is..
Last edited on Jul 9, 2010 at 10:26am
Topic archived. No new replies allowed.