tags:

views:

298

answers:

5

I have the following code

void reportResults() 
  {
    wstring env(_wgetenv(L"ProgramFiles"));
    env += L"\Internet Explorer\iexplore.exe";
    wstringstream url;
    url << "\"\"" << env.c_str() << "\"  http://yahoo.com\"";
    wchar_t arg[BUFSIZE];
    url.get(arg, BUFSIZE);
    wcout << arg << endl;
    _wsystem(arg);
  }

Where arg is: ""C:\Program Files\Internet Explorer\iexplore.exe" http://yahoo.com"

The program functions as expected, launching IE and navigating to Yahoo, but the calling function (reportResults) never exits. How do I get the program to exit leaving the browser alive? Thanks.

+7  A: 

You want to use _wspawn() instead of _wsystem(). This will spawn a new process for the browser process. _wsystem() blocks on the command that you create; this is why you're not getting back to your code. _wspawn() creates a new, separate process, which should return to your code immediately.

McWafflestix
A: 

If you want to use the current implementation, you will have to fork() the process and let a child handle the browser spawning. Thus, the main process will continue and exit the function.

Alan Haggai Alavi
A: 

Instead of executing

"C:\Program Files\Internet Explorer\iexplore.exe" "http://yahoo.com"

execute

start "C:\Program Files\Internet Explorer\iexplore.exe" "http://yahoo.com"
Brian
+5  A: 

The _wsystem command will wait for the command in arg to return and returns the return value of the command. If you close the Internet Explorer window it will return command back to your program.

heavyd
seems like the only answer that actually explains what's wrong instead of just giving the straight forward solution. +1
Idan K
+5  A: 

Why not just use ShellExecute to launch the default browser with a given URL?

Synopsis:

LONG r = ShellExecute(NULL, "open", "http://www.microsoft.com", NULL, NULL, SW_SHOWNORMAL);

EDIT:

I suppose since it must be IE, this might work (note, untested code):

LONG r = ShellExecute(NULL, NULL, "iexplore.exe", "http://www.microsoft.com", NULL, SW_SHOWNORMAL);
Evan Teran
+1 The other posters are right regarding the blocking nature of _wsystem, but THIS is, without a doubt, the right way to accomplish what you're trying to do.Relying on a hardcoded path to IE a really bad idea. (Sure, it's there on most machines, but what if they use FireFox? What if the user's moved IE somehow for some reason?).
DarkSquid
The customer specified IE, so that's what it must be.
Jon