shellexecute()

Can anyone give me an example of how to use shellexecute?
What about launching a program though? I want it to execute a program
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.
How does it know the path the the executable though?
It doesn't. You have to give it to it.
Where do I specify that? in the program.exe spot?
Also what is the best way to execute a program?
you asked one question in two place :
http://www.cplusplus.com/forum/beginner/25858/
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
Topic archived. No new replies allowed.