-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshellintegration.cpp
More file actions
142 lines (120 loc) · 4.55 KB
/
Copy pathshellintegration.cpp
File metadata and controls
142 lines (120 loc) · 4.55 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
// Copyright (c) 2026 Christopher Antos
// License: http://opensource.org/licenses/MIT
#include "main.h"
#include "shellintegration.h"
static const WCHAR c_drive_verb[] = TEXT("Software\\Classes\\Drive\\shell\\Elucidisk");
static const WCHAR c_directory_verb[] = TEXT("Software\\Classes\\Directory\\shell\\Elucidisk");
static const WCHAR c_drive_caption[] = TEXT("Scan drive with Elucidisk");
static const WCHAR c_directory_caption[] = TEXT("Scan folder with Elucidisk");
static LONG GetExecutablePath(std::wstring& path)
{
std::vector<WCHAR> buffer(MAX_PATH);
for (;;)
{
SetLastError(ERROR_SUCCESS);
const DWORD length = GetModuleFileNameW(0, buffer.data(), DWORD(buffer.size()));
if (!length)
return GetLastError();
if (length < buffer.size() - 1 ||
(length < buffer.size() && buffer[length] == L'\0'))
{
path.assign(buffer.data(), length);
return ERROR_SUCCESS;
}
if (buffer.size() >= 32768)
return ERROR_INSUFFICIENT_BUFFER;
buffer.resize(buffer.size() * 2);
}
}
static LONG SetStringValue(HKEY key, const WCHAR* name, const std::wstring& value)
{
return RegSetValueExW(key, name, 0, REG_SZ,
reinterpret_cast<const BYTE*>(value.c_str()),
DWORD((value.length() + 1) * sizeof(WCHAR)));
}
static LONG RegisterVerb(const WCHAR* verb, const WCHAR* caption, const std::wstring& executable)
{
HKEY raw_key = nullptr;
LONG result = RegCreateKeyExW(HKEY_CURRENT_USER, verb, 0, nullptr,
REG_OPTION_NON_VOLATILE, KEY_SET_VALUE,
nullptr, &raw_key, nullptr);
if (result != ERROR_SUCCESS)
return result;
result = SetStringValue(raw_key, nullptr, caption);
if (result == ERROR_SUCCESS)
result = SetStringValue(raw_key, L"Icon", executable);
RegCloseKey(raw_key);
if (result != ERROR_SUCCESS)
{
RegDeleteTreeW(HKEY_CURRENT_USER, verb);
return result;
}
std::wstring command_key(verb);
command_key += L"\\command";
raw_key = nullptr;
result = RegCreateKeyExW(HKEY_CURRENT_USER, command_key.c_str(), 0, nullptr,
REG_OPTION_NON_VOLATILE, KEY_SET_VALUE,
nullptr, &raw_key, nullptr);
if (result != ERROR_SUCCESS)
{
RegDeleteTreeW(HKEY_CURRENT_USER, verb);
return result;
}
const std::wstring command = L"\"" + executable + L"\" \"%1\"";
result = SetStringValue(raw_key, nullptr, command);
RegCloseKey(raw_key);
if (result != ERROR_SUCCESS)
RegDeleteTreeW(HKEY_CURRENT_USER, verb);
return result;
}
LONG RegisterShellIntegration()
{
std::wstring executable;
const LONG result = GetExecutablePath(executable);
if (result != ERROR_SUCCESS)
return result;
const LONG drive_result = RegisterVerb(c_drive_verb, c_drive_caption, executable);
const LONG directory_result = RegisterVerb(c_directory_verb, c_directory_caption, executable);
if (drive_result != ERROR_SUCCESS)
return drive_result;
if (directory_result != ERROR_SUCCESS)
return directory_result;
return ERROR_SUCCESS;
}
LONG UnregisterShellIntegration()
{
const LONG drive_result = RegDeleteTreeW(HKEY_CURRENT_USER, c_drive_verb);
const LONG directory_result = RegDeleteTreeW(HKEY_CURRENT_USER, c_directory_verb);
if (drive_result != ERROR_SUCCESS && drive_result != ERROR_FILE_NOT_FOUND)
return drive_result;
if (directory_result != ERROR_SUCCESS && directory_result != ERROR_FILE_NOT_FOUND)
return directory_result;
return ERROR_SUCCESS;
}
static LONG IsVerbRegistered(const WCHAR* verb, bool& registered)
{
HKEY key = nullptr;
const LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, verb, 0, KEY_READ, &key);
if (result == ERROR_SUCCESS)
{
RegCloseKey(key);
registered = true;
return ERROR_SUCCESS;
}
if (result == ERROR_FILE_NOT_FOUND || result == ERROR_PATH_NOT_FOUND)
{
registered = false;
return ERROR_SUCCESS;
}
return result;
}
int IsShellIntegrationRegistered()
{
bool drive_registered = false;
bool directory_registered = false;
const LONG drive_result = IsVerbRegistered(c_drive_verb, drive_registered);
const LONG directory_result = IsVerbRegistered(c_directory_verb, directory_registered);
if (drive_result != ERROR_SUCCESS || directory_result != ERROR_SUCCESS)
return -1;
return drive_registered || directory_registered;
}