-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericHelper.cpp
More file actions
315 lines (253 loc) · 7.23 KB
/
Copy pathGenericHelper.cpp
File metadata and controls
315 lines (253 loc) · 7.23 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//#include "stdafx.h"
//#include <WS2tcpip.h>
#include <Windows.h>
#include <direct.h>
#include <math.h>
#include <tchar.h>
#include <time.h>
#include <Shlwapi.h>
#include <ShlObj.h>
#include <IPHlpApi.h>
#include "GenericHelper.h"
#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib,"winmm.lib")
#pragma comment(lib, "version.lib")
#pragma comment(lib, "shlwapi.lib")
BOOL SystemShutdown(UINT uFlags)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return(FALSE);
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(uFlags, 0))
return FALSE;
return TRUE;
}
// 查找进程主窗口的回调函数
BOOL CALLBACK EnumWindowCallBack(HWND hWnd, LPARAM lParam)
{
ProcessWindow *pProcessWindow = (ProcessWindow *)lParam;
DWORD dwProcessId;
GetWindowThreadProcessId(hWnd, &dwProcessId);
// 判断是否是指定进程的主窗口
if (pProcessWindow->dwProcessId == dwProcessId && IsWindowVisible(hWnd) &&
(GetWindowLong(hWnd, GWL_STYLE) & WS_MAXIMIZEBOX) && GetParent(hWnd) == NULL)
{
pProcessWindow->hwndWindow = hWnd;
return FALSE;
}
return TRUE;
}
string_t binToHex(const unsigned char* buffer, int size)
{
string_t s;
TCHAR digit[4];
unsigned char* p = (unsigned char*)buffer;
for (int index = 0; index < size; index++)
{
_stprintf_s(digit, _T("%02x "), *p++);
s += digit;
}
return s;
}
// DWORD GetTimeGap32(DWORD dwBefor)
// {
// DWORD dwCur = ::timeGetTime();
// return (dwCur - dwBefor);
// }
//************************************************************
//FILETIME, SYSTEMTIME 与 time_t 相互转换
//#####SYSTEMTIME 与 FILETIME相互转换#####
//可以使用系统函数
//FileTimeToSystemTime(&ftcreate,&stcreate);
//参数:
//(lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME)
//说明
//根据一个FILETIME结构的内容,装载一个SYSTEMTIME结构
//返回值
//Long,非零表示成功,零表示失败。会设置GetLastError
//参数表
//参数 类型及说明
//lpFileTime FILETIME,包含了文件时间的一个结构
//lpSystemTime SYSTEMTIME,用于装载系统时间信息的一个结构
//#####SYSTEMTIME 与 time_t相互转换#####
//#### Time_tToSystemTime ####
void TimetToSystemTime(time_t t, LPSYSTEMTIME pst)
{
FILETIME ft;
LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
ft.dwLowDateTime = (DWORD)ll;
ft.dwHighDateTime = (DWORD)(ll >> 32);
FileTimeToSystemTime(&ft, pst);
}
//#### FileTimeToTime_t ####
void FileTimeToTime_t(FILETIME ft, time_t *t)
{
//LONGLONG ll;
ULARGE_INTEGER ui;
ui.LowPart = ft.dwLowDateTime;
ui.HighPart = ft.dwHighDateTime;
//ll = (LONGLONG(ft.dwHighDateTime)<<32) + ft.dwLowDateTime;
*t = ((LONGLONG)(ui.QuadPart - 116444736000000000) / 10000000);
}
//#### SystemTimeToTime_t ####
void SystemTimeToTime_t(SYSTEMTIME st, time_t *pt)
{
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
FileTimeToTime_t(ft, pt);
}
//********************************************************************/
SYSTEMTIME TimetToSystemTime(__time64_t t, bool toLocal)
{
if (toLocal)
{
struct tm gm;
_localtime64_s(&gm, &t);
t = _mkgmtime64(&gm);
}
FILETIME ft;
SYSTEMTIME pst;
LONGLONG nLL = Int32x32To64(t, 10000000) + 116444736000000000;
ft.dwLowDateTime = (DWORD)nLL;
ft.dwHighDateTime = (DWORD)(nLL >> 32);
FileTimeToSystemTime(&ft, &pst);
return pst;
}
/* **SYSTEMTIME转time_t */
__time64_t SystemTimeToTimet(const SYSTEMTIME& st, bool toUTC)
{
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
LONGLONG nLL;
ULARGE_INTEGER ui;
ui.LowPart = ft.dwLowDateTime;
ui.HighPart = ft.dwHighDateTime;
nLL = ((LONGLONG)ft.dwHighDateTime << 32) + ft.dwLowDateTime;
__time64_t pt = (long)((LONGLONG)(ui.QuadPart - 116444736000000000) / 10000000);
if (toUTC)
{
struct tm gm;
_gmtime64_s(&gm, &pt);
pt = _mktime64(&gm);
}
return pt;
}
bool SeparateInt64(INT64 llSize, int& GB, int& MB, int& kB, int& Byte)
{
if (llSize < 0) return false;
GB = int(llSize/1000000000);
MB = int((llSize-INT64(GB)*1000000000)/1000000);
kB = int((llSize-INT64(GB)*1000000000-MB*1000000)/1000);
Byte = int((llSize-INT64(GB)*1000000000-MB*1000000-kB*1000));
return true;
}
bool IntegerToString(INT64 llSize, LPTSTR strOut, DWORD nSize)
{
if (nSize < 20) return false;
int ret = -1;
int GB = int(llSize/1000000000);
int MB = int(llSize/1000000%1000);
int kB = int(llSize/1000%1000);
int Byte = int(llSize%1000);
if (GB > 0)
{
ret = _stprintf_s(strOut, nSize, _T("%d,%03d,%03d,%03d"), GB, MB, kB, Byte);
}
else if (MB > 0)
{
ret = _stprintf_s(strOut, nSize, _T("%d,%03d,%03d"), MB, kB, Byte);
}
else if (kB > 0)
{
ret = _stprintf_s(strOut, nSize, _T("%d,%03d"), kB, Byte);
}
else
{
ret = _stprintf_s(strOut, nSize, _T("%d"), Byte);
}
return (ret != -1);
}
double ArrayMax(double* arr, int len, int* retIdx)
{
double dmax = std::numeric_limits<double>::min();
int idx = -1;
for (int i = 1; i < len; i++)
{
if (arr[i] > dmax)
{
dmax = arr[i]; idx = i;
}
}
if (retIdx) *retIdx = idx;
return dmax;
}
bool EnumHostIPv4Addr(std::vector<std::string>& ipvec)
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
bool bRet = false;
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO) * 3);
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO) * 3;
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
ipvec.clear();
pAdapter = pAdapterInfo;
while (pAdapter) {
//if (strcmp(pAdapter->IpAddressList.IpAddress.String, "0.0.0.0") != 0)
ipvec.emplace_back(pAdapter->IpAddressList.IpAddress.String);
pAdapter = pAdapter->Next;
}
bRet = true;
}
else {
bRet = false;
}
free(pAdapterInfo);
return bRet;
}
// LPCTSTR FindHostIPv4Addr(const std::vector<std::string>& ipvec, bool biggest)
// {
// if (ipvec.size() == 0) return nullptr;
//
// static TCHAR ipaddr[16] = { 0 };
// int idx = -1, num = 0; DWORD dwIP = 0;
// IN_ADDR addr;
//
// for each (auto& var in ipvec)
// {
// ++num;
// inet_pton(AF_INET, var.c_str(), &addr);
// ULONG ul = ntohl(addr.S_un.S_addr);
// if ((num == 1) || (biggest && ul > dwIP) || (!biggest && ul < dwIP))
// {
// dwIP = ul;
// idx = num - 1;
// }
// }
// addr.S_un.S_addr = htonl(dwIP);
//
// if (InetNtop(AF_INET, &addr, ipaddr, 16))
// return ipaddr;
// else
// return nullptr;
// }