-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_utils.cpp
197 lines (175 loc) · 4.2 KB
/
os_utils.cpp
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
195
196
197
#include "os_utils.h"
#include "utils.h"
#include "assertions.h"
#include <fstream>
#include <thread>
namespace util
{
std::string printHresultErrorDescription( HRESULT hres )
{
_com_error error{hres};
return util::ws2s( error.ErrorMessage() );
}
std::wstring printHresultErrorDescriptionW( HRESULT hres )
{
_com_error error{hres};
return error.ErrorMessage();
}
std::string getLastErrorAsString()
{
// get the error message, if any
DWORD errorMsgId = ::GetLastError();
if ( errorMsgId == 0 )
{
return "";
}
LPSTR buff = nullptr;
size_t messageLength = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
errorMsgId,
MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
(LPSTR)&buff,
0,
nullptr );
std::string message;
message.assign( buff, messageLength );
// free the buffer allocated by the system
LocalFree( buff );
return message;
}
std::string getLastNtErrorAsString( DWORD ntStatusCode )
{
LPSTR ntStatusMessage = nullptr;
HMODULE hNtdll = LoadLibraryA( "ntdll.dll" );
size_t messageLength = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE,
hNtdll,
ntStatusCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &ntStatusMessage,
0,
nullptr );
std::string message;
message.assign( ntStatusMessage,
messageLength );
// free the buffer allocated by the system
LocalFree( ntStatusMessage );
FreeLibrary( hNtdll );
return message;
}
std::wstring bstrToStr( const BSTR& bstr )
{
ASSERT( bstr != nullptr, "BSTR was null!" );
std::wstring str{bstr, SysStringLen( bstr )}; // takes ownership so no need to SysFreeString
return str;
}
#pragma warning( disable : 4267 )
BSTR strToBstr( const std::wstring& str )
{
ASSERT( !str.empty(), "String was null!" );
BSTR bstr = SysAllocStringLen( str.data(),
str.size() );
return bstr;
}
#pragma warning( default : 4267 )
__int64 filetimeToInt64( const FILETIME& fileTime )
{
ULARGE_INTEGER ui64;
ui64.LowPart = fileTime.dwLowDateTime;
ui64.HighPart = fileTime.dwHighDateTime;
return static_cast<__int64>( ui64.QuadPart );
}
void pinThreadToCore( HANDLE hThread,
DWORD core )
{
// a set bit represents a CPU core
DWORD_PTR mask = ( static_cast<DWORD_PTR>( 1 ) << core );
auto ret = SetThreadAffinityMask( GetCurrentThread(),
mask );
}
static std::vector<HANDLE> g_detachedThreads;
//===================================================
// \function doPeriodically
// \brief like a timer event
// executes void(*f)() function at periodic (ms) intervals
// \date 2021/09/06 1:05
void doPeriodically( const std::function<void(void)>& f,
size_t intervalMs,
bool now )
{
std::thread t{[f, intervalMs, now] () -> void
{
if ( now )
{
while ( true )
{
f();
auto chronoInterval = std::chrono::milliseconds( intervalMs );
std::this_thread::sleep_for( chronoInterval );
}
}
else
{
while ( true )
{
auto chronoInterval = std::chrono::milliseconds( intervalMs );
std::this_thread::sleep_for( chronoInterval );
f();
}
}
}
};
g_detachedThreads.push_back( t.native_handle() );
t.detach();
}
void doAfter( const std::function<void(void)>& f,
size_t intervalMs )
{
std::thread t{[f, intervalMs] () -> void
{
auto chronoInterval = std::chrono::milliseconds( intervalMs );
std::this_thread::sleep_for( chronoInterval );
f();
}
};
g_detachedThreads.push_back( t.native_handle() );
t.detach();
}
std::optional<DWORD> registryGetDword( HKEY hKey,
const std::wstring& regName )
{
DWORD bufferSize = sizeof( DWORD );
DWORD val = 0ul;
long ret = RegQueryValueExW( hKey,
regName.c_str(),
nullptr,
nullptr,
reinterpret_cast<LPBYTE>( &val ),
&bufferSize );
if ( ret != ERROR_SUCCESS )
{
return std::nullopt;
}
return val;
}
std::optional<std::wstring> registryGetString( HKEY hKey,
const std::wstring& regName )
{
wchar_t buffer[512];
DWORD bufferSize = sizeof( buffer );
long ret = RegQueryValueExW( hKey,
regName.c_str(),
nullptr,
nullptr,
reinterpret_cast<LPBYTE>( buffer ),
&bufferSize );
if ( ret != ERROR_SUCCESS )
{
return std::nullopt;
}
std::wstring str{std::begin( buffer ), std::end( buffer )};
return str;
}
}//util