views:

549

answers:

5

I have a program written in C++ which does some computer diagnostics. Before the program exits, I need it to launch Internet Explorer and navigate to a specific URL. How do I do that from C++? Thanks.

+3  A: 

Here you are... I am assuming that you're talking MSVC++ here...

// I do not recommend this... but will work for you
system("\"%ProgramFiles%\\Internet Explorer\\iexplore.exe\"");


// I would use this instead... give users what they want
#include <windows.h>

void main()
{
     ShellExecute(NULL, "open", "http://stackoverflow.com/questions/982266/launch-ie-from-a-c-program", NULL, NULL, SW_SHOWNORMAL);
}
Mike Curry
this will launch the default browser, and that might be IE?
Joakim Elofsson
Thank you, but that launches the default browser which might not be IE.
Jon
Are you using some custom plug-in or something? I'd !%$%$ if a program opened up anything other than Firefox for me. Users have a choice these days ;)
Mike Curry
There you are... I added a way for Internet Explorer, though, I didn't like doing it, lol.
Mike Curry
Indeed. This is for an employer to validate an employee's machine. IE is required.
Jon
The first option should work then.
Mike Curry
Another way to get path is from registry, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE, default value is the path to the executable.
Joakim Elofsson
+1  A: 

Using just standard C++, if iexplore is on the path then

#include <stdlib.h>

...
string foo ("iexplore.exe http://example.com");
system(foo.c_str());

If it's not on the path then you need to work out the path somehow and pass the whole thing to the system call.

string foo ("path\\to\\iexplore.exe http://example.com");
system(foo.c_str());
Glen
See my post above... you should either use genenv, or %ProgramFiles%
Mike Curry
+1 This answer is better than mine. Expect that iexplore.exe is on the path (when would it not be?) and call generically with system()
John Pirie
annoyingly, iexplore isn't on my PATH on XP. :-(
Glen
+1  A: 

I'm with Glen and John, except I'd prefer to use CreateProcess instead. That way you have a process handle you can do something with. Examples might be Kill IE when you are done with it, or have a thread watching for IE to terminate (WaitForSingleObject with the process handle) so it could do something like restart it, or shut down your program too.

T.E.D.
+2  A: 

if you really need to launch internet explorer you should also look into using CoCreateInstance(CLSID_InternetExplorer, ...) and then navigating. depending on what else you want to do it might be a better option.

Jewel S
+1  A: 

Do you really need to launch IE or just some content in a browser? ShellExecute will launch whatever browser is configured to be the defualt.

ShellExecute( GetDesktopWindow(), "open", szURL, NULL, NULL, SW_SHOW );

cobaia