我有一个用C++编写的程序,用于进行一些计算机诊断。在程序退出之前,我需要它启动Internet Explorer并导航到特定的URL。我该如何从C++中实现这个功能? 谢谢。
我有一个用C++编写的程序,用于进行一些计算机诊断。在程序退出之前,我需要它启动Internet Explorer并导航到特定的URL。我该如何从C++中实现这个功能? 谢谢。
您好,我假设您在谈论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", "https://dev59.com/SkfSa4cB1Zd3GeqPBffe", NULL, NULL, SW_SHOWNORMAL);
}
您是否真的需要启动IE或者只是在浏览器中展示内容?ShellExecute
函数将会启动默认配置的任何浏览器。请按照以下方式调用:
ShellExecute(NULL, "open", szURL, NULL, NULL, SW_SHOW);
NULL
。我已更新了此处的代码;如果您在自己的程序中使用它,请修复它。 - Cody Grayinclude <windows.h>
int main()
{
ShellExecute(0, "open",
"C:\\progra~1\\intern~1\\iexplore.exe",
"http://www.foo.com",
"",
SW_MAXIMIZE);
return 0;
}
如果你真的需要启动Internet Explorer,你还应该考虑使用CoCreateInstance(CLSID_InternetExplorer, ...),然后进行导航。根据你想要做什么,这可能是更好的选择。
如果iexplore在路径上,则只需使用标准C ++
#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());
尝试这个 system("\"C:\Program Files\Internet Explorer\iexplore\" http://www.shail.com"); 完美运行。