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.
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);
}
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());
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.
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.
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 );