Jon
Jon

Reputation: 703

Launch IE from a C++ program

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.

Upvotes: 4

Views: 13547

Answers (7)

Shail Gautam
Shail Gautam

Reputation: 71

Try this system("\"C:\Program Files\Internet Explorer\iexplore\" http://www.shail.com"); Works perfectly..

Upvotes: 0

cobaia
cobaia

Reputation: 31

Do you really need to launch IE or just some content in a browser? The ShellExecute function will launch whatever browser is configured to be the default. Call it like this:

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

Upvotes: 3

stdout123
stdout123

Reputation: 43

include <windows.h>

int main()
{
  ShellExecute(0, "open",
                  "C:\\progra~1\\intern~1\\iexplore.exe",
                  "http://www.foo.com",
                  "",
                  SW_MAXIMIZE); 
  return 0;
}

Upvotes: 3

Jewel S
Jewel S

Reputation:

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.

Upvotes: 3

T.E.D.
T.E.D.

Reputation: 44804

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.

Upvotes: 2

Kladskull
Kladskull

Reputation: 10732

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

Upvotes: 9

Glen
Glen

Reputation: 22290

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

Upvotes: 2

Related Questions