-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRadFType.cpp
289 lines (254 loc) · 8.95 KB
/
RadFType.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <cstdio>
#include <Windows.h>
#include <tchar.h>
#include <vector>
#include <string>
#include <memory>
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
struct WinError
{
DWORD dwError;
LPCTSTR szModule;
void print(FILE *stream, LPCTSTR msg) const
{
HMODULE hLibrary = szModule != nullptr ? GetModuleHandle(szModule) : NULL;
LPTSTR pMessage = nullptr;
if (!FormatMessage((hLibrary != NULL ? FORMAT_MESSAGE_FROM_HMODULE : FORMAT_MESSAGE_FROM_SYSTEM) |
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS,
hLibrary,
dwError,
0,
(LPTSTR) &pMessage,
0,
NULL))
{
_ftprintf(stream, _T("Format message failed with 0x%08x\n"), GetLastError());
}
else
{
//_ftprintf(stream, _T("Error: 0x%08x %s %s"), dwError, msg, pMessage);
//_ftprintf(stream, _T("%s %s"), msg, pMessage);
msg; //ignore unused
_ftprintf(stream, _T("%s"), pMessage);
LocalFree(pMessage);
}
}
};
inline bool Empty(LPCTSTR s)
{
return s[0] == TEXT('\0');
}
template <>
class std::default_delete<HKEY>
{
public:
typedef HKEY pointer;
void operator()(HKEY k) const
{
RegCloseKey(k);
}
};
bool Check(LSTATUS retCode, LPCTSTR msg)
{
if (retCode != ERROR_SUCCESS)
{
if (msg)
WinError({DWORD(retCode)}).print(stderr, msg);
return false;
}
return true;
}
#define CHECK(x) Check(x, TEXT(#x))
#define CHECK_RET(x, r) if (!Check(x, TEXT(#x))) return r
#define CHECK_MSG_RET(x, m, r) if (!Check(x, m)) return r
#define CHECK_CONT(x) if (!Check(x, TEXT(#x))) continue
inline LSTATUS RegOpenKeyEx(HKEY hKey, LPCTSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, std::unique_ptr<HKEY>* phResultKey)
{
HKEY hResultKey = NULL;
LSTATUS retCode = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, &hResultKey);
if (retCode == ERROR_SUCCESS)
phResultKey->reset(hResultKey);
return retCode;
}
inline LSTATUS RegCreateKey(HKEY hKey, LPCTSTR lpSubKey, std::unique_ptr<HKEY>* phResultKey)
{
HKEY hResultKey = NULL;
LSTATUS retCode = RegCreateKey(hKey, lpSubKey, &hResultKey);
if (retCode == ERROR_SUCCESS)
phResultKey->reset(hResultKey);
return retCode;
}
bool DisplayValue(LPCTSTR verb, HKEY hCommandKey, LPCTSTR type)
{
TCHAR data[1024] = TEXT("");
LONG size = ARRAYSIZE(data) * sizeof(TCHAR);
CHECK(RegQueryValue(hCommandKey, nullptr, data, &size));
if (!Empty(data))
{
if (Empty(verb))
_ftprintf(stdout, TEXT("%s=%s\n"), type, data);
else
_ftprintf(stdout, TEXT("%s:%s=%s\n"), type, verb, data);
}
return !Empty(data);
}
bool ShowFtype(HKEY hBaseKey, LPCTSTR type, LPCTSTR verb)
{
std::unique_ptr<HKEY> hTypeKey;
CHECK_MSG_RET(RegOpenKeyEx(hBaseKey, type, 0, KEY_READ, &hTypeKey), nullptr, false);
std::unique_ptr<HKEY> hVerbKey;
TCHAR subkey[100];
_stprintf_s(subkey, TEXT("shell\\%s"), verb);
CHECK_MSG_RET(RegOpenKeyEx(hTypeKey.get(), subkey, 0, KEY_READ, &hVerbKey), nullptr, false);
std::unique_ptr<HKEY> hCommandKey;
CHECK_MSG_RET(RegOpenKeyEx(hVerbKey.get(), TEXT("command"), 0, KEY_READ, &hCommandKey), nullptr, false);
const bool valid = DisplayValue(verb, hCommandKey.get(), type);
return valid;
}
void SetFtype(HKEY hBaseKey, LPCTSTR type, LPCTSTR verb, LPCTSTR value, LPCTSTR name)
{
std::unique_ptr<HKEY> hTypeKey;
CHECK_RET(RegCreateKey(hBaseKey, type, &hTypeKey), ;);
std::unique_ptr<HKEY> hVerbKey;
TCHAR subkey[100];
_stprintf_s(subkey, TEXT("shell\\%s"), verb);
CHECK_RET(RegCreateKey(hTypeKey.get(), subkey, &hVerbKey), ;);
std::unique_ptr<HKEY> hCommandKey;
CHECK_RET(RegCreateKey(hVerbKey.get(), TEXT("command"), &hCommandKey), ;);
if (value)
{
CHECK(RegSetValue(hCommandKey.get(), nullptr, REG_SZ, value, (lstrlen(value) + 1) * sizeof(TCHAR)));
}
else
{
CHECK(RegDeleteValue(hCommandKey.get(), nullptr));
}
if (name)
{
CHECK(RegSetValue(hVerbKey.get(), nullptr, REG_SZ, name, (lstrlen(name) + 1) * sizeof(TCHAR)));
}
DisplayValue(verb, hCommandKey.get(), type);
}
void EnumFtype(HKEY hKey, LPCTSTR type)
{
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
// Get the class name and the value count.
CHECK_RET(RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime), // last write time
;);
std::vector<TCHAR> achKey(cbMaxSubKey + 1);
for (DWORD i = 0; i < cSubKeys; ++i)
{
DWORD cbName = static_cast<DWORD>(achKey.size());
CHECK_CONT(RegEnumKeyEx(hKey, i,
achKey.data(),
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime));
if (!achKey.empty())
ShowFtype(hKey, achKey.data(), type);
}
}
int _tmain(const int argc, LPCTSTR argv[])
{
const LPCTSTR strSubKey = TEXT("Software\\Classes");
HKEY hBaseKey = HKEY_LOCAL_MACHINE;
std::tstring type;
std::tstring name;
bool setValue = false;
bool showHelp = false;
std::tstring value;
std::tstring verb = TEXT("open");
for (int argi = 1; argi < argc; ++argi)
{
LPCTSTR arg = argv[argi];
if (arg[0] == TEXT('/'))
{
if (lstrcmpi(arg, TEXT("/U")) == 0)
hBaseKey = HKEY_CURRENT_USER;
else if (lstrcmpi(arg, TEXT("/V")) == 0)
{
verb = argv[++argi];
size_t f = verb.find(TEXT(':'));
if (f != std::tstring::npos)
{
name = verb.substr(f + 1);
verb = verb.substr(0, f);
}
}
else if (lstrcmpi(arg, TEXT("/?")) == 0)
showHelp = true;
else
_ftprintf(stderr, TEXT("Unknown option %s\n"), arg);
}
else if (type.empty())
{
type = arg;
size_t f = type.find(TEXT('='));
if (f != std::tstring::npos)
{
setValue = true;
value = type.substr(f + 1);
type = type.substr(0, f);
}
}
else
{
value += ' ';
value += arg;
}
}
if (showHelp)
{
_ftprintf(stdout, TEXT("Displays or modifies file types used in file extension associations\n"));
_ftprintf(stdout, TEXT("\n"));
_ftprintf(stdout, TEXT("FTYPE [/U] [/V <verb>[:<name>]] [fileType[=[openCommandString]]]\n"));
_ftprintf(stdout, TEXT("\n"));
_ftprintf(stdout, TEXT(" /U Set for current user only\n"));
_ftprintf(stdout, TEXT(" /V Verb to use (default: open)\n"));
_ftprintf(stdout, TEXT(" fileType Specifies the file type to examine or change\n"));
_ftprintf(stdout, TEXT(" openCommandString Specifies the open command to use when launching files\n"));
_ftprintf(stdout, TEXT(" of this type.\n"));
return EXIT_SUCCESS;
}
std::unique_ptr<HKEY> hKey;
CHECK_RET(RegOpenKeyEx(hBaseKey, strSubKey, 0, KEY_READ, &hKey), EXIT_FAILURE);
if (!type.empty())
{
if (setValue)
SetFtype(hKey.get(), type.c_str(), verb.c_str(), value.empty() ? nullptr : value.c_str(), name.empty() ? nullptr : name.c_str());
else
if (!ShowFtype(hKey.get(), type.c_str(), verb.c_str()))
_ftprintf(stderr, TEXT("File type '%s' not found or no %s command associated with it.\n"), type.c_str(), verb.c_str());
}
else
EnumFtype(hKey.get(), verb.c_str());
return EXIT_SUCCESS;
}