C++中在浏览器打开html文件
在C++中打开相应的html文件,需要借助ShellExecute函数,对于该函数的输入参数比较复杂,所以在调用的过程中,花费了很多的时间。
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <tchar.h>
#include <iostream>
int main(int argc, char** argv)
{
char buf[1000];
GetCurrentDirectory(1000, buf); //得到当前文件的路径
std::string root = buf;
std::string path = root + "/web/index.html";//通过拼接得到html文件的绝对路径
//参数格式要按照以下的方式来写,不不然就会报类型转换的错误
ShellExecute(NULL, TEXT("open"), TEXT(path.c_str()),NULL,NULL,SW_SHOWNORMAL);
return 0;
}
在上面的代码中,其中有一个宏定义,是为了避免window一下不常用的库,如果不加该宏定义就会报错。
报错的内容如下: