从C ++程序启动IE

时间:2009-06-11 16:48:20

标签: c++ internet-explorer

我有一个用C ++编写的程序,可以进行一些计算机诊断。在程序退出之前,我需要它来启动Internet Explorer并导航到特定的URL。我如何从C ++中做到这一点? 感谢。

7 个答案:

答案 0 :(得分:8)

你在这里......我假设你在这里谈论MSVC ++ ......

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

答案 1 :(得分:3)

如果你真的需要启动Internet Explorer,你还应该考虑使用CoCreateInstance(CLSID_InternetExplorer,...)然后导航。根据你想做的其他事情,这可能是一个更好的选择。

答案 2 :(得分:2)

仅使用标准C ++,如果iexplore在路径上那么

#include <stdlib.h>

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

如果它不在路径上那么你需要以某种方式计算出路径并将整个事情传递给系统调用。

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

答案 3 :(得分:2)

我和格伦和约翰在一起,除了我更愿意使用CreateProcess。这样你就可以使用进程处理。完成后,示例可能是Kill IE,或者有一个线程正在监视IE终止(WaitForSingleObject带有进程句柄),所以它可以做一些事情,比如重新启动它,或关闭你的程序

答案 4 :(得分:2)

您真的需要在浏览器中启动IE或某些内容吗? ShellExecute函数将启动任何配置为默认浏览器的浏览器。这样称呼:

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

答案 5 :(得分:2)

include <windows.h>

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

答案 6 :(得分:0)

试试这个 system(&#34; \&#34; C:\ Program Files \ Internet Explorer \ iexplore \&#34; http://www.shail.com&#34;);   完美地运作..