-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
45 lines (38 loc) · 868 Bytes
/
utils.cpp
File metadata and controls
45 lines (38 loc) · 868 Bytes
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
#include "utils.h"
#include <atlstr.h>
#include <Windows.h>
std::wstring GetExePath()
{
WCHAR filePath[MAX_PATH] = { 0 };
DWORD result = GetModuleFileNameW(NULL, filePath, MAX_PATH);
if (result == 0) {
return std::wstring();
}
else {
return filePath;
}
}
std::wstring GetWin32Error(DWORD errorCode)
{
LPWSTR errorText = nullptr;
DWORD result = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPWSTR>(&errorText),
0,
nullptr);
if (result == 0) {
// This call failed for some reason...
return std::wstring();
}
std::wstring error(errorText);
LocalFree(errorText);
return error;
}
std::wstring GetLastWin32Error()
{
DWORD lastError = GetLastError();
return GetWin32Error(lastError);
}