-
Notifications
You must be signed in to change notification settings - Fork 3
/
sbcommon.h
4183 lines (3891 loc) · 108 KB
/
sbcommon.h
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "resource.h"
#include "comdef.h"
#include "fav.h"
#include "locale.h"
#include <sddl.h>
#include <VersionHelpers.h>
#include <strsafe.h>
#include "include/cef_version.h"
#include "mmsystem.h"
#pragma comment(lib, "winmm.lib")
class CProcessingTimeMon
{
public:
CProcessingTimeMon(LPCTSTR lpFmt)
{
m_timeCount = 0;
strFmt = lpFmt;
m_timeCount = timeGetTime();
}
~CProcessingTimeMon()
{
DWORD timeCount = timeGetTime() - m_timeCount;
if (timeCount > 20) //20ms以上
{
CString strMsg;
strMsg.Format(_T("#\t%d ms\t%s\n"), timeCount, (LPCTSTR)strFmt);
::OutputDebugString(strMsg);
}
}
private:
DWORD m_timeCount;
CString strFmt;
};
#define PROC_TIME(name) CProcessingTimeMon CPTM##name(_T(#name));
#define PROC_TIME_S(name) \
{ \
CProcessingTimeMon CPTM##name(_T(#name));
#define PROC_TIME_E(name) }
#pragma warning(disable : 4996)
#define WM_NEWINSTANCE WM_USER + 16
#define WM_ACTIVE_HEAD WM_USER + 40
#define WM_ACTIVE_TAIL WM_USER + 44
#define WM_SAVE_WND_M WM_USER + 48
#define WM_AUTH_DBL WM_USER + 56
#define WM_ACTIVE_FRM WM_USER + 60
#define WM_SCHEDULE_CEF_WORK WM_USER + 61
#define WND_TYPE_DEV_TOOLS 324929
///////////////////////////////////////////////////////////////////
//アプリ名を変更する場合は、以下を編集する。
static TCHAR gstrThisAppNameR[] = _T("Chronos");
static TCHAR gstrThisAppNameSG[] = _T("Chronos SystemGuard");
static TCHAR sgSZB_UA_START[] = _T("Mozilla/5.0 (");
// clang-format off
#define SB_CHROME_VERSION MAKE_STRING(CHROME_VERSION_MAJOR) "." \
MAKE_STRING(CHROME_VERSION_MINOR) "." \
MAKE_STRING(CHROME_VERSION_BUILD) "." \
MAKE_STRING(CHROME_VERSION_PATCH)
// clang-format on
//2021-01-07Googleにログインできない CEF経由ではNGになった。
//調査結果、FirefoxにすればOK, Edge/87.0.0.0をつけてもOK
//デフォルトのUAをEdgeに変更する対応にする。
//2021-11-30 ↑の対策がNGになっていることに気がついた。UAにEdgeをつけてもNG
static TCHAR sgSZB_UA_END[] = _T(") AppleWebKit/537.36 (KHTML, like Gecko;KA-ZUMA) Chrome/" SB_CHROME_VERSION " Safari/537.36 Chronos/SystemGuard");
#undef SB_CHROME_VERSION
typedef HRESULT(WINAPI* pfnDwmIsCompositionEnabled)(BOOL* pfEnabled);
typedef HRESULT(WINAPI* pfnDwmGetWindowAttribute)(HWND hwnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);
typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
#define WM_VIEW_INIT_OK 36002
#define WM_DELETE_WINDOW_LIST 36004
//#define WM_CLOSE_DELAY 36009
#define WM_NEW_WINDOW_URL 36011
#define WM_CLOSE_TIME_LIMIT 36014
#define WM_CLOSE_MAX_MEM 36015
#define WM_CLOSE_WAR_MEM 36016
#define WM_ADD_FAVORITE 36017
#define WM_APP_EXIT 36019
#define WM_SEL_SEARCH 36020
#define WM_COPY_IMAGE 36021
#define ID_MYCOMBO_OK 33801
#define ID_FAV_START 40000
#define ID_FAV_END 45000
#define ID_WINDOW_START 50000
#define ID_WINDOW_END 55000
#define ID_CLOSE_WINDOW_START 45001
#define ID_CLOSE_WINDOW_END 45016
#define DEBUG_LOG_LEVEL_OUTPUT_ALL 0
#define DEBUG_LOG_LEVEL_OUTPUT_NO_FILE 1
#define DEBUG_LOG_LEVEL_OUTPUT_URL 2
#define DEBUG_LOG_TYPE_GE 0 //一般情報
#define DEBUG_LOG_TYPE_DE 1 //詳細情報
#define DEBUG_LOG_TYPE_URL 2 //URL情報
#define DEBUG_LOG_TYPE_TR 3 //Browser動作情報
#define DEBUG_LOG_TYPE_CL 4 //Close処理関連情報
#define DEBUG_LOG_TYPE_JS 5 //Javascript関連情報
#define DEBUG_LOG_TYPE_EX 6 //例外処理関連情報
#define DEBUG_LOG_TYPE_AC 7 //操作アクション情報
//IE設定:0
//プロキシ無し(直接):1
//手動設定:2
#define CSG_PROXY_IE 0
#define CSG_PROXY_NA 1
#define CSG_PROXY_TF 2
//VirtualEnv
#define VE_NA 0
#define VE_THINAPP 1
#define VE_TURBO 2
//RDS
#define RDS_NA 0
#define RDS_RDP 1
#define RDS_VMWARE 2
#define RDS_CITRIX 3
#define TF_ALLOW 0
#define TF_DENY 1
static TCHAR sDEBUG_LOG_TYPE[][4] = {
_T("GE"),
_T("DE"),
_T("URL"),
_T("TR"),
_T("CL"),
_T("JS"),
_T("EX"),
_T("AC"),
};
///////////////////////////////////////////////////////////////////
static TCHAR DEF_URLS[][9] = {
_T("http://"),
_T("https://"),
_T("about:"),
_T("file://"),
_T("mailto:"),
_T("chrome:"),
};
static TCHAR DEF_FILE[][8] = {
_T("file://"),
_T("\\\\"),
};
static TCHAR DEF_SCRIPT[][11] = {
_T("javascript"),
_T("script"),
};
static TCHAR DEF_ETC_PROTOCOLS[][13] = {
_T("mailto:"),
_T("vmware-view:"),
_T("notes:"),
};
#define KEY_COMB_SHIFT 0x00000001
#define KEY_COMB_CTRL 0x00000010
#define KEY_COMB_ALT 0x00000100
#define KEY_COMB_LEFT 0x00001000
#define KEY_COMB_UP 0x00010000
#define KEY_COMB_RIGHT 0x00100000
#define KEY_COMB_DOWN 0x01000000
// ファイル時間から64ビット整数に変換
static ULONGLONG getTimeInt64(LPFILETIME ftTime)
{
ULARGE_INTEGER u64Time = {0};
u64Time.u.LowPart = ftTime->dwLowDateTime;
u64Time.u.HighPart = ftTime->dwHighDateTime;
return u64Time.QuadPart;
}
// ファイル時間の加算
static LPFILETIME addFileTime(LPFILETIME ftTime1, LPFILETIME ftTime2)
{
static FILETIME ftTime = {0};
ULARGE_INTEGER u64Time1 = {0};
ULARGE_INTEGER u64Time2 = {0};
// 1セット
u64Time1.u.LowPart = ftTime1->dwLowDateTime;
u64Time1.u.HighPart = ftTime1->dwHighDateTime;
// 2セット
u64Time2.u.LowPart = ftTime2->dwLowDateTime;
u64Time2.u.HighPart = ftTime2->dwHighDateTime;
// 加算
u64Time1.QuadPart += u64Time2.QuadPart;
// 変換
ftTime.dwLowDateTime = u64Time1.u.LowPart;
ftTime.dwHighDateTime = u64Time1.u.HighPart;
return &ftTime;
}
// ファイル時間から時間文字列に変換
static void getTimeString(LPFILETIME ftTime, BOOL bLocalTime, CString& str)
{
str.Empty();
FILETIME ltTime = {0};
SYSTEMTIME stTime = {0};
if (bLocalTime)
{
FileTimeToLocalFileTime(ftTime, <Time);
}
else
{
ltTime = *ftTime;
}
FileTimeToSystemTime(<Time, &stTime);
str.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),
stTime.wYear,
stTime.wMonth,
stTime.wDay,
stTime.wHour,
stTime.wMinute,
stTime.wSecond);
return;
}
static void getTimeStringEx(LPFILETIME ftTime, CString& str)
{
str.Empty();
FILETIME ltTime = {0};
SYSTEMTIME stTime = {0};
ltTime = *ftTime;
FileTimeToSystemTime(<Time, &stTime);
str.Format(_T("%02d:%02d:%02d (BASE%04d-%02d-%02d )"),
stTime.wHour,
stTime.wMinute,
stTime.wSecond,
stTime.wYear,
stTime.wMonth,
stTime.wDay);
return;
}
/////////////////////////////////////////////////////////////////////
#include <winternl.h>
#include <tlhelp32.h>
#include <locale.h>
#pragma comment(lib, "psapi.lib")
#include <psapi.h>
namespace SBUtil
{
enum class ICON_INDEX
{
FOLDER,
IE_FILE,
WARNING,
IE_FILE_FAV,
LOCK,
BLANK,
ZOOM,
NEW,
NEW_I,
RESTORE,
SAVE_WND,
CLOSE,
CUT,
COPY,
PASTE,
SEL_ALL,
FIND_PAGE,
IE_OPTION,
SETTINGS,
CLOSE_ALL,
CLOSE_BUT_THIS,
PREV_WND,
NEXT_WND,
CASCADE_WND,
TILE_V_WND,
TILE_H_WND,
MAXIMIZE_V_WND,
MAXIMIZE_H_WND,
MINIMIZE_ALL,
FULL_SCREEN,
APP_ABOUT,
TOPMOST_WND,
TOPMOST_REL_WND,
ADD_FAVORITE,
ORGANIZE_FAVORITE,
GO,
PASTE_GO,
PASTE_SEARCH,
SEARCH,
};
static inline void Split(CStringArray* pstrArray, LPCTSTR szTarget, LPCTSTR szDelimiter)
{
if (!szTarget)
return;
if (!szDelimiter)
return;
if (!pstrArray)
return;
pstrArray->RemoveAll();
CString strTarget(szTarget);
int intDelimiterLen = 0;
int intStart = 0;
int intEnd = 0;
strTarget += szDelimiter;
intDelimiterLen = (int)_tcslen(szDelimiter);
intStart = 0;
while (intEnd = strTarget.Find(szDelimiter, intStart), intEnd >= 0)
{
pstrArray->Add(strTarget.Mid(intStart, intEnd - intStart));
intStart = intEnd + intDelimiterLen;
}
return;
}
static inline BOOL IsURL(LPCTSTR str)
{
if (str == NULL)
return FALSE;
CString strCheckStr = str;
strCheckStr.MakeLower();
if (strCheckStr.Find(DEF_URLS[0]) == 0 ||
strCheckStr.Find(DEF_URLS[1]) == 0 ||
strCheckStr.Find(DEF_URLS[2]) == 0 ||
strCheckStr.Find(DEF_URLS[3]) == 0 ||
strCheckStr.Find(DEF_URLS[4]) == 0 ||
strCheckStr.Find(DEF_URLS[5]) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
return FALSE;
}
static inline BOOL IsURL_HTTP(LPCTSTR str)
{
if (str == NULL)
return FALSE;
CString strCheckStr = str;
if (strCheckStr.Find(DEF_URLS[0]) == 0 || //http
strCheckStr.Find(DEF_URLS[1]) == 0 //https
)
{
return TRUE;
}
else
{
return FALSE;
}
return FALSE;
}
static inline BOOL IsURL_HTTPS(LPCTSTR str)
{
if (str == NULL)
return FALSE;
CString strCheckStr = str;
if (strCheckStr.Find(DEF_URLS[1]) == 0) //https
{
return TRUE;
}
else
{
return FALSE;
}
return FALSE;
}
static inline BOOL IsURL_FILE(LPCTSTR str)
{
BOOL bRet = FALSE;
if (str == NULL)
return FALSE;
CString strCheckStr = str;
if (strCheckStr.Find(DEF_FILE[0]) == 0 ||
strCheckStr.Find(DEF_FILE[1]) == 0)
{
bRet = TRUE;
}
else
{
//2文字目が:は、パス(c:)
if (strCheckStr.Find(_T(":")) == 1)
bRet = TRUE;
}
return bRet;
}
inline static BOOL IsScript(LPCTSTR strURL)
{
if (strURL == NULL)
return FALSE;
CString strCheckStr = strURL;
strCheckStr.MakeLower();
if (strCheckStr.Find(DEF_SCRIPT[0]) == 0 ||
strCheckStr.Find(DEF_SCRIPT[1]) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
return FALSE;
}
static inline BOOL IsETC_PROTOCOL(LPCTSTR str)
{
if (str == NULL)
return FALSE;
// _T("mailto:"),
// _T("vmware-view:"),
// _T("notes:").
CString strCheckStr = str;
if (strCheckStr.Find(DEF_ETC_PROTOCOLS[0]) == 0 ||
strCheckStr.Find(DEF_ETC_PROTOCOLS[1]) == 0 ||
strCheckStr.Find(DEF_ETC_PROTOCOLS[2]) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
return FALSE;
}
static inline CString Trim_URLOnly(LPCTSTR str)
{
CString strRet;
CString strTemp;
CString strTemp2;
strTemp = str;
if (!strTemp.IsEmpty())
{
int iQPos = 0;
int iShPos = 0;
iQPos = strTemp.Find(_T("?"));
if (iQPos > -1)
{
strTemp2 = strTemp.Mid(0, iQPos);
strTemp = strTemp2;
}
iShPos = strTemp.Find(_T("#"));
if (iShPos > -1)
{
strTemp2 = strTemp.Mid(0, iShPos);
strTemp = strTemp2;
}
strRet = strTemp;
}
return strRet;
}
static void GetDivChar(LPCTSTR str, int size, CString& strRet, BOOL bAppend = TRUE)
{
strRet = str;
CString str1 = str;
int iTabStrLen = size;
LPCTSTR szEllipsis = _T("...");
if (strRet.GetLength() > iTabStrLen)
{
if (bAppend)
strRet = str1.Mid(0, iTabStrLen) + szEllipsis;
else
strRet = str1.Mid(0, iTabStrLen);
}
return;
}
static BOOL Is64BitWindows()
{
#if defined(_WIN64)
return TRUE; // 64-bit programs run only on Win64
#elif defined(_WIN32)
// 32-bit programs run on both 32-bit and 64-bit Windows
// so must sniff
BOOL f64 = FALSE;
LPFN_ISWOW64PROCESS fnIsWow64Process = {0};
HMODULE hModule = GetModuleHandle(_T("kernel32.dll"));
if (!hModule)
return FALSE;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(hModule, "IsWow64Process");
if (NULL != fnIsWow64Process)
return fnIsWow64Process(GetCurrentProcess(), &f64) && f64;
return FALSE;
#else
return FALSE; // Win64 does not support Win16
#endif
}
static inline BOOL IsWindowsServerRDS()
{
return IsWindowsServer() && GetSystemMetrics(SM_REMOTESESSION);
}
static inline void SafeGetWindowText(HWND hWnd, CString& str)
{
CString strText;
int nLen = ::GetWindowTextLength(hWnd);
int nRetLen = ::GetWindowText(hWnd, strText.GetBufferSetLength(nLen + 2), nLen + 2);
strText.ReleaseBuffer();
if (nRetLen < nLen)
{
str.Empty();
return;
}
str = strText;
return;
}
static inline void GetInternetShortcutUrl(LPCTSTR pszFolderURLFile, CString& strVal)
{
TCHAR szURL[4096] = {0};
DWORD nSize = 4096;
::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"), NULL, szURL, nSize, pszFolderURLFile);
strVal = szURL;
if (!SBUtil::IsURL_HTTP(strVal))
{
strVal.Empty();
}
return;
}
static CString GetDownloadFolderPath()
{
CString strRet;
TCHAR* path = 0;
SHGetKnownFolderPath(FOLDERID_Downloads, 0, NULL, &path);
strRet = path;
CoTaskMemFree(path);
return strRet;
}
static CString GetLocalAppDataPath()
{
CString strRet;
TCHAR* path = 0;
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &path);
strRet = path;
CoTaskMemFree(path);
return strRet;
}
static CString GetValidFileName(LPCTSTR lpFileName)
{
CString strRet(lpFileName);
strRet.TrimRight();
strRet.TrimLeft();
if (!strRet.IsEmpty())
{
//ファイル名に使えない文字を置き換える。
strRet.Replace(_T("<"), _T("<"));
strRet.Replace(_T(">"), _T(">"));
strRet.Replace(_T(":"), _T(":"));
strRet.Replace(_T("\""), _T("”"));
strRet.Replace(_T("/"), _T("-"));
strRet.Replace(_T("\\"), _T("¥"));
strRet.Replace(_T("|"), _T("|"));
strRet.Replace(_T("?"), _T("?"));
strRet.Replace(_T("*"), _T("*"));
}
return strRet;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static CString MyConvertSidToStringSid(PSID pSid)
{
LPTSTR p = NULL;
BOOL br = ::ConvertSidToStringSid(pSid, &p);
if (!br)
{
return _T("");
}
CString strResult = p;
::LocalFree(p);
return strResult;
}
static CString GetTokenStringSid(HANDLE hToken)
{
DWORD dwTokenUserLength = 0;
::GetTokenInformation(hToken, TokenUser, 0, 0, &dwTokenUserLength);
TOKEN_USER* pTokenUser = (TOKEN_USER*)::LocalAlloc(LMEM_FIXED, dwTokenUserLength);
if (pTokenUser == NULL)
{
return _T("");
}
BOOL br = ::GetTokenInformation(hToken, TokenUser, pTokenUser, dwTokenUserLength, &dwTokenUserLength);
if (!br)
{
::LocalFree(pTokenUser);
return _T("");
}
CString strResult = MyConvertSidToStringSid(pTokenUser->User.Sid);
::LocalFree(pTokenUser);
return strResult;
}
static CString GetProcessStringSid(HANDLE hProcess)
{
HANDLE hToken = NULL;
BOOL br = ::OpenProcessToken(hProcess, TOKEN_QUERY, &hToken);
if (!br)
{
return _T("");
}
CString strResult = GetTokenStringSid(hToken);
::CloseHandle(hToken);
return strResult;
}
// 現在の(プロセスの)ユーザのSIDの取得
static CString GetCurrentProcessStringSid()
{
HANDLE hProcess = ::GetCurrentProcess();
return GetProcessStringSid(hProcess);
}
static bool GetMonitorWorkRectM(HMONITOR hMon, LPRECT prcWork, LPRECT prcMonitor = NULL)
{
MONITORINFO mi = {0};
::ZeroMemory(&mi, sizeof(mi));
mi.cbSize = sizeof(mi);
::GetMonitorInfo(hMon, &mi);
if (NULL != prcWork)
*prcWork = mi.rcWork;
if (NULL != prcMonitor)
*prcMonitor = mi.rcMonitor;
return (mi.dwFlags == MONITORINFOF_PRIMARY) ? true : false;
}
static bool GetMonitorWorkRect(HWND hWnd, LPRECT prcWork, LPRECT prcMonitor = NULL)
{
HMONITOR hMon = ::MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
return GetMonitorWorkRectM(hMon, prcWork, prcMonitor);
}
static bool GetMonitorWorkRectRC(LPCRECT prc, LPRECT prcWork, LPRECT prcMonitor = NULL)
{
HMONITOR hMon = ::MonitorFromRect(prc, MONITOR_DEFAULTTONEAREST);
return GetMonitorWorkRectM(hMon, prcWork, prcMonitor);
}
static bool GetMonitorWorkRectPT(POINT pt, LPRECT prcWork, LPRECT prcMonitor = NULL)
{
HMONITOR hMon = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
return GetMonitorWorkRectM(hMon, prcWork, prcMonitor);
}
static void SetAbsoluteForegroundWindow(HWND hWnd, BOOL bEnforceActive)
{
int nTargetID, nForegroundID = 0;
INT_PTR sp_time = 0;
// フォアグラウンドウィンドウを作成したスレッドのIDを取得
nForegroundID = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
// 目的のウィンドウを作成したスレッドのIDを取得
nTargetID = GetWindowThreadProcessId(hWnd, NULL);
// スレッドのインプット状態を結び付ける
AttachThreadInput(nTargetID, nForegroundID, TRUE); // TRUE で結び付け
// 現在の設定を sp_time に保存
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &sp_time, 0);
// ウィンドウの切り替え時間を 0ms にする
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)0, 0);
if (bEnforceActive)
{
// ウィンドウをフォアグラウンドに持ってくる
if (::IsIconic(hWnd))
{
ShowWindow(hWnd, SW_HIDE);
ShowWindow(hWnd, SW_NORMAL);
}
else if (::IsZoomed(hWnd))
{
ShowWindow(hWnd, SW_HIDE);
ShowWindow(hWnd, SW_MAXIMIZE);
}
else
{
ShowWindow(hWnd, SW_HIDE);
ShowWindow(hWnd, SW_NORMAL);
}
}
SetForegroundWindow(hWnd);
// 設定を元に戻す
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)sp_time, 0);
// スレッドのインプット状態を切り離す
AttachThreadInput(nTargetID, nForegroundID, FALSE); // FALSE で切り離し
}
// Usually IsWindowsXXOrGreater version helper API should be used,
// but there is no IsWindows11OrGreater API yet.
// NOTE: GetVersionEx return information which depends on manifest,
// so use RtlGetVersion API.
static inline bool IsWindows11OrLater()
{
auto versionInfo = RTL_OSVERSIONINFOW{sizeof(RTL_OSVERSIONINFOW), 0, 0, 0, 0, 0};
auto hModule = GetModuleHandle(L"ntdll.dll");
if (!hModule)
{
return false;
}
typedef NTSTATUS(WINAPI * RtlGetVersionPtr)(PRTL_OSVERSIONINFOW lpVersionInformation);
auto RtlGetVersion = (RtlGetVersionPtr)GetProcAddress(hModule, "RtlGetVersion");
if (!RtlGetVersion)
{
return false;
}
if (RtlGetVersion(&versionInfo))
{
return false;
}
return (versionInfo.dwMajorVersion >= 11 || // for future version
(versionInfo.dwMajorVersion == 10 && versionInfo.dwBuildNumber >= 22000)); // for Windows 11
}
/////////////////////////////////////////////////////////////////
struct RTL_USER_PROCESS_PARAMETERS_I
{
BYTE Reserved1[16];
PVOID Reserved2[10];
UNICODE_STRING ImagePathName;
UNICODE_STRING CommandLine;
};
struct PEB_INTERNAL
{
BYTE Reserved1[2];
BYTE BeingDebugged;
BYTE Reserved2[1];
PVOID Reserved3[2];
struct PEB_LDR_DATA* Ldr;
RTL_USER_PROCESS_PARAMETERS_I* ProcessParameters;
BYTE Reserved4[104];
PVOID Reserved5[52];
struct PS_POST_PROCESS_INIT_ROUTINE* PostProcessInitRoutine;
BYTE Reserved6[128];
PVOID Reserved7[1];
ULONG SessionId;
};
typedef NTSTATUS(NTAPI* NtQueryInformationProcessPtr)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL);
typedef ULONG(NTAPI* RtlNtStatusToDosErrorPtr)(NTSTATUS Status);
static SIZE_T GetRemoteCommandLineW(HANDLE hProcess, LPWSTR pszBuffer, UINT bufferLength)
{
HINSTANCE hNtDll = GetModuleHandleW(L"ntdll.dll");
if (!hNtDll)
return 0;
NtQueryInformationProcessPtr NtQueryInformationProcess = (NtQueryInformationProcessPtr)GetProcAddress(hNtDll, "NtQueryInformationProcess");
RtlNtStatusToDosErrorPtr RtlNtStatusToDosError = (RtlNtStatusToDosErrorPtr)GetProcAddress(hNtDll, "RtlNtStatusToDosError");
if (!NtQueryInformationProcess || !RtlNtStatusToDosError)
{
return 0;
}
// Get PROCESS_BASIC_INFORMATION
PROCESS_BASIC_INFORMATION pbi = {0};
ULONG len = 0;
NTSTATUS status = NtQueryInformationProcess(
hProcess, ProcessBasicInformation, &pbi, sizeof(pbi), &len);
SetLastError(RtlNtStatusToDosError(status));
if (NT_ERROR(status) || !pbi.PebBaseAddress)
{
return 0;
}
// Read PEB memory block
SIZE_T bytesRead = 0;
PEB_INTERNAL peb = {0};
if (!ReadProcessMemory(hProcess, pbi.PebBaseAddress, &peb, sizeof(peb), &bytesRead))
{
return 0;
}
// Obtain size of commandline string
RTL_USER_PROCESS_PARAMETERS_I upp = {0};
if (!ReadProcessMemory(hProcess, peb.ProcessParameters, &upp, sizeof(upp), &bytesRead))
{
return 0;
}
if (!upp.CommandLine.Length)
{
return 0;
}
// Check the buffer size
DWORD dwNeedLength = (upp.CommandLine.Length + 1) / sizeof(wchar_t) + 1;
if (bufferLength < dwNeedLength)
{
return dwNeedLength;
}
pszBuffer[dwNeedLength - 1] = L'\0';
if (!ReadProcessMemory(hProcess, upp.CommandLine.Buffer, pszBuffer, upp.CommandLine.Length, &bytesRead))
{
return 0;
}
return bytesRead / sizeof(wchar_t);
}
static CString GetCommandLineData(DWORD dwPID)
{
CString strRet;
if (dwPID == 0) return strRet;
try
{
HANDLE processHandle = {0};
std::unique_ptr<WCHAR> szCommandLine(new WCHAR[8192]());
processHandle = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, dwPID);
if (processHandle)
{
LPWSTR rawSzCommandLine = szCommandLine.get();
GetRemoteCommandLineW(processHandle, rawSzCommandLine, 8192);
CloseHandle(processHandle);
CStringW strW(rawSzCommandLine);
strW.TrimLeft();
strW.TrimRight();
if (!strW.IsEmpty())
strRet = strW;
}
}
catch (...)
{
ATLASSERT(0);
}
return strRet;
}
}; // namespace SBUtil
//////////////////////////////////////////////////////////////////////
static int wildcmp(const char* wild, const char* string)
{
const char *cp = NULL, *mp = NULL;
while ((*string) && (*wild != '*'))
{
if ((*wild != *string) && (*wild != '?'))
{
return 0;
}
wild++;
string++;
}
while (*string)
{
if (*wild == '*')
{
if (!*++wild)
{
return 1;
}
mp = wild;
cp = string + 1;
}
else if ((*wild == *string) || (*wild == '?'))
{
wild++;
string++;
}
else
{
wild = mp;
string = cp++;
}
}
while (*wild == '*')
{
wild++;
}
return !*wild;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define EXTVAL(name) _ExTxtVal(_T(#name), name);
static TCHAR REG_SETTING_KEY[] = _T("SOFTWARE\\ChronosSystemGuard");
class AppSettings
{
public:
AppSettings()
{
m_IsSGMode = FALSE;
Clear();
}
~AppSettings() {}
enum class MediaAccessPermission
{
NO_MEDIA_ACCESS,
MANUAL_MEDIA_APPROVAL,
DEFAULT_MEDIA_APPROVAL
};
void Clear()
{
EnableAdvancedLogMode = 0;
EnableAdvancedLogVerboseMode = 0;
AdvancedLogLevel = 0;
ShowLogo = 0;
EnableTab = 0;
EnablePDFExtension = 0;
EnableMemcache = 0;
EnableGPURendering = 0;
EnableCrashRecovery = 0;
EnableMultipleInstance = 0;
EnableRebar = 0;
EnableStatusbar = 0;
WideMargin = 0;
HeightMargin = 0;
DefaultZoomSize = 0;
RedirectMsgTimeout = 0;
KeyCombination = 0;
ProxyType = CSG_PROXY_IE; //0
StartURL.Empty();
EnforceInitParam.Empty();
CustomBrowser.Empty();
CustomBrowser2.Empty();
CustomBrowser3.Empty();
CustomBrowser4.Empty();
CustomBrowser5.Empty();
InitMessage.Empty();
ProxyAddress.Empty();
ProxyBypassAddress.Empty();
UserAgentAppendStr.Empty();
MemoryUsageLimit = 0;
WindowCountLimit = 0;
EnableMediaAccessByApproval = static_cast<int>(AppSettings::MediaAccessPermission::NO_MEDIA_ACCESS);
EnableMediaAccessPermission = static_cast<int>(AppSettings::MediaAccessPermission::NO_MEDIA_ACCESS);
EnableDownloadRestriction = 0;
EnableUploadRestriction = 0;
EnableRunningTime = 0;
RunningLimitTime = 0;
EnableURLRedirect = 0;
EnableURLFilter = 0;
EnableCustomScript = 0;
EnableLogging = 0;
EnableUploadLogging = 0;
EnableDownloadLogging = 0;
EnableBrowsingLogging = 0;
EnableAccessAllLogging = 0;
LogServerURL.Empty();
LogMethod.Empty();
RequestHeader.Empty();
//ChFiler---------------------------------
RootPath.Empty();
UploadBasePath.Empty();
ExtFilter.Empty();
EnableOpendOp = 0;
EnableTransferLog = 0;
EnableUploadSync = 0;
EnableUploadSyncMirror = 0;
UploadSyncInterval = 0;
EnableAutoTransfer = 0;
DisableOpendOpAlert = 0;
DisableExitOpAlert = 0;
ConfirmAutoRefresh = 0;
TransferPath.Empty();
TransferSubFolder.Empty();
ShowUploadTab = 0;
UploadPath.Empty();
DisallowExt.Empty();
unZipMessage.Empty();
ExitMessage.Empty();
//ChTaskMGR---------------------------------
LABEL_TYPE = 0;
LABEL_CHK_INTERVAL = 0;
LABEL_ALPHA_BLEND = 0;
TASK_LIST_TYPE = 0;
TASK_LIST_MODE_DETAIL = 0;
}