-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetwebbrowser.cpp
170 lines (148 loc) · 4.54 KB
/
getwebbrowser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#define UNICODE
#define _UNICODE
#include <Windows.h>
#include <iostream>
#include <string>
std::wstring GetDefaultBrowser()
{
HKEY hKey;
LSTATUS status = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice", 0, KEY_READ, &hKey);
if (status != ERROR_SUCCESS)
{
return L"";
}
wchar_t progId[MAX_PATH];
DWORD dataSize = sizeof(progId);
status = RegQueryValueEx(hKey, L"ProgId", nullptr, nullptr, reinterpret_cast<LPBYTE>(progId), &dataSize);
RegCloseKey(hKey);
if (status != ERROR_SUCCESS)
{
return L"";
}
HKEY hKeyProgId;
status = RegOpenKeyEx(HKEY_CLASSES_ROOT, progId, 0, KEY_READ, &hKeyProgId);
if (status != ERROR_SUCCESS)
{
return L"";
}
wchar_t defaultBrowser[MAX_PATH];
dataSize = sizeof(defaultBrowser);
status = RegQueryValueEx(hKeyProgId, nullptr, nullptr, nullptr, reinterpret_cast<LPBYTE>(defaultBrowser), &dataSize);
RegCloseKey(hKeyProgId);
if (status != ERROR_SUCCESS)
{
return L"";
}
return std::wstring(defaultBrowser);
}
//std::wstring GetDefaultBrowserPath()
//{ //"D:\program files\360browsers\360ChromeX\Chrome\Application\360ChromeX.exe" -- "%1"
// HKEY hKey;
// LSTATUS status = RegOpenKeyEx(HKEY_CLASSES_ROOT, L"http\\shell\\open\\command", 0, KEY_READ, &hKey);
// if (status != ERROR_SUCCESS)
// {
// return L"";
// }
//
// wchar_t browserPath[MAX_PATH];
// DWORD dataSize = sizeof(browserPath);
// status = RegQueryValueEx(hKey, nullptr, nullptr, nullptr, reinterpret_cast<LPBYTE>(browserPath), &dataSize);
// RegCloseKey(hKey);
// if (status != ERROR_SUCCESS)
// {
// return L"";
// }
//
// std::wstring path(browserPath);
// // The path may contain additional parameters, so we need to extract the executable path.
// size_t pos = path.find_first_of(L"\"");
// if (pos != std::wstring::npos)
// {
// path = path.substr(1, pos - 1); // Extracting the executable path
// }
//
// return path;
//}
std::wstring GetDefaultBrowserPath()
{
std::wstring defaultBrowserPath;
HKEY hKey;
LSTATUS status;
// // Check HKEY_CLASSES_ROOT\http\shell\open\command
// status = RegOpenKeyEx(HKEY_CLASSES_ROOT, L"http\\shell\\open\\command", 0, KEY_READ, &hKey);
// if (status == ERROR_SUCCESS)
// {
// wchar_t browserPath[MAX_PATH];
// DWORD dataSize = sizeof(browserPath);
// status = RegQueryValueEx(hKey, nullptr, nullptr, nullptr, reinterpret_cast<LPBYTE>(browserPath), &dataSize);
// RegCloseKey(hKey);
// if (status == ERROR_SUCCESS)
// {
// defaultBrowserPath = std::wstring(browserPath);
//
// // Extracting the executable path
// size_t pos = defaultBrowserPath.find_first_of(L"\"");
// if (pos != std::wstring::npos)
// {
// defaultBrowserPath = defaultBrowserPath.substr(1, pos - 1);
// }
//
// return defaultBrowserPath;
// }
// }
// Check HKEY_LOCAL_MACHINE\SOFTWARE\Classes\http\shell\open\command
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Classes\\http\\shell\\open\\command", 0, KEY_READ, &hKey);
if (status == ERROR_SUCCESS)
{
wchar_t browserPath[MAX_PATH];
DWORD dataSize = sizeof(browserPath);
status = RegQueryValueEx(hKey, nullptr, nullptr, nullptr, reinterpret_cast<LPBYTE>(browserPath), &dataSize);
RegCloseKey(hKey);
if (status == ERROR_SUCCESS)
{
defaultBrowserPath = std::wstring(browserPath);
// Extracting the executable path
size_t pos = defaultBrowserPath.find_first_of(L"\"");
if (pos != std::wstring::npos)
{
defaultBrowserPath = defaultBrowserPath.substr(1, pos - 1);
}
return defaultBrowserPath;
}
}
return L"";
}
std::wstring GetDefaultBrowserPath2()
{
HKEY hKetRoot, hkeySub;
TCHAR ValueName[MAX_PATH];
ULONG cbValueName = MAX_PATH;
ULONG cbDataValue = MAX_PATH;
std::wstring s = L"";
DWORD dwType;
if (RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKetRoot) == ERROR_SUCCESS)
{
if (RegOpenKeyEx(hKetRoot, L"http\\shell\\open\\command", 0, KEY_ALL_ACCESS, &hkeySub) == ERROR_SUCCESS)
{
wchar_t szPath[MAX_PATH];
RegEnumValueW(hkeySub, 0, ValueName, &cbValueName, NULL, &dwType, (LPBYTE)szPath, &cbDataValue);
s = std::wstring(szPath);
RegCloseKey(hkeySub);
}
RegCloseKey(hKetRoot);
}
return s;
}
int main()
{
std::wstring defaultBrowser = GetDefaultBrowserPath();
if (!defaultBrowser.empty())
{
std::wcout << L"The default browser is: " << defaultBrowser << std::endl;
}
else
{
std::wcout << L"Failed to retrieve default browser." << std::endl;
}
return 0;
}