如何获取系统当前的默认浏览器呢?呃,如果你说,去读 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 下整个获取过程如下:
- 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
- );
- }
整个过程比较繁琐,但还是比较容易理解的。
|