设为首页 加入收藏

TOP

VC获取默认浏览器
2014-11-23 19:22:58 】 浏览:2032
Tags:获取 默认 浏览器
如何获取系统当前的默认 浏览器呢?呃,如果你说,去读HKEY_CLASSES_ROOT\http\shell\open\command 的注册表值,也不是不可以,但在WIN7 下不一定正确。那么我是怎么知道的呢?

昨天这样读了半天,发现总是不正确,我们将Chrome 设为默认浏览器,发现QQ 电脑管家弹出提示,然后果断打开之,发现:



我们果断得到了一个注册表项:
HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\Ftp\UserChoice[Progid]。
然后发现它的值为ChromeHTML.FXI4CGER3K4X3DSME7GMQ74NWM。

然后我们去HKEY_CLASSES_ROOT\ChromeHTML.FXI4CGER3K4X3DSME7GMQ74NWM\shell\open\command 下,就可以读出默认值了。



如果是IE 浏览器的话,将在HKEY_CLASSES_ROOT\IE.FTP\shell\open\command 下获取到。所以,在WIN7 下整个获取过程如下:
[cpp]
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
);
}


整个过程比较繁琐,但还是比较容易理解的。
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC++实现获取所有的TCP与UDP链接 下一篇VC改变DialogBar的背景色

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目