编译环境VC UNICODE
在VC中采用ShellExecute打开网页总是那么不保险,经常会因为某些原因打不开网页,所以做过多种尝试,我认为这个方法比较可靠。
链接地址:/Files/pbreak/OpenHtml.rar
主要代码:
////////////////////////////////////////////////////////////////
// 说明: 利用默认浏览器打开网页
////////////////////////////////////////////////////////////////
void OpenHtml::GetUrl(CString sURL)
{
HKEY hkRoot,hSubKey; //定义注册表根关键字及子关键字
TCHAR ValueName[256];
CString htmlValue = _T("htmlfile");
unsigned char DataValue[256];
unsigned long cbValueName=256;
unsigned long cbDataValue=256;
TCHAR ShellChar[256]; //定义命令行
DWORD dwType;
if(RegOpenKey(HKEY_CLASSES_ROOT,_T(".html"),&hkRoot) == ERROR_SUCCESS)
{
TCHAR lpWstr[MAX_PATH];
DWORD lpType = REG_SZ;
DWORD maxBufSize = MAX_PATH;
if(RegQueryValueEx(hkRoot, NULL, NULL, &lpType, (LPBYTE)lpWstr, &maxBufSize) == ERROR_SUCCESS)
htmlValue.Format(_T("%s"),lpWstr);
RegCloseKey(hkRoot);
}
//打开注册表根关键字
if(RegOpenKey(HKEY_CLASSES_ROOT,NULL,&hkRoot) == ERROR_SUCCESS)
{
//打开子关键字
if(RegOpenKeyEx(hkRoot,
htmlValue + _T("\\shell\\open\\command"),
0,
KEY_ALL_ACCESS,
&hSubKey)==ERROR_SUCCESS)
{
//读取注册表,获取默认浏览器的命令行
RegEnumValue( hSubKey,
0,
ValueName,
&cbValueName,
NULL,
&dwType,
DataValue,
&cbDataValue);
// 调用参数(主页地址)赋值
_tcscpy(ShellChar,(TCHAR *)DataValue);
CString str;
str.Format(_T("%s"), ShellChar);
int pos = str.ReverseFind('\"');
str = str.Left(pos + 1);
str += sURL;
// 启动浏览器
USES_CONVERSION;
WinExec(W2A(str),SW_SHOW);
}
else
{
//关闭注册表
RegCloseKey(hSubKey);
RegCloseKey(hkRoot);
}
}
}