Best Alternative to system() with example?

I was wondering what the BEST alternative is to start an application.
I'm using system() for the moment but I've read all over that system functions are ill-advised.
If any are multi-OS, that'd be excellent.

Example: To open up a Firefox browser window, I am using:
system("START firefox");
..if the user inputs "firefox".

How would that get converted over from system to another function?

- TruYadoo
On Windows you can use ShellExcetute() function
ShellExecute(0,"open","firefox",0,0,SW_SHOW);

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

I'm not sure how to do this on Linux so take a look at this thread: http://www.cplusplus.com/forum/unices/2047/#msg7731
Thank you. It worked perfectly plus everything is starting or "opening" a lot quicker.
Except Google Chrome still doesn't want to work as it didn't while using system()
I looked and the path to Google Chrome was "chrome" and I'm putting it in as I did with everything else.
It reads my request but it never opens.
Does Google Chrome just hate me?
How would I go about trying to open my folders such as 'My Documents' or 'My Pictures'?
I haven't been able to find anything that explains it. - plz & ty
ShellExecute(0,"open","explorer C:\\Users\\John's Desktop\\My Documents",0,0,SW_SHOW);
Nope. And I did forget about making that post. My apologies.
Everything is just starting to smash together. I need a break; from everything...
Last edited on
Once I do get it setup to open My Documents, how would I change it to open up the current User's My Documents if its on a different computer?
One of the following should work:

Windows 7
1
2
string path=string(getenv("userprofile"))+"\\Documents\\";
ShellExecute(0,"explore",path.c_str(),0,0,SW_SHOW);


Windows XP
1
2
string path=string(getenv("userprofile"))+"\\My Documents\\";
ShellExecute(0,"explore",path.c_str(),0,0,SW_SHOW);


Works on Windows 7, should work on XP too
1
2
3
4
5
6
7
8
9
#include <shlobj.h>
// ...
LPITEMIDLIST pidl;
SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);

char path[MAX_PATH];
SHGetPathFromIDList(pidl, path);

ShellExecute(0,"explore",path,0,0,SW_SHOW);
Last edited on
Wow, I had forgotten that I wrote that nifty little module. :O)

[Unix & Linux]

In any case, starting the browser on *nix systems is a little involved. This link is for Tcl/Tk, but it contains the important information about starting stuff on different systems. (Scroll down to the "Multi-Platform Solution".
http://wiki.tcl.tk/557

Also, this would also be a good case for using system(), methinks. (Just make sure to start your process in the background!)

[/Unix & Linux]

The link also has solutions for Mac.
@ Null, The top Windows 7 worked.
Last edited on
Topic archived. No new replies allowed.