如何獲取系統(tǒng)當(dāng)前的默認(rèn)瀏覽器呢?呃,如果你說(shuō),去讀 HKEY_CLASSES_ROOT\http\shell\open\command 的注冊(cè)表值,也不是不可以,但在 WIN7 下不一定正確。那么我是怎么知道的呢?
昨天這樣讀了半天,發(fā)現(xiàn)總是不正確,我們將 Chrome 設(shè)為默認(rèn)瀏覽器,發(fā)現(xiàn) QQ 電腦管家彈出提示,然后果斷打開(kāi)之,發(fā)現(xiàn):

我們果斷得到了一個(gè)注冊(cè)表項(xiàng):
HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\Ftp\UserChoice[Progid]。
然后發(fā)現(xiàn)它的值為 ChromeHTML.FXI4CGER3K4X3DSME7GMQ74NWM。
然后我們?nèi)?span style="font-family: Comic Sans MS;">HKEY_CLASSES_ROOT\ChromeHTML.FXI4CGER3K4X3DSME7GMQ74NWM\shell\open\command 下,就可以讀出默認(rèn)值了。

如果是 IE 瀏覽器的話,將在 HKEY_CLASSES_ROOT\IE.FTP\shell\open\command 下獲取到。所以,在 WIN7 下整個(gè)獲取過(guò)程如下:
- 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
- );
- }
整個(gè)過(guò)程比較繁瑣,但還是比較容易理解的。
|