-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathUtils.cpp
More file actions
194 lines (162 loc) · 5.68 KB
/
PathUtils.cpp
File metadata and controls
194 lines (162 loc) · 5.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
#include "PathUtils.h"
// Function to get the Downloads folder path for the current user
std::string GetDownloadsPath()
{
char* buffer = nullptr;
size_t size = 0;
if (_dupenv_s(&buffer, &size, "USERPROFILE") != 0 || !buffer)
return ""; // Fehler
std::string downloadsPath = std::string(buffer) + "\\Downloads\\PATH-VAR-DUMB-FILE.txt";
free(buffer);
return downloadsPath;
}
// Get the user-specific PATH variable (non-admin)
std::string GetUserPathVariable()
{
HKEY hKey;
const char* subkey = "Environment";
char value[32767]; // Max registry value length for environment vars
DWORD value_length = sizeof(value);
DWORD type = 0;
// Open the key for the current user
if (RegOpenKeyExA(HKEY_CURRENT_USER, subkey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return "";
// Query the PATH value
if (RegQueryValueExA(hKey, "Path", nullptr, &type, reinterpret_cast<LPBYTE>(value), &value_length) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return "";
}
RegCloseKey(hKey);
// Ensure the value is a string
if (type != REG_SZ && type != REG_EXPAND_SZ)
return "";
return std::string(value);
}
// Get the system-wide PATH variable (admin)
std::string GetAdminPathVariable()
{
HKEY hKey;
const char* subkey = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
char value[32767]; // Max registry value length for environment vars
DWORD value_length = sizeof(value);
DWORD type = 0;
// Open the key for the local machine (admin)
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return "";
// Query the PATH value
if (RegQueryValueExA(hKey, "Path", nullptr, &type, reinterpret_cast<LPBYTE>(value), &value_length) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return "";
}
RegCloseKey(hKey);
// Ensure the value is a string
if (type != REG_SZ && type != REG_EXPAND_SZ)
return "";
return std::string(value);
}
// Function to split a PATH variable into individual entries
std::vector<std::string> SplitPath(const std::string& path)
{
std::vector<std::string> entries;
// Only Windows uses ';' as delimiter
char delimiter = ';';
std::string temp;
for (char c : path)
{
if (c == delimiter)
{
if (!temp.empty())
entries.push_back(temp);
temp.clear();
}
else temp += c;
}
if (!temp.empty())
entries.push_back(temp);
return entries;
}
// Update Path Variables for the Logged User
bool SetUserPath(const std::string& newPath)
{
HKEY hKey;
LONG result = RegOpenKeyExA(HKEY_CURRENT_USER, "Environment", 0, KEY_SET_VALUE, &hKey);
if (result != ERROR_SUCCESS)
{
std::cerr << "Failed to open registry key: " << result << std::endl;
return false;
}
result = RegSetValueExA(hKey, "PATH", 0, REG_EXPAND_SZ, (const BYTE*)newPath.c_str(), (DWORD)(newPath.size() + 1));
RegCloseKey(hKey);
if (result != ERROR_SUCCESS)
{
std::cerr << "Failed to set PATH in registry: " << result << std::endl;
return false;
}
// Broadcast Änderung an das System (für laufende Prozesse)
SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, nullptr);
return true;
}
// Function to open a file in the default editor
void OpenFileInDefaultEditor(const std::string& filepath) {
ShellExecuteA(
nullptr, // hwnd
"open", // operation
filepath.c_str(), // file to open
nullptr, // parameters
nullptr, // default directory
SW_SHOWNORMAL // show mode
);
}
// Function to open a folder in Windows Explorer
void OpenFolderDialog(const std::string& path)
{
// Convert std::string → wide string (UTF-16)
std::wstring wPath(path.begin(), path.end());
// Open the folder in Explorer
ShellExecuteW(nullptr, L"open", wPath.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
}
// Function to open a folder selection dialog and return the selected path
bool OpenFolderDialog(std::string& outPath)
{
bool result = false;
// COM initialisieren
HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) return false;
IFileDialog* pFileDialog = nullptr;
hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));
if (SUCCEEDED(hr))
{
// Ordner-Auswahl aktivieren
DWORD options;
pFileDialog->GetOptions(&options);
pFileDialog->SetOptions(options | FOS_PICKFOLDERS);
// Dialog anzeigen
hr = pFileDialog->Show(nullptr);
if (SUCCEEDED(hr))
{
IShellItem* pItem;
hr = pFileDialog->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath = nullptr;
pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (pszFilePath)
{
// wchar_t* → std::string
int size_needed = WideCharToMultiByte(CP_UTF8, 0, pszFilePath, -1, nullptr, 0, nullptr, nullptr);
outPath.resize(size_needed - 1); // Platz für alle Zeichen ohne Nullterminator
WideCharToMultiByte(CP_UTF8, 0, pszFilePath, -1, &outPath[0], size_needed, nullptr, nullptr);
CoTaskMemFree(pszFilePath);
result = true;
}
pItem->Release();
}
}
pFileDialog->Release();
}
CoUninitialize();
return result;
}