我正在开发一个c++程序,该程序应该启动internet并在Windows7上显示本地html文件。我试图使用ShellExecute,但它不起作用。我搜索了一下,但找不到一个有效的答案。以下是代码:
ShellExecute(NULL, "open", "start iexplore %userprofile%\\Desktop\\html_files\\file.hml", NULL, NULL, SW_SHOWDEFAULT);
我将该命令复制到system()调用中,只是为了查看它是否能工作,它做到了。下面是我尝试过的system()调用:
system("start iexplore %userprofile%\\Desktop\\html_files\\file.html");由于系统调用有效,这显然是ShellExecute的一个问题。基本上,Internet没有出现。不过,所有的编译都是正确的。有什么想法吗?
发布于 2015-05-09 00:40:44
我认为IE不会识别URI中的环境变量,事实上,%具有特殊的意义。
像这样的事情应该有效:
#include <windows.h>
int main()
{
ShellExecute(NULL, "open",
"C:\\progra~1\\intern~1\\iexplore.exe",
"file:///C:/Users/UserName/Desktop/html_files/file.html",
"",
SW_MAXIMIZE);
return 0;
}另一种方法是获取%userprofile%环境变量值并连接您的URI:
#if (_MSC_VER >= 1400)
#pragma warning(push)
#pragma warning(disable: 4996) // Disabling deprecation... bad...
#endif
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <string>
int main()
{
std::string uri = std::string("file:///") + getenv("USERPROFILE") + "/Desktop/html_files/file.txt";
ShellExecute(NULL, "open",
"C:\\progra~1\\intern~1\\iexplore.exe",
uri.c_str(),
"",
SW_MAXIMIZE);
return 0;
}我在这里禁用警告,但是您应该使用_dupenv_s而不是getenv。
祝好运。
发布于 2015-05-09 01:40:11
用户可以自定义用户的shell文件夹(包括桌面)的路径,因此不能保证%userprofile\desktop在所有系统上都是正确的路径。获取用户实际桌面路径的正确方法是使用SHGetFolderPath(CSIDL_DESKTOPDIRECTORY)或SHGetKnownFolderPath(FOLDERID_Desktop)。
您不需要知道iexplorer.exe的路径,Windows知道如何找到它。因此,只需将"iexplorer.exe“本身指定为lpFile参数,将HTML指定为lpParameter参数:
ShellExecute(NULL, "open", "iexplore.exe", "full path to\\file.hml", NULL, SW_SHOWDEFAULT);尽管如此,这是非常具体的IE。如果要在用户的默认HTML /查看器中加载文件,请将lpVerb参数设置为NULL,将HTML设置为lpFile参数:
ShellExecute(NULL, NULL, "full path to\\file.hml", NULL, NULL, SW_SHOWDEFAULT);这与用户双击Windows资源管理器中的文件一样。
https://stackoverflow.com/questions/30134749
复制相似问题