转载两篇方法,一个是Win7,一个是WinXP, 如果哪位实验了Vista 和 Win8,请告知结果~
XP:
HKEY hkRoot,hSubKey; //定义注册表根关键字及子关键字
char ValueName[256];
unsigned char DataValue[256];
unsigned long cbValueName=256;
unsigned long cbDataValue=256;
char ShellChar[256]; //定义命令行
DWORD dwType;
//打开注册表根关键字
if(RegOpenKey(HKEY_CLASSES_ROOT,NULL,&hkRoot)==ERROR_SUCCESS)
{
//打开子关键字
if(RegOpenKeyEx(hkRoot,
"htmlfile//shell//open//command",
0,
KEY_ALL_ACCESS,
&hSubKey)==ERROR_SUCCESS)
{
//读取注册表,获取默认浏览器的命令行
RegEnumValue(hSubKey,
0,
ValueName,
&cbValueName,
NULL,
&dwType,
DataValue,
&cbDataValue);
// 调用参数(主页地址)赋值
strcpy(ShellChar,(char *)DataValue);
strcat(ShellChar," www.neaase.net/~xiaohui");
// 启动浏览器
WinExec(ShellChar,SW_SHOW);
}
else
MessageBox("WEB浏览器打开错误!","错误",MB_OK);
}
else
MessageBox("WEB浏览器打开错误!","错误",MB_OK);
//关闭注册表
RegCloseKey(hSubKey);
RegCloseKey(hkRoot);
Win7:
void LaunchDefaultBrowser()
{
HKEY hDefBrowserPos = NULL;
wstring wstrDefBrowserPath = L"iexplore.exe";
WCHAR wszBuffer[MAX_PATH + 1] = {0};
DWORD dwDataSize = sizeof(wszBuffer);
if (ERROR_SUCCESS == ::RegGetValueW(
HKEY_CURRENT_USER,
L"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\Ftp\UserChoice\",
L"Progid",
RRF_RT_REG_SZ,
0,
wszBuffer,
&dwDataSize
))
{
wstring wstrDefBrowserPos = wszBuffer;
wstrDefBrowserPos += L"\shell\open\command\";
dwDataSize = sizeof(wszBuffer);
if (ERROR_SUCCESS == ::RegGetValueW(
HKEY_CLASSES_ROOT,
wstrDefBrowserPos.c_str(),
NULL,
RRF_RT_REG_SZ,
0,
wszBuffer,
&dwDataSize
))
{
// 解出exe 路径.
wstrDefBrowserPath = wszBuffer;
wstring::size_type leftQuotation = wstrDefBrowserPath.find(L'"');
if (leftQuotation != wstring::npos)
{
wstring::size_type rightQuotation = wstrDefBrowserPath.find(L'"', leftQuotation + 1);
if (rightQuotation != wstring::npos)
{
wstrDefBrowserPath.assign(
wstrDefBrowserPath.begin() + leftQuotation + 1,
wstrDefBrowserPath.begin() + rightQuotation
);
}
}
}
}
::ShellExecuteW( NULL, L"open", wstrDefBrowserPath.c_str(), NULL, NULL, SW_NORMAL);
}