-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsysupport.cpp
926 lines (853 loc) · 25.9 KB
/
sysupport.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
#include <process.h>
#include "sysupport.h"
//#include "AC/shellExt.h"
#pragma comment(lib, "version.lib")
//#include "AC/highLevelAPI.h" // 加载xp下不支持的API
#include <Iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib")
namespace gtc {
////////////////////////////////////////////////////////////////////////////////////////////////////////
// format类 2014-06-27
// 格式化类
////////////////////////////////////////////////////////////////////////////////////////////////////////
std::wstring format::rect(const RECT &rc, bool showSize /*= false*/)
{
std::wstring wstr;
wchar_t pbuf[80] = {0};
std::swprintf(pbuf, _countof(pbuf), L"%d,%d,%d,%d", rc.left, rc.top, rc.right, rc.bottom);
wstr = pbuf;
if (showSize)
{
std::swprintf(pbuf, _countof(pbuf), L" %d,%d", rc.right-rc.left, rc.bottom-rc.top);
wstr += pbuf;
}
return wstr;
//std::wostringstream os;
//os << rc.left << L"," << rc.top << L"," << rc.right << L"," << rc.bottom;
//if (showSize)
//{
// os << L" " << rc.right-rc.left << L"," << rc.bottom-rc.top;
//}
//os << std::ends;
//return os.str();
}
RECT format::rect(PCWSTR prect)
{
RECT rc = {0};
wchar_t *pend = nullptr;
try
{
rc.left = std::wcstol(prect, &pend, 10); assert(pend);
rc.top = std::wcstol(pend+1, &pend, 10); assert(pend);
rc.right = std::wcstol(pend+1, &pend, 10); assert(pend);
rc.bottom = std::wcstol(pend+1, nullptr, 10);
}
catch (...)
{
::ZeroMemory(&rc, sizeof(rc));
}
return rc;
}
std::wstring format::point(const POINT &pt)
{
wchar_t pbuf[40] = {0};
std::swprintf(pbuf, _countof(pbuf), L"%d,%d", pt.x, pt.y);
return pbuf;
//std::wostringstream os;
//os << pt.x << L"," << pt.y << std::ends;
//return os.str();
}
POINT format::point(PCWSTR ppoint)
{
POINT pt = {0};
wchar_t *pend = nullptr;
try
{
pt.x = std::wcstol(ppoint, &pend, 10); assert(pend);
pt.y = std::wcstol(pend+1, nullptr, 10);
}
catch (...)
{
pt.x = pt.y = 0;
}
return pt;
}
std::wstring format::size(const SIZE &sz)
{
wchar_t pbuf[40] = {0};
std::swprintf(pbuf, _countof(pbuf), L"%d,%d", sz.cx, sz.cy);
return pbuf;
//std::wostringstream os;
//os << sz.cx << L"," <<sz.cy << std::ends;
//return os.str();
}
std::wstring format::color(DWORD clr)
{
wchar_t pbuf[10] = {0};
std::swprintf(pbuf, _countof(pbuf), L"#%08X", clr);
return pbuf;
}
DWORD format::color(PCWSTR pclr)
{
PWSTR pstr = NULL;
if( *pclr == L'#') pclr = ::CharNext(pclr);
DWORD clrColor = std::wcstoul(pclr, &pstr, 16);
return clrColor;
}
std::wstring format::floatString(float fltValue, int32_t decimals /*= 2*/)
{
if(decimals < 0) decimals = 0;
else if(decimals > 16) decimals = 16;
wchar_t fmt[10] = {0};
std::swprintf(fmt, 10, L"%%0.%df", decimals);
wchar_t plgcValue[80] = {0};
std::swprintf(plgcValue, _countof(plgcValue), fmt, fltValue);
return plgcValue;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// convert类 2014-04-25
// 常用类型转换操作类
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
// app类
// 常用文件操作类
////////////////////////////////////////////////////////////////////////////////////////////////////////
std::wstring app::mc_wsLogPath = L""; // 日志文件路径
std::wstring app::mc_wsIniPath = L""; // 配置文件路径
std::wstring app::mc_wsIniName = L""; // 配置文件名称
std::wstring app::mc_wsLogFileName = L""; // 日志文件名的开头部分
std::wstring app::mc_wsDumpFileName = L""; // dump文件的开头部分
DWORD app::MAIN_THREAD_ID = 0;
HWND app::MAIN_WINDOW = nullptr;
AdapterInfoArrayType app::getAdapterInfo(PCWSTR pmacSeparator /*= NULL*/)
{
AdapterInfoArrayType vec;
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
PIP_ADAPTER_INFO pAdapter = NULL;
ULONG len = sizeof(IP_ADAPTER_INFO);
DWORD dwret = GetAdaptersInfo(pIpAdapterInfo, &len);
if (dwret == ERROR_BUFFER_OVERFLOW)
{
delete pIpAdapterInfo;
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[len];
assert(pIpAdapterInfo);
dwret = GetAdaptersInfo(pIpAdapterInfo, &len);
}
wchar_t pmac[8] = {0};
std::wstring wsMac, wsIP;
int addrLen;
if (dwret == ERROR_SUCCESS)
{
pAdapter = pIpAdapterInfo;
while (pAdapter)
{
wsMac.clear();
wsIP.clear();
// mac
addrLen = (int)pAdapter->AddressLength;
for (int idx = 0; idx < addrLen; ++idx) {
std::swprintf(pmac, _countof(pmac), L"%02X", pAdapter->Address[idx]);
wsMac += pmac;
if (pmacSeparator && idx < addrLen-1)
{
wsMac += pmacSeparator;
}
}
// ip
IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
if (pIpAdapterInfo)
{
wsIP = gtc::stringUtil::toWChar(pIpAddrString->IpAddress.String);
}
//可能网卡有多IP,因此通过循环去判断
//IP_ADDR_STRING *pIpAddrString =&(pIpAdapterInfo->IpAddressList);
//do
//{
// cout<<"该网卡上的IP数量:"<<++IPnumPerNetCard<<endl;
// cout<<"IP 地址:"<<pIpAddrString->IpAddress.String<<endl;
// cout<<"子网地址:"<<pIpAddrString->IpMask.String<<endl;
// cout<<"网关地址:"<<pIpAdapterInfo->GatewayList.IpAddress.String<<endl;
// pIpAddrString=pIpAddrString->Next;
//} while (pIpAddrString);
if (pAdapter->Type == MIB_IF_TYPE_ETHERNET)
{
vec.push_back(BSAdapterInfo(wsIP, wsMac));
}
//SEND_LOGA("WARNING name:<%s> desc:<%s> type:<%d> mac:<%s>", pAdapter->AdapterName, pAdapter->Description, pAdapter->Type, strmac.c_str());
pAdapter = pAdapter->Next;
}
delete pIpAdapterInfo;
}
return vec;
}
RECT app::getDeskArea(bool containTaskbar /*= true*/)
{
RECT rcWork = {0};
if (containTaskbar)
{
SIZE sz = {GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN)};
rcWork.right = sz.cx;
rcWork.bottom = sz.cy;
}
else
{
SystemParametersInfo(SPI_GETWORKAREA, NULL, (PVOID)&rcWork, 0);
}
return rcWork;
}
bool app::addFilterWindowMessage(HWND hWnd, UINT uMsg)
{
if(app::isWindowsXP()) return true;
//////////////////////////////////////////////////////////////////////////////////////////
// 2015-6-9 User32.dll API 相关声明
typedef BOOL (WINAPI* fn_ChangeWindowMessageFilter)(UINT /*message*/, DWORD /*dwFlag*/);
typedef BOOL (WINAPI* fn_ChangeWindowMessageFilterEx )(HWND /*hWnd*/, UINT /*message*/, DWORD /*action*/, PCHANGEFILTERSTRUCT /*pChangeFilterStruct*/);
fn_ChangeWindowMessageFilter _fnChangeWindowMessageFilter = NULL;
fn_ChangeWindowMessageFilterEx _fnChangeWindowMessageFilterEx = NULL;
HMODULE hmdl = LoadLibrary(L"User32.dll");
if (hmdl)
{
_fnChangeWindowMessageFilter = (fn_ChangeWindowMessageFilter)GetProcAddress(hmdl, "ChangeWindowMessageFilter");
_fnChangeWindowMessageFilterEx = (fn_ChangeWindowMessageFilterEx)GetProcAddress(hmdl, "ChangeWindowMessageFilterEx");
FreeLibrary(hmdl);
}
if (_fnChangeWindowMessageFilterEx)
{
return _fnChangeWindowMessageFilterEx(hWnd, uMsg, MSGFLT_ALLOW, NULL) == TRUE;
}
else if (_fnChangeWindowMessageFilter)
{
return _fnChangeWindowMessageFilter(uMsg, MSGFLT_ADD) == TRUE;
}
return false;
}
void app::captureWindow(HWND hWnd)
{
assert(hWnd);
HWND hWndParent = hWnd;
while( ::GetParent(hWndParent) != NULL ) hWndParent = ::GetParent(hWndParent);
::SetForegroundWindow(hWnd);
//::SetActiveWindow(hWndParent);
//::SetActiveWindow(*_pdialogMainPtr);
::ShowWindow(hWnd, SW_SHOW);
::SendMessage(hWndParent, WM_NCACTIVATE, TRUE, 0L);
}
bool app::exec(PCWSTR pexeFile, PCWSTR pParameter /*= NULL*/, int showFlag /*= SW_SHOW*/)
{
// 用默认浏览器打开网页 exec(L"http://www.xxx.com")
// 打开文件夹 exec(L"c:\\")
// 执行命令 exec(L"explorer.exe")
// 执行命令 exec(L"c:\windows\explorer.exe")
assert(pexeFile);
if(!pexeFile) return false;
if(!(stringUtil::startsWith(pexeFile, L"http://")
|| stringUtil::startsWith(pexeFile, L"https://")
|| stringUtil::startsWith(pexeFile, L"www.")
|| ::PathFileExistsW(pexeFile)
|| stringUtil::endsWith(pexeFile, L".exe"))) {
SEND_LOG(L"ERROR app::exec 文件不存在!<%s>", pexeFile);
return false;
}
//if(!(stringUtil::startsWith(pexeFile, L"http://")
// || stringUtil::startsWith(pexeFile, L"https://")
// || stringUtil::startsWith(pexeFile, L"www.")) && !::PathFileExistsW(pexeFile)) {
// SEND_LOG(L"ERROR app::exec 文件不存在!<%s>", pexeFile);
// return false;
//}
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
SHELLEXECUTEINFO shexe = {0};
shexe.cbSize = sizeof(SHELLEXECUTEINFO);
shexe.fMask = SEE_MASK_DEFAULT;
shexe.lpVerb = L"open";
shexe.lpFile = pexeFile;
shexe.lpParameters = pParameter;
shexe.nShow = SW_SHOW;
bool bl = ShellExecuteEx(&shexe) == TRUE;
if(!bl)
SEND_LOG(L"app::exec 启动进程失败!!! bl:%d lasterr:%u exefile:<%s>", bl, GetLastError(), pexeFile);
CoUninitialize();
return bl;
}
uint64_t app::getProductVersion(PCWSTR pFileName /*= NULL*/, uint64_t *poutFileVersion /*= NULL*/)
{
uint64_t ver = 0;
wchar_t pBuf[MAX_PATH] = {0};
if (!pFileName || std::wcslen(pFileName) <= 0)
{
GetModuleFileName(NULL, pBuf, _countof(pBuf));
pFileName = pBuf;
}
DWORD dwHandle = 0;
BYTE *pData = NULL, *pVerData = NULL;
VS_FIXEDFILEINFO *pverInfo = NULL;
UINT len = 0;
DWORD dwInfoSize = GetFileVersionInfoSize(pFileName, &dwHandle);
if (dwInfoSize > 0)
{
pData = new BYTE[dwInfoSize];
//pVerData = new BYTE[sizeof(VS_FIXEDFILEINFO)];
if (GetFileVersionInfo(pFileName, dwHandle, dwInfoSize, pData)
&& VerQueryValue(pData, L"\\", (LPVOID *)&pVerData, &len))
{
pverInfo = (VS_FIXEDFILEINFO *)pVerData;
ver = pverInfo->dwProductVersionMS;
ver <<= 32;
ver |= pverInfo->dwProductVersionLS;
if (poutFileVersion)
{
*poutFileVersion = pverInfo->dwFileVersionMS;
*poutFileVersion <<= 32;
*poutFileVersion |= pverInfo->dwFileVersionLS;
}
}
}
//
if(pData) delete[] pData;
//if(pVerData) delete pVerData;
return ver;
}
bool app::writeProfile(PCWSTR psection, PCWSTR pkey, PCWSTR pValue, PCWSTR pfileName)
{
assert(psection && pkey && pValue);
std::wstring wsini(mc_wsIniPath);
std::wstring wsname(pfileName ? pfileName : L"");
if (!wsname.empty() && wsname.find(L':') != std::wstring::npos)
{ // 全路径
wsini = wsname;
}
else
{
wsini += L"\\";
wsini += (wsname.length() > 0 ? wsname : mc_wsIniName);
}
return ::WritePrivateProfileStringW(psection, pkey, pValue, wsini.c_str()) == TRUE;
}
bool app::writeProfile(PCWSTR psection, PCWSTR pkey, int32_t iValue, PCWSTR pfileName)
{
assert(psection && pkey);
wchar_t buf[32] = {0};
std::swprintf(buf, _countof(buf), L"%d", iValue);
return writeProfile(psection, pkey, buf, pfileName);
}
bool app::writeProfile(PCWSTR psection, PCWSTR pkey, const POINT &pt, PCWSTR pfileName)
{
assert(psection && pkey);
wchar_t buf[32] = {0};
std::swprintf(buf, _countof(buf), L"%d,%d", pt.x, pt.y);
return writeProfile(psection, pkey, buf, pfileName);
}
bool app::writeProfile(PCWSTR psection, PCWSTR pkey, const RECT &rc, PCWSTR pfileName /*= nullptr*/)
{
assert(psection && pkey);
wchar_t buf[32] = {0};
std::swprintf(buf, _countof(buf), L"%s", gtc::format::rect(rc).c_str());
return writeProfile(psection, pkey, buf, pfileName);
}
bool app::writeProfile(PCWSTR psection, PCWSTR pkey, datetime &dt, PCWSTR pfileName /*= nullptr*/)
{
assert(psection && pkey);
return writeProfile(psection, pkey, dt.format().c_str());
}
bool app::readProfile(PCWSTR psection, PCWSTR pkey, std::wstring &refoutData, PCWSTR pfileName)
{
static std::wstring c_wsDefaultNone = L"{3ED02101-0407-41A3-A601-5C9936B36B5F}";
assert(psection && pkey);
std::wstring wsini(mc_wsIniPath);
std::wstring wsname(pfileName ? pfileName : L"");
if (!wsname.empty() && wsname.find(L':') != std::wstring::npos)
{ // 全路径
wsini = wsname;
}
else
{
wsini += L"\\";
wsini += (wsname.length() > 0 ? wsname : mc_wsIniName);
}
wchar_t buf[MAX_PATH] = {0};
DWORD len = GetPrivateProfileStringW(psection, pkey, c_wsDefaultNone.c_str(), buf, _countof(buf), wsini.c_str());
buf[len] = L'\0';
refoutData = buf;
bool bRet = refoutData != c_wsDefaultNone;
if(!bRet) refoutData.clear();
return bRet;
}
bool app::readProfileLarge(PCWSTR psection, PCWSTR pkey, std::wstring &refoutData, PCWSTR pfileName /*= nullptr*/)
{
static std::wstring c_wsDefaultNone = L"{3ED02101-0407-41A3-A601-5C9936B36B5F}";
assert(psection && pkey);
std::wstring wsini(mc_wsIniPath);
std::wstring wsname(pfileName ? pfileName : L"");
if (!wsname.empty() && wsname.find(L':') != std::wstring::npos)
{ // 全路径
wsini = wsname;
}
else
{
wsini += L"\\";
wsini += (wsname.length() > 0 ? wsname : mc_wsIniName);
}
wchar_t buf[4000] = {0};
DWORD len = GetPrivateProfileStringW(psection, pkey, c_wsDefaultNone.c_str(), buf, _countof(buf), wsini.c_str());
buf[len] = L'\0';
refoutData = buf;
return refoutData != c_wsDefaultNone;
}
bool app::readProfileInt(PCWSTR psection, PCWSTR pkey, int32_t &refoutData, PCWSTR pfileName, int32_t dftValue /*= 0*/)
{
assert(psection && pkey);
std::wstring wstr;
if (readProfile(psection, pkey, wstr, pfileName))
{
refoutData = std::wcstol(wstr.c_str(), NULL, 10);
return true;
}
else
{
refoutData = dftValue;
}
return false;
//std::wstring wsini(mc_wsIniPath);
//wsini += L"\\";
//wsini += (pfileName && std::wcslen(pfileName) > 0 ? pfileName : mc_wsIniName);
//return GetPrivateProfileIntW(psection, pkey, dftValue, wsini.c_str());
}
bool app::readProfilePoint(PCWSTR psection, PCWSTR pkey, POINT &refoutData, PCWSTR pfileName)
{
std::wstring wstr;
if (readProfile(psection, pkey, wstr, pfileName) && !wstr.empty())
{
refoutData = format::point(wstr.c_str());
return true;
}
return false;
}
bool app::readProfileRect(PCWSTR psection, PCWSTR pkey, RECT &refoutData, PCWSTR pfileName /*= nullptr*/)
{
std::wstring wstr;
if (readProfile(psection, pkey, wstr, pfileName) && !wstr.empty())
{
refoutData = format::rect(wstr.c_str());
return true;
}
return false;
}
bool app::readProfileDatetime(PCWSTR psection, PCWSTR pkey, datetime &refoutData, PCWSTR pfileName /*= nullptr*/)
{
std::wstring wstr;
if (readProfile(psection, pkey, wstr, pfileName) && !wstr.empty())
{
refoutData.parse(wstr.c_str());
return true;
}
return false;
}
std::wstring app::expandEnvironmentString(PCWSTR pSrc)
{
std::wstring ws;
DWORD len = ::ExpandEnvironmentStrings(pSrc, NULL, 0);
if (len > 0)
{
wchar_t *pDst = new wchar_t[len+1];
memset(pDst, 0, (len+1)*sizeof(wchar_t));
::ExpandEnvironmentStrings(pSrc, pDst, len+1);
ws = pDst;
delete[] pDst;
}
return ws;
}
BEOSVersion app::getOSVersion(OSVERSIONINFOEX *posVersion /*= NULL*/)
{
static BEOSVersion ver = BEOSVersionNone;
static OSVERSIONINFOEX osver = {0};
if(ver != BEOSVersionNone)
{
if(posVersion)
{
if(osver.dwOSVersionInfoSize == sizeof(OSVERSIONINFOEX))
memcpy_s(posVersion, sizeof(OSVERSIONINFOEX), &osver, sizeof(OSVERSIONINFOEX));
else
memcpy_s(posVersion, sizeof(OSVERSIONINFO), &osver, sizeof(OSVERSIONINFO));
}
return ver;
}
osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if(!GetVersionEx((OSVERSIONINFO *)&osver))
{
osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(!GetVersionEx((OSVERSIONINFO *)&osver)) return BEOSVersionNone;
}
SYSTEM_INFO info = {0}; //用SYSTEM_INFO结构判断64位AMD处理器
GetSystemInfo(&info); //调用GetSystemInfo函数填充结构
switch(osver.dwMajorVersion)
{
case 5:
{
switch(osver.dwMinorVersion)
{
case 1:
{
if(osver.wSuiteMask & VER_SUITE_PERSONAL)
ver = BEOSVersionXPHome;
else
ver = BEOSVersionXPProfessional;
}
break;
case 2:
{
if(osver.wProductType == VER_NT_WORKSTATION
&& info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
ver = BEOSVersionXP64;
else if(GetSystemMetrics(SM_SERVERR2)==0)
ver = BEOSVersionServer2003;
else if(GetSystemMetrics(SM_SERVERR2)!=0)
ver = BEOSVersionServer2003R2;
}
break;
}
}
break;
case 6:
{
switch(osver.dwMinorVersion)
{
case 0:
{
if(osver.wProductType == VER_NT_WORKSTATION)
ver = BEOSVersionVista;
else
ver = BEOSVersionServer2008;
}
break;
case 1:
{
if(osver.wProductType == VER_NT_WORKSTATION)
{
if(osver.wSuiteMask == (VER_SUITE_PERSONAL | VER_SUITE_SINGLEUSERTS))
ver = BEOSVersionWin7HomeBasic;
}
else
ver = BEOSVersionServer2008R2;
}
break;
case 2:
{
if(osver.wProductType == VER_NT_WORKSTATION)
ver = BEOSVersionWin8;
else
ver = BEOSVersionServer2012;
}
break;
case 3:
{
if(osver.wProductType == VER_NT_WORKSTATION)
ver = BEOSVersionWin81;
else
ver = BEOSVersionServer2012R2;
}
break;
}
}
break;
}
if(posVersion)
{
if(osver.dwOSVersionInfoSize == sizeof(OSVERSIONINFOEX))
memcpy_s(posVersion, sizeof(OSVERSIONINFOEX), &osver, sizeof(OSVERSIONINFOEX));
else
memcpy_s(posVersion, sizeof(OSVERSIONINFO), &osver, sizeof(OSVERSIONINFO));
}
return ver;
}
bool app::isWindowsXP()
{
BEOSVersion osv = getOSVersion();
return osv == BEOSVersionXPHome || osv == BEOSVersionXPProfessional || osv == BEOSVersionXP64;
}
bool app::isWindowsVistaLater()
{
OSVERSIONINFOEX osver = {0};
BEOSVersion osv = getOSVersion(&osver);
return osver.dwMajorVersion >= 6 && osver.dwMinorVersion >= 0;
}
bool app::isWindows7Later()
{
OSVERSIONINFOEX osver = {0};
BEOSVersion osv = getOSVersion(&osver);
return osver.dwMajorVersion >= 6 && osver.dwMinorVersion >= 1;
}
bool app::isWindows8Later()
{
OSVERSIONINFOEX osver = {0};
BEOSVersion osv = getOSVersion(&osver);
return osver.dwMajorVersion >= 6 && osver.dwMinorVersion >= 2;
}
bool app::isWindowsOS64()
{
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
return (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
|| si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64
|| si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ALPHA64);
//bool is64 = false;
//typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
//LPFN_ISWOW64PROCESS fnIsWow64Process;
//fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle(L"kernel32"),"IsWow64Process");
//if (fnIsWow64Process)
//{
// BOOL f64 = FALSE;
// if(fnIsWow64Process(GetCurrentProcess(),&f64)) is64 = f64 == TRUE;
//}
//else
//{
// SYSTEM_INFO info = {0}; //用SYSTEM_INFO结构判断64位AMD处理器
// GetSystemInfo(&info); //调用GetSystemInfo函数填充结构
// is64 = info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
// || info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64
// || info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ALPHA64
// || info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA32_ON_WIN64;
//}
//return is64;
}
#if defined(_WIN32)
int app::sehFilterDefault(LPEXCEPTION_POINTERS pExcept, PCWSTR pdesc, bool createDumpFile /*= true*/)
{
if (pExcept)
{
if(createDumpFile) sehUnhandleFilterDump(pExcept);
if (pExcept->ExceptionRecord)
{
gtc::app::writeVSLogs(L"SEH_<%s> code:%08X flags:%d exceptionRecord:%08X exceptionAddress:%08X NumberParameters:%d",
pdesc?pdesc:L"", pExcept->ExceptionRecord->ExceptionCode, pExcept->ExceptionRecord->ExceptionFlags, pExcept->ExceptionRecord->ExceptionRecord,
pExcept->ExceptionRecord->ExceptionAddress, pExcept->ExceptionRecord->NumberParameters);
//if(pExcept->ExceptionRecord->ExceptionFlags == 0 && pExcept->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
// return EXCEPTION_CONTINUE_EXECUTION;
return EXCEPTION_EXECUTE_HANDLER;
}
}
return EXCEPTION_EXECUTE_HANDLER;
}
LONG WINAPI app::sehUnhandleFilterDump(LPEXCEPTION_POINTERS pExcept)
{
if (pExcept)
{
std::wstring path(mc_wsLogPath.empty()?getAppPath():mc_wsLogPath);
char stimer[60] = {0};
std::time_t tm = {0};
std::time(&tm);
std::tm *ptminfo = std::localtime(&tm);
assert(ptminfo);
std::strftime(stimer, sizeof(stimer), "%Y%m%d%H%M%S.dmp", ptminfo);
path += L"\\";
path += mc_wsDumpFileName;
path += convert::toWChar(stimer);
//messageEx(L"%08X logpath:<%s>", pExcept, path.c_str());
HANDLE hFile = ::CreateFileW(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if (hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION exceptInfo = {0};
exceptInfo.ThreadId = ::GetCurrentThreadId();
exceptInfo.ExceptionPointers = pExcept;
exceptInfo.ClientPointers = FALSE;
BOOL bOK = ::MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &exceptInfo, NULL, NULL );
if (!bOK)
{
writeVSLogs(L"app::sehUnhandleFilterDump 创建dmp文件失败#%d!", ::GetLastError());
}
::CloseHandle(hFile);
}
//if(pExcept->ExceptionRecord && pExcept->ExceptionRecord->ExceptionFlags == 0 && pExcept->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
// return EXCEPTION_CONTINUE_EXECUTION;
}
return EXCEPTION_EXECUTE_HANDLER;
}
#endif
HGLOBAL app::createGlobalHandle(const void* ptr, int size, UINT nFlag /*= GMEM_SHARE|GMEM_MOVEABLE*/)
{
assert(size > 0);
if(size <= 0) return NULL;
HGLOBAL hGlobal = GlobalAlloc(nFlag, size);
if (NULL != hGlobal && ptr != NULL)
{
LPVOID pdest = GlobalLock(hGlobal);
if (NULL != pdest)
{
memcpy_s(pdest, size, ptr, size);
}
GlobalUnlock(hGlobal);
}
return hGlobal;
}
HGLOBAL app::copyGlobalMemory(HGLOBAL hDest, HGLOBAL hSource)
{
assert(hSource != NULL);
// make sure we have suitable hDest
ULONG_PTR nSize = ::GlobalSize(hSource);
if (hDest == NULL)
{
hDest = ::GlobalAlloc(GMEM_SHARE|GMEM_MOVEABLE, nSize);
if (hDest == NULL)
return NULL;
}
else if (nSize > ::GlobalSize(hDest))
{
// hDest is not large enough
return NULL;
}
// copy the bits
LPVOID lpSource = ::GlobalLock(hSource);
LPVOID lpDest = ::GlobalLock(hDest);
assert(lpDest != NULL);
assert(lpSource != NULL);
::memcpy_s(lpDest, (ULONG)::GlobalSize(hDest), lpSource, (ULONG)nSize);
::GlobalUnlock(hDest);
::GlobalUnlock(hSource);
// success -- return hDest
return hDest;
}
void app::setClipboard(const std::wstring &wstr)
{
if(wstr.size() == 0) return;
if(!OpenClipboard(NULL)) return;
::EmptyClipboard();
//std::string str = gtc::stringUtil::toChar(wstr.c_str());
HGLOBAL hg = createGlobalHandle(wstr.c_str(), (wstr.length()+1)*sizeof(wchar_t), GMEM_DDESHARE);
//HGLOBAL hg = createGlobalHandle(str.c_str(), str.length()+1, GMEM_DDESHARE);
if(!hg)
{
CloseClipboard();
return;
}
SetClipboardData(CF_UNICODETEXT,hg);
CloseClipboard();
//GlobalFree(hg);
}
void app::setClipboard(const std::string &str)
{
if(str.size() == 0) return;
if(!OpenClipboard(NULL)) return;
::EmptyClipboard();
HGLOBAL hg = createGlobalHandle(str.c_str(), str.length()+1, GMEM_DDESHARE);
//HGLOBAL hg = createGlobalHandle(str.c_str(), str.length()+1, GMEM_DDESHARE);
if(!hg)
{
CloseClipboard();
return;
}
SetClipboardData(CF_TEXT,hg);
CloseClipboard();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// thread 类 2015-3-18
// 线程操作的相关封装
////////////////////////////////////////////////////////////////////////////////////////////////////////
CRITICAL_SECTION thread::mc_csDataLock;
thread::PtrArrayType thread::mc_threadData;
template<> thread* gtc::singleton<thread>::m_pSingleton = NULL;
static thread nc_thread;
thread::thread()
{
::InitializeCriticalSectionAndSpinCount(&thread::mc_csDataLock, 4000);
}
thread::~thread()
{
::DeleteCriticalSection(&thread::mc_csDataLock);
}
// gtc::singleton
thread& thread::getSingleton()
{
return *m_pSingleton;
}
thread* thread::getSingletonPtr()
{
return m_pSingleton;
}
HANDLE thread::start(PTHREAD_START_ADDRESS pstartAddress, void *pParam, bool onlyOne /*= false*/
, bool isAutofree /*= true*/, volatile LONG *pLooplock /*= NULL*/, HANDLE hEvent /*= NULL*/, PCWSTR pDesc /*= NULL*/)
{
assert(pstartAddress);
if(!pstartAddress) return false;
// 是否只要一个运行
if(onlyOne && exists(pstartAddress)) return NULL;
_BSThreadInfo *pthr = new _BSThreadInfo();
assert(pthr);
::memset(pthr, 0, sizeof(_BSThreadInfo));
pthr->bAutofree = isAutofree;
pthr->plLooplock = pLooplock;
pthr->hEvent = hEvent;
if(pDesc) std::wcsncpy(pthr->pDescription, pDesc, _countof(pthr->pDescription));
pthr->pParameter = pParam;
pthr->pthreadFunc = pstartAddress;
pthr->hThread = (HANDLE)::_beginthreadex(NULL, 0, (unsigned int (__stdcall * ) (void *))pstartAddress, pthr, CREATE_SUSPENDED, NULL);
if (pthr->hThread)
{
::EnterCriticalSection(&mc_csDataLock);
mc_threadData.push_back(pthr);
::LeaveCriticalSection(&mc_csDataLock);
if(pthr->plLooplock)
{
assert(*pthr->plLooplock == 0);
InterlockedCompareExchange(pthr->plLooplock, 1, 0);
}
::ResumeThread(pthr->hThread);
}
else
delete pthr;
return pthr->hThread;
}
void thread::free(_BSThreadInfo *pThreadInfo)
{
assert(pThreadInfo);
if(!pThreadInfo) return ;
::EnterCriticalSection(&mc_csDataLock);
PtrArrayType::iterator iter = std::find(mc_threadData.begin(), mc_threadData.end(), pThreadInfo);
if (iter != mc_threadData.end())
{
if((*iter)->bAutofree && (*iter)->hThread) CloseHandle((*iter)->hThread);
delete *iter;
mc_threadData.erase(iter);
}
::LeaveCriticalSection(&mc_csDataLock);
}
DWORD thread::waitfor(HANDLE hThread, DWORD timeout /*= INFINITE*/)
{
assert(hThread);
DWORD dwRet = 0;
_BSThreadInfo *pThreadInfo = NULL;
::EnterCriticalSection(&mc_csDataLock);
for (PtrArrayType::iterator iter = mc_threadData.begin(); iter != mc_threadData.end(); ++iter)
{
if ((*iter)->hThread == hThread)
{
pThreadInfo = *iter;
break;
}
}
::LeaveCriticalSection(&mc_csDataLock);
if (pThreadInfo)
{
if(pThreadInfo->plLooplock) InterlockedCompareExchange(pThreadInfo->plLooplock, 0, 1);
if(pThreadInfo->hEvent) SetEvent(pThreadInfo->hEvent);
dwRet = WaitForSingleObject(pThreadInfo->hThread, timeout);
if (dwRet == WAIT_OBJECT_0)
{
if(!pThreadInfo->bAutofree) free(pThreadInfo);
}
}
return dwRet;
}
bool thread::exists(PTHREAD_START_ADDRESS pstartAddress)
{
assert(pstartAddress);
::EnterCriticalSection(&mc_csDataLock);
for (PtrArrayType::const_iterator iter = mc_threadData.cbegin(); iter != mc_threadData.cend(); ++iter)
{
if ((*iter)->pthreadFunc == pstartAddress)
{
::LeaveCriticalSection(&mc_csDataLock);
return true;
}
}
::LeaveCriticalSection(&mc_csDataLock);
return false;
}
}