forked from Roka1338/RedEngine-full-leaked-source
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutils.cpp
More file actions
98 lines (73 loc) · 2.08 KB
/
utils.cpp
File metadata and controls
98 lines (73 loc) · 2.08 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
#include "utils.h"
inline std::wstring Utils::to_wstring(const std::string& str, const std::locale& loc)
{
std::vector<wchar_t> buf(str.size());
std::use_facet<std::ctype<wchar_t>>(loc).widen(str.data(), str.data() + str.size(), buf.data());
return std::wstring(buf.data(), buf.size());
}
inline std::string Utils::to_string(const std::wstring& str, const std::locale& loc)
{
std::vector<char> buf(str.size());
std::use_facet<std::ctype<wchar_t>>(loc).narrow(str.data(), str.data() + str.size(), '?', buf.data());
return std::string(buf.data(), buf.size());
}
CString Utils::ConvertSidToString(PSID pSID)
{
ATLASSERT(pSID != NULL);
if (pSID == NULL)
AtlThrow(E_POINTER);
LPTSTR pszSID = NULL;
if (!ConvertSidToStringSid(pSID, &pszSID))
AtlThrowLastWin32();
CString strSID(pszSID);
LocalFree(pszSID);
pszSID = NULL;
return strSID;
}
std::string Utils::GetCurrentUserSid()
{
HANDLE hToken = NULL;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
//_tprintf(_T("OpenProcessToken failed. GetLastError returned: %d\n"),
GetLastError();
return false;
}
DWORD dwBufferSize = 0;
if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
{
//_tprintf(_T("GetTokenInformation failed. GetLastError returned: %d\n"),
GetLastError();
// Cleanup
CloseHandle(hToken);
hToken = NULL;
return false;
}
std::vector<BYTE> buffer;
buffer.resize(dwBufferSize);
PTOKEN_USER pTokenUser = reinterpret_cast<PTOKEN_USER>(&buffer[0]);
if (!GetTokenInformation(
hToken,
TokenUser,
pTokenUser,
dwBufferSize,
&dwBufferSize))
{
//_tprintf(_T("2 GetTokenInformation failed. GetLastError returned: %d\n"),
GetLastError();
// Cleanup
CloseHandle(hToken);
hToken = NULL;
return false;
}
if (!IsValidSid(pTokenUser->User.Sid))
{
//_tprintf(_T("The owner SID is invalid.\n"));
// Cleanup
CloseHandle(hToken);
hToken = NULL;
return false;
}
//return to_string(ConvertSidToString(pTokenUser->User.Sid).GetString());
return ConvertSidToString(pTokenUser->User.Sid).GetString();
}