-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathexplorer_command.cc
More file actions
242 lines (209 loc) · 7.68 KB
/
explorer_command.cc
File metadata and controls
242 lines (209 loc) · 7.68 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include <windows.h>
#include <shellapi.h>
#include <filesystem>
#include <string>
#include <utility>
#include <shlwapi.h>
#include <shobjidl_core.h>
#include <userenv.h>
#include <wrl/module.h>
#include <wrl/implements.h>
#include <wrl/client.h>
#include "wil/stl.h"
#include "wil/filesystem.h"
#include "wil/win32_helpers.h"
using Microsoft::WRL::ClassicCom;
using Microsoft::WRL::ComPtr;
using Microsoft::WRL::InhibitRoOriginateError;
using Microsoft::WRL::Module;
using Microsoft::WRL::ModuleType;
using Microsoft::WRL::RuntimeClass;
using Microsoft::WRL::RuntimeClassFlags;
extern "C" BOOL WINAPI DllMain(HINSTANCE instance,
DWORD reason,
LPVOID reserved) {
switch (reason) {
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
return true;
}
namespace {
// Extracted from
// https://source.chromium.org/chromium/chromium/src/+/main:base/command_line.cc;l=109-159
std::wstring QuoteForCommandLineArg(const std::wstring& arg) {
// We follow the quoting rules of CommandLineToArgvW.
// http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
std::wstring quotable_chars(L" \\\"");
if (arg.find_first_of(quotable_chars) == std::wstring::npos) {
// No quoting necessary.
return arg;
}
std::wstring out;
out.push_back('"');
for (size_t i = 0; i < arg.size(); ++i) {
if (arg[i] == '\\') {
// Find the extent of this run of backslashes.
size_t start = i, end = start + 1;
for (; end < arg.size() && arg[end] == '\\'; ++end) {}
size_t backslash_count = end - start;
// Backslashes are escapes only if the run is followed by a double quote.
// Since we also will end the string with a double quote, we escape for
// either a double quote or the end of the string.
if (end == arg.size() || arg[end] == '"') {
// To quote, we need to output 2x as many backslashes.
backslash_count *= 2;
}
for (size_t j = 0; j < backslash_count; ++j)
out.push_back('\\');
// Advance i to one before the end to balance i++ in loop.
i = end - 1;
} else if (arg[i] == '"') {
out.push_back('\\');
out.push_back('"');
} else {
out.push_back(arg[i]);
}
}
out.push_back('"');
return out;
}
static int IsContextMenuEnabled() {
static int enabled = -1;
HKEY subhkey;
int err;
#if defined(INSIDER)
const wchar_t kTitleRegkey[] = L"Software\\Classes\\VSCodeInsidersContextMenu";
#else
const wchar_t kTitleRegkey[] = L"Software\\Classes\\VSCodeContextMenu";
#endif
if (enabled != -1)
return enabled;
// Check if the context menu is enabled in the registry.
err = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
kTitleRegkey,
0,
KEY_QUERY_VALUE | KEY_WOW64_64KEY,
&subhkey);
if (err != ERROR_SUCCESS) {
err = RegOpenKeyExW(HKEY_CURRENT_USER,
kTitleRegkey,
0,
KEY_QUERY_VALUE | KEY_WOW64_64KEY,
&subhkey);
}
if (err != ERROR_SUCCESS) {
enabled = 0;
} else {
enabled = 1;
RegCloseKey(subhkey);
}
return enabled;
}
}
class __declspec(uuid(DLL_UUID)) ExplorerCommandHandler final : public RuntimeClass<RuntimeClassFlags<ClassicCom | InhibitRoOriginateError>, IExplorerCommand> {
public:
// IExplorerCommand implementation:
IFACEMETHODIMP GetTitle(IShellItemArray* items, PWSTR* name) {
static std::wstring cached_title;
static bool title_cached = false;
if (!title_cached) {
const size_t kMaxStringLength = 1024;
wchar_t value_w[kMaxStringLength];
wchar_t expanded_value_w[kMaxStringLength];
DWORD value_size_w = sizeof(value_w);
#if defined(INSIDER)
const wchar_t kTitleRegkey[] = L"Software\\Classes\\VSCodeInsidersContextMenu";
#else
const wchar_t kTitleRegkey[] = L"Software\\Classes\\VSCodeContextMenu";
#endif
HKEY subhkey = nullptr;
LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, kTitleRegkey, 0, KEY_READ, &subhkey);
if (result != ERROR_SUCCESS) {
result = RegOpenKeyEx(HKEY_CURRENT_USER, kTitleRegkey, 0, KEY_READ, &subhkey);
}
DWORD type = REG_EXPAND_SZ;
RegQueryValueEx(subhkey, L"Title", nullptr, &type,
reinterpret_cast<LPBYTE>(&value_w), &value_size_w);
RegCloseKey(subhkey);
value_size_w = ExpandEnvironmentStrings(value_w, expanded_value_w, kMaxStringLength);
if (value_size_w && value_size_w < kMaxStringLength) {
cached_title = expanded_value_w;
} else {
cached_title = L"UnExpected Title";
}
title_cached = true;
}
return SHStrDup(cached_title.c_str(), name);
}
IFACEMETHODIMP GetIcon(IShellItemArray* items, PWSTR* icon) {
std::filesystem::path module_path{ wil::GetModuleFileNameW<std::wstring>(wil::GetModuleInstanceHandle()) };
module_path = module_path.remove_filename().parent_path().parent_path().parent_path();
module_path /= EXE_NAME;
return SHStrDupW(module_path.c_str(), icon);
}
IFACEMETHODIMP GetToolTip(IShellItemArray* items, PWSTR* infoTip) {
*infoTip = nullptr;
return E_NOTIMPL;
}
IFACEMETHODIMP GetCanonicalName(GUID* guidCommandName) {
*guidCommandName = GUID_NULL;
return S_OK;
}
IFACEMETHODIMP GetState(IShellItemArray* items, BOOL okToBeSlow, EXPCMDSTATE* cmdState) {
*cmdState = IsContextMenuEnabled() ? ECS_ENABLED : ECS_HIDDEN;
return S_OK;
}
IFACEMETHODIMP GetFlags(EXPCMDFLAGS* flags) {
*flags = ECF_DEFAULT;
return S_OK;
}
IFACEMETHODIMP EnumSubCommands(IEnumExplorerCommand** enumCommands) {
*enumCommands = nullptr;
return E_NOTIMPL;
}
IFACEMETHODIMP Invoke(IShellItemArray* items, IBindCtx* bindCtx) {
if (items) {
std::filesystem::path module_path{ wil::GetModuleFileNameW<std::wstring>(wil::GetModuleInstanceHandle()) };
module_path = module_path.remove_filename().parent_path().parent_path().parent_path();
module_path /= EXE_NAME;
DWORD count;
RETURN_IF_FAILED(items->GetCount(&count));
for (DWORD i = 0; i < count; ++i) {
ComPtr<IShellItem> item;
auto result = items->GetItemAt(i, &item);
if (SUCCEEDED(result)) {
wil::unique_cotaskmem_string path;
result = item->GetDisplayName(SIGDN_FILESYSPATH, &path);
if (SUCCEEDED(result)) {
HINSTANCE ret = ShellExecuteW(nullptr, L"open", module_path.c_str(), QuoteForCommandLineArg(path.get()).c_str(), nullptr, SW_SHOW);
if ((INT_PTR)ret <= HINSTANCE_ERROR) {
RETURN_LAST_ERROR();
}
}
}
}
}
return S_OK;
}
};
CoCreatableClass(ExplorerCommandHandler)
CoCreatableClassWrlCreatorMapInclude(ExplorerCommandHandler)
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv) {
if (ppv == nullptr)
return E_POINTER;
*ppv = nullptr;
return Module<ModuleType::InProc>::GetModule().GetClassObject(rclsid, riid, ppv);
}
STDAPI DllCanUnloadNow(void) {
return Module<ModuleType::InProc>::GetModule().GetObjectCount() == 0 ? S_OK : S_FALSE;
}
STDAPI DllGetActivationFactory(HSTRING activatableClassId,
IActivationFactory** factory) {
return Module<ModuleType::InProc>::GetModule().GetActivationFactory(activatableClassId, factory);
}