Repository navigation
Expand file tree
/
Copy pathcontext_menu.cpp
More file actions
71 lines (58 loc) · 2.1 KB
/
Copy pathcontext_menu.cpp
File metadata and controls
71 lines (58 loc) · 2.1 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
#include "context_menu.hpp"
#if defined(_WIN32)
#include <windows.h>
#include <string>
#endif
namespace dynamic_file_binder {
bool ContextMenu::Install()
{
#if defined(_WIN32)
wchar_t exePath[MAX_PATH]{};
if (GetModuleFileNameW(nullptr, exePath, MAX_PATH) == 0) {
return false;
}
const std::wstring command = L"\"" + std::wstring(exePath) + L"\" \"%1\"";
const wchar_t* keys[] = {
L"Software\\Classes\\*\\shell\\DynamicFileBinder",
L"Software\\Classes\\Directory\\shell\\DynamicFileBinder",
};
for (const wchar_t* keyPath : keys) {
HKEY key = nullptr;
if (RegCreateKeyExW(HKEY_CURRENT_USER, keyPath, 0, nullptr, 0, KEY_WRITE, nullptr, &key, nullptr) != ERROR_SUCCESS) {
return false;
}
const wchar_t* menuText = L"Dynamisch einbinden";
RegSetValueExW(key, nullptr, 0, REG_SZ, reinterpret_cast<const BYTE*>(menuText), static_cast<DWORD>((wcslen(menuText) + 1) * sizeof(wchar_t)));
RegSetValueExW(key, L"Icon", 0, REG_SZ, reinterpret_cast<const BYTE*>(exePath), static_cast<DWORD>((wcslen(exePath) + 1) * sizeof(wchar_t)));
RegCloseKey(key);
std::wstring commandKeyPath = std::wstring(keyPath) + L"\\command";
if (RegCreateKeyExW(HKEY_CURRENT_USER, commandKeyPath.c_str(), 0, nullptr, 0, KEY_WRITE, nullptr, &key, nullptr) != ERROR_SUCCESS) {
return false;
}
RegSetValueExW(key, nullptr, 0, REG_SZ, reinterpret_cast<const BYTE*>(command.c_str()), static_cast<DWORD>((command.size() + 1) * sizeof(wchar_t)));
RegCloseKey(key);
}
return true;
#else
return false;
#endif
}
bool ContextMenu::Uninstall()
{
#if defined(_WIN32)
const wchar_t* keys[] = {
L"Software\\Classes\\*\\shell\\DynamicFileBinder",
L"Software\\Classes\\Directory\\shell\\DynamicFileBinder",
};
bool success = true;
for (const wchar_t* keyPath : keys) {
if (RegDeleteTreeW(HKEY_CURRENT_USER, keyPath) != ERROR_SUCCESS) {
success = false;
}
}
return success;
#else
return false;
#endif
}
} // namespace dynamic_file_binder