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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
|
// Windows Template Library - WTL version 10.0
// Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Microsoft Public License (http://opensource.org/licenses/MS-PL)
// which can be found in the file MS-PL.txt at the root folder.
#ifndef __ATLAPP_H__
#define __ATLAPP_H__
#pragma once
#ifndef __cplusplus
#error WTL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLBASE_H__
#error atlapp.h requires atlbase.h to be included first
#endif
#ifdef _WIN32_WCE
#error WTL10 doesn't support Windows CE
#endif
#ifdef _ATL_NO_COMMODULE
#error WTL doesn't support _ATL_NO_COMMODULE
#endif
#ifdef _ATL_NO_WIN_SUPPORT
#error WTL doesn't support _ATL_NO_WIN_SUPPORT
#endif
#if (_MSC_VER < 1400)
#error WTL10 requires C++ compiler version 14 (Visual C++ 2005) or higher
#endif
#if (WINVER < 0x0501)
#error WTL requires WINVER >= 0x0501
#endif
#if (_WIN32_WINNT < 0x0501)
#error WTL requires _WIN32_WINNT >= 0x0501
#endif
#if (_WIN32_IE < 0x0600)
#error WTL requires _WIN32_IE >= 0x0600
#endif
#if (_ATL_VER < 0x0800)
#error WTL10 requires ATL version 8 or higher
#endif
#ifdef _ATL_MIN_CRT
#error WTL10 doesn't support _ATL_MIN_CRT
#endif
#ifdef _ATL_NO_MSIMG
#error WTL10 doesn't support _ATL_NO_MSIMG
#endif
#include <limits.h>
#ifdef _MT
#include <process.h> // for _beginthreadex
#endif
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#include <commdlg.h>
#include <shellapi.h>
// Check for VS2005 without newer WinSDK
#if (_MSC_VER == 1400) && !defined(RB_GETEXTENDEDSTYLE)
#error WTL10 requires WinSDK 6.0 ot higher
#endif
#include <uxtheme.h>
#pragma comment(lib, "uxtheme.lib")
#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE)
#include <VersionHelpers.h>
#endif
#include "atlres.h"
///////////////////////////////////////////////////////////////////////////////
// WTL version number
#define _WTL_VER 0x1000 // version 10.0
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CMessageFilter
// CIdleHandler
// CMessageLoop
//
// CAppModule
// CServerAppModule
//
// Global functions:
// AtlInitCommonControls()
// AtlGetDefaultGuiFont()
// AtlCreateControlFont()
// AtlCreateBoldFont()
// AtlGetStringPtr()
///////////////////////////////////////////////////////////////////////////////
// Miscellaneous global support
// define useful macros from winuser.h
#ifndef IS_INTRESOURCE
#define IS_INTRESOURCE(_r) (((ULONG_PTR)(_r) >> 16) == 0)
#endif // IS_INTRESOURCE
// protect template members from windowsx.h macros
#ifdef _INC_WINDOWSX
#undef SubclassWindow
#endif // _INC_WINDOWSX
// define useful macros from windowsx.h
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lParam) ((int)(short)LOWORD(lParam))
#endif
#ifndef GET_Y_LPARAM
#define GET_Y_LPARAM(lParam) ((int)(short)HIWORD(lParam))
#endif
// Dummy structs for compiling with /CLR
#ifdef _MANAGED
__if_not_exists(_IMAGELIST::_IMAGELIST) { struct _IMAGELIST { }; }
__if_not_exists(_TREEITEM::_TREEITEM) { struct _TREEITEM { }; }
__if_not_exists(_PSP::_PSP) { struct _PSP { }; }
#endif
// Forward declaration for ATL11 fix
#if (_ATL_VER >= 0x0B00)
namespace ATL { HRESULT AtlGetCommCtrlVersion(LPDWORD pdwMajor, LPDWORD pdwMinor); }
#endif
#ifndef WM_MOUSEHWHEEL
#define WM_MOUSEHWHEEL 0x020E
#endif
// Used for stack allocations with ATL::CTempBuffer
#ifndef _WTL_STACK_ALLOC_THRESHOLD
#define _WTL_STACK_ALLOC_THRESHOLD 512
#endif
namespace WTL
{
DECLARE_TRACE_CATEGORY(atlTraceUI)
#ifdef _DEBUG
__declspec(selectany) ATL::CTraceCategory atlTraceUI(_T("atlTraceUI"));
#endif // _DEBUG
// Common Controls initialization helper
inline BOOL AtlInitCommonControls(DWORD dwFlags)
{
INITCOMMONCONTROLSEX iccx = { sizeof(INITCOMMONCONTROLSEX), dwFlags };
BOOL bRet = ::InitCommonControlsEx(&iccx);
ATLASSERT(bRet);
return bRet;
}
// Default GUI font helper - "MS Shell Dlg" stock font
inline HFONT AtlGetDefaultGuiFont()
{
return (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
}
// Control font helper - default font for controls not in a dialog
// (NOTE: Caller owns the font, and should destroy it when it's no longer needed)
inline HFONT AtlCreateControlFont()
{
LOGFONT lf = {};
ATLVERIFY(::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &lf, 0) != FALSE);
HFONT hFont = ::CreateFontIndirect(&lf);
ATLASSERT(hFont != NULL);
return hFont;
}
// Bold font helper
// (NOTE: Caller owns the font, and should destroy it when it's no longer needed)
inline HFONT AtlCreateBoldFont(HFONT hFont = NULL)
{
LOGFONT lf = {};
if(hFont == NULL)
ATLVERIFY(::SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &lf, 0) != FALSE);
else
ATLVERIFY(::GetObject(hFont, sizeof(LOGFONT), &lf) == sizeof(LOGFONT));
lf.lfWeight = FW_BOLD;
HFONT hFontBold = ::CreateFontIndirect(&lf);
ATLASSERT(hFontBold != NULL);
return hFontBold;
}
// Resource string pointer
inline LPCWSTR AtlGetStringPtr(UINT uID, int* pch = NULL)
{
LPCWSTR lpstr = NULL;
int nRet = ::LoadStringW(ATL::_AtlBaseModule.GetResourceInstance(), uID, (LPWSTR)&lpstr, 0);
if(pch != NULL)
*pch = nRet;
return lpstr;
}
///////////////////////////////////////////////////////////////////////////////
// RunTimeHelper - helper functions for Windows version and structure sizes
#ifndef _WTL_NO_RUNTIME_STRUCT_SIZE
#ifndef _SIZEOF_STRUCT
#define _SIZEOF_STRUCT(structname, member) (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
#endif
#if (_WIN32_WINNT >= 0x0600) && !defined(REBARBANDINFO_V6_SIZE)
#define REBARBANDINFO_V6_SIZE _SIZEOF_STRUCT(REBARBANDINFO, cxHeader)
#endif // (_WIN32_WINNT >= 0x0600) && !defined(REBARBANDINFO_V6_SIZE)
#if (_WIN32_WINNT >= 0x0600) && !defined(LVGROUP_V5_SIZE)
#define LVGROUP_V5_SIZE _SIZEOF_STRUCT(LVGROUP, uAlign)
#endif // (_WIN32_WINNT >= 0x0600) && !defined(LVGROUP_V5_SIZE)
#if (_WIN32_WINNT >= 0x0600) && !defined(LVTILEINFO_V5_SIZE)
#define LVTILEINFO_V5_SIZE _SIZEOF_STRUCT(LVTILEINFO, puColumns)
#endif // (_WIN32_WINNT >= 0x0600) && !defined(LVTILEINFO_V5_SIZE)
#if defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN) && !defined(MCHITTESTINFO_V1_SIZE)
#define MCHITTESTINFO_V1_SIZE _SIZEOF_STRUCT(MCHITTESTINFO, st)
#endif // defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN) && !defined(MCHITTESTINFO_V1_SIZE)
#if (WINVER >= 0x0600) && !defined(NONCLIENTMETRICS_V1_SIZE)
#define NONCLIENTMETRICS_V1_SIZE _SIZEOF_STRUCT(NONCLIENTMETRICS, lfMessageFont)
#endif // (WINVER >= 0x0600) && !defined(NONCLIENTMETRICS_V1_SIZE)
#ifndef TTTOOLINFO_V2_SIZE
#define TTTOOLINFO_V2_SIZE _SIZEOF_STRUCT(TTTOOLINFO, lParam)
#endif
#endif // !_WTL_NO_RUNTIME_STRUCT_SIZE
namespace RunTimeHelper
{
inline bool IsCommCtrl6()
{
DWORD dwMajor = 0, dwMinor = 0;
HRESULT hRet = ATL::AtlGetCommCtrlVersion(&dwMajor, &dwMinor);
return (SUCCEEDED(hRet) && (dwMajor >= 6));
}
inline bool IsVista()
{
#ifdef _versionhelpers_H_INCLUDED_
return ::IsWindowsVistaOrGreater();
#else // !_versionhelpers_H_INCLUDED_
OSVERSIONINFO ovi = { sizeof(OSVERSIONINFO) };
BOOL bRet = ::GetVersionEx(&ovi);
return ((bRet != FALSE) && (ovi.dwMajorVersion >= 6));
#endif // _versionhelpers_H_INCLUDED_
}
inline bool IsThemeAvailable()
{
return IsCommCtrl6() && (::IsThemeActive() != FALSE) && (::IsAppThemed() != FALSE);
}
inline bool IsWin7()
{
#ifdef _versionhelpers_H_INCLUDED_
return ::IsWindows7OrGreater();
#else // !_versionhelpers_H_INCLUDED_
OSVERSIONINFO ovi = { sizeof(OSVERSIONINFO) };
BOOL bRet = ::GetVersionEx(&ovi);
return ((bRet != FALSE) && ((ovi.dwMajorVersion > 6) || ((ovi.dwMajorVersion == 6) && (ovi.dwMinorVersion >= 1))));
#endif // _versionhelpers_H_INCLUDED_
}
inline bool IsRibbonUIAvailable()
{
static INT iRibbonUI = -1;
#if defined(NTDDI_WIN7) && (NTDDI_VERSION >= NTDDI_WIN7)
if (iRibbonUI == -1)
{
HMODULE hRibbonDLL = ::LoadLibrary(_T("propsys.dll"));
if (hRibbonDLL != NULL)
{
const GUID CLSID_UIRibbonFramework = { 0x926749fa, 0x2615, 0x4987, { 0x88, 0x45, 0xc3, 0x3e, 0x65, 0xf2, 0xb9, 0x57 } };
// block - create instance
{
ATL::CComPtr<IUnknown> pIUIFramework;
iRibbonUI = SUCCEEDED(pIUIFramework.CoCreateInstance(CLSID_UIRibbonFramework)) ? 1 : 0;
}
::FreeLibrary(hRibbonDLL);
}
else
{
iRibbonUI = 0;
}
}
#endif // defined(NTDDI_WIN7) && (NTDDI_VERSION >= NTDDI_WIN7)
return (iRibbonUI == 1);
}
inline UINT SizeOf_REBARBANDINFO()
{
UINT uSize = sizeof(REBARBANDINFO);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
if(!(IsVista() && IsCommCtrl6()))
uSize = REBARBANDINFO_V6_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
return uSize;
}
inline UINT SizeOf_LVGROUP()
{
UINT uSize = sizeof(LVGROUP);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
if(!IsVista())
uSize = LVGROUP_V5_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
return uSize;
}
inline UINT SizeOf_LVTILEINFO()
{
UINT uSize = sizeof(LVTILEINFO);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
if(!IsVista())
uSize = LVTILEINFO_V5_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (_WIN32_WINNT >= 0x0600)
return uSize;
}
inline UINT SizeOf_MCHITTESTINFO()
{
UINT uSize = sizeof(MCHITTESTINFO);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
if(!(IsVista() && IsCommCtrl6()))
uSize = MCHITTESTINFO_V1_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && defined(NTDDI_VERSION) && (NTDDI_VERSION >= NTDDI_LONGHORN)
return uSize;
}
inline UINT SizeOf_NONCLIENTMETRICS()
{
UINT uSize = sizeof(NONCLIENTMETRICS);
#if !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
if(!IsVista())
uSize = NONCLIENTMETRICS_V1_SIZE;
#endif // !defined(_WTL_NO_RUNTIME_STRUCT_SIZE) && (WINVER >= 0x0600)
return uSize;
}
inline UINT SizeOf_TOOLINFO()
{
UINT uSize = sizeof(TOOLINFO);
#ifndef _WTL_NO_RUNTIME_STRUCT_SIZE
if(!IsVista())
uSize = TTTOOLINFO_V2_SIZE;
#endif
return uSize;
}
} // namespace RunTimeHelper
///////////////////////////////////////////////////////////////////////////////
// ModuleHelper - helper functions for ATL (deprecated)
namespace ModuleHelper
{
inline HINSTANCE GetModuleInstance()
{
return ATL::_AtlBaseModule.GetModuleInstance();
}
inline HINSTANCE GetResourceInstance()
{
return ATL::_AtlBaseModule.GetResourceInstance();
}
inline void AddCreateWndData(ATL::_AtlCreateWndData* pData, void* pObject)
{
ATL::_AtlWinModule.AddCreateWndData(pData, pObject);
}
inline void* ExtractCreateWndData()
{
return ATL::_AtlWinModule.ExtractCreateWndData();
}
} // namespace ModuleHelper
///////////////////////////////////////////////////////////////////////////////
// SecureHelper - WTL10 requires use of secure functions
// these are here only for compatibility with existing projects
namespace SecureHelper
{
inline void strcpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
{
ATL::Checked::strcpy_s(lpstrDest, cchDest, lpstrSrc);
}
inline void strcpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
{
ATL::Checked::wcscpy_s(lpstrDest, cchDest, lpstrSrc);
}
inline void strcpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
{
#ifdef _UNICODE
strcpyW_x(lpstrDest, cchDest, lpstrSrc);
#else
strcpyA_x(lpstrDest, cchDest, lpstrSrc);
#endif
}
inline errno_t strncpyA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc, size_t cchCount)
{
return ATL::Checked::strncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
}
inline errno_t strncpyW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc, size_t cchCount)
{
return ATL::Checked::wcsncpy_s(lpstrDest, cchDest, lpstrSrc, cchCount);
}
inline errno_t strncpy_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc, size_t cchCount)
{
#ifdef _UNICODE
return strncpyW_x(lpstrDest, cchDest, lpstrSrc, cchCount);
#else
return strncpyA_x(lpstrDest, cchDest, lpstrSrc, cchCount);
#endif
}
inline void strcatA_x(char* lpstrDest, size_t cchDest, const char* lpstrSrc)
{
ATL::Checked::strcat_s(lpstrDest, cchDest, lpstrSrc);
}
inline void strcatW_x(wchar_t* lpstrDest, size_t cchDest, const wchar_t* lpstrSrc)
{
ATL::Checked::wcscat_s(lpstrDest, cchDest, lpstrSrc);
}
inline void strcat_x(LPTSTR lpstrDest, size_t cchDest, LPCTSTR lpstrSrc)
{
#ifdef _UNICODE
strcatW_x(lpstrDest, cchDest, lpstrSrc);
#else
strcatA_x(lpstrDest, cchDest, lpstrSrc);
#endif
}
inline void memcpy_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
{
ATL::Checked::memcpy_s(pDest, cbDest, pSrc, cbSrc);
}
inline void memmove_x(void* pDest, size_t cbDest, const void* pSrc, size_t cbSrc)
{
ATL::Checked::memmove_s(pDest, cbDest, pSrc, cbSrc);
}
inline int vsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
{
return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
}
inline int wvsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, va_list args)
{
return _vstprintf_s(lpstrBuff, cchBuff, lpstrFormat, args);
}
inline int sprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
{
va_list args;
va_start(args, lpstrFormat);
int nRes = vsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
va_end(args);
return nRes;
}
inline int wsprintf_x(LPTSTR lpstrBuff, size_t cchBuff, LPCTSTR lpstrFormat, ...)
{
va_list args;
va_start(args, lpstrFormat);
int nRes = wvsprintf_x(lpstrBuff, cchBuff, lpstrFormat, args);
va_end(args);
return nRes;
}
} // namespace SecureHelper
///////////////////////////////////////////////////////////////////////////////
// MinCrtHelper - WTL10 doesn't support _ATL_MIN_CRT,
// these are here only for compatibility with existing projects
namespace MinCrtHelper
{
inline int _isspace(TCHAR ch)
{
return _istspace(ch);
}
inline int _isdigit(TCHAR ch)
{
return _istdigit(ch);
}
inline int _atoi(LPCTSTR str)
{
return _ttoi(str);
}
inline LPCTSTR _strrchr(LPCTSTR str, TCHAR ch)
{
return _tcsrchr(str, ch);
}
inline LPTSTR _strrchr(LPTSTR str, TCHAR ch)
{
return _tcsrchr(str, ch);
}
} // namespace MinCrtHelper
///////////////////////////////////////////////////////////////////////////////
// GenericWndClass - generic window class usable for subclassing
// Use in dialog templates to specify a placeholder to be subclassed
// Specify as a custom control with class name WTL_GenericWindow
// Call Rregister() before creating dialog (for example, in WinMain)
namespace GenericWndClass
{
inline LPCTSTR GetName()
{
return _T("WTL_GenericWindow");
}
inline ATOM Register()
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
wc.lpfnWndProc = ::DefWindowProc;
wc.hInstance = ModuleHelper::GetModuleInstance();
wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = GetName();
ATOM atom = ::RegisterClassEx(&wc);
ATLASSERT(atom != 0);
return atom;
}
inline BOOL Unregister() // only needed for DLLs or tmp use
{
return ::UnregisterClass(GetName(), ModuleHelper::GetModuleInstance());
}
} // namespace GenericWndClass
///////////////////////////////////////////////////////////////////////////////
// CMessageFilter - Interface for message filter support
class ATL_NO_VTABLE CMessageFilter
{
public:
virtual BOOL PreTranslateMessage(MSG* pMsg) = 0;
};
///////////////////////////////////////////////////////////////////////////////
// CIdleHandler - Interface for idle processing
class ATL_NO_VTABLE CIdleHandler
{
public:
virtual BOOL OnIdle() = 0;
};
///////////////////////////////////////////////////////////////////////////////
// CMessageLoop - message loop implementation
class CMessageLoop
{
public:
ATL::CSimpleArray<CMessageFilter*> m_aMsgFilter;
ATL::CSimpleArray<CIdleHandler*> m_aIdleHandler;
MSG m_msg;
CMessageLoop()
{
memset(&m_msg, 0, sizeof(m_msg));
}
virtual ~CMessageLoop()
{ }
// Message filter operations
BOOL AddMessageFilter(CMessageFilter* pMessageFilter)
{
return m_aMsgFilter.Add(pMessageFilter);
}
BOOL RemoveMessageFilter(CMessageFilter* pMessageFilter)
{
return m_aMsgFilter.Remove(pMessageFilter);
}
// Idle handler operations
BOOL AddIdleHandler(CIdleHandler* pIdleHandler)
{
return m_aIdleHandler.Add(pIdleHandler);
}
BOOL RemoveIdleHandler(CIdleHandler* pIdleHandler)
{
return m_aIdleHandler.Remove(pIdleHandler);
}
// message loop
int Run()
{
BOOL bDoIdle = TRUE;
int nIdleCount = 0;
BOOL bRet = FALSE;
for(;;)
{
while(bDoIdle && !::PeekMessage(&m_msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!OnIdle(nIdleCount++))
bDoIdle = FALSE;
}
bRet = ::GetMessage(&m_msg, NULL, 0, 0);
if(bRet == -1)
{
ATLTRACE2(atlTraceUI, 0, _T("::GetMessage returned -1 (error)\n"));
continue; // error, don't process
}
else if(!bRet)
{
ATLTRACE2(atlTraceUI, 0, _T("CMessageLoop::Run - exiting\n"));
break; // WM_QUIT, exit message loop
}
if(!PreTranslateMessage(&m_msg))
{
::TranslateMessage(&m_msg);
::DispatchMessage(&m_msg);
}
if(IsIdleMessage(&m_msg))
{
bDoIdle = TRUE;
nIdleCount = 0;
}
}
return (int)m_msg.wParam;
}
// Overrideables
// Override to change message filtering
virtual BOOL PreTranslateMessage(MSG* pMsg)
{
// loop backwards
for(int i = m_aMsgFilter.GetSize() - 1; i >= 0; i--)
{
CMessageFilter* pMessageFilter = m_aMsgFilter[i];
if((pMessageFilter != NULL) && pMessageFilter->PreTranslateMessage(pMsg))
return TRUE;
}
return FALSE; // not translated
}
// override to change idle processing
virtual BOOL OnIdle(int /*nIdleCount*/)
{
for(int i = 0; i < m_aIdleHandler.GetSize(); i++)
{
CIdleHandler* pIdleHandler = m_aIdleHandler[i];
if(pIdleHandler != NULL)
pIdleHandler->OnIdle();
}
return FALSE; // don't continue
}
// override to change non-idle messages
virtual BOOL IsIdleMessage(MSG* pMsg) const
{
// These messages should NOT cause idle processing
switch(pMsg->message)
{
case WM_MOUSEMOVE:
case WM_NCMOUSEMOVE:
case WM_PAINT:
case 0x0118: // WM_SYSTIMER (caret blink)
return FALSE;
}
return TRUE;
}
};
///////////////////////////////////////////////////////////////////////////////
// CStaticDataInitCriticalSectionLock and CWindowCreateCriticalSectionLock
// internal classes to manage critical sections for ATL (deprecated)
class CStaticDataInitCriticalSectionLock
{
public:
ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
CStaticDataInitCriticalSectionLock() : m_cslock(ATL::_pAtlModule->m_csStaticDataInitAndTypeInfo, false)
{ }
HRESULT Lock()
{
return m_cslock.Lock();
}
void Unlock()
{
m_cslock.Unlock();
}
};
class CWindowCreateCriticalSectionLock
{
public:
ATL::CComCritSecLock<ATL::CComCriticalSection> m_cslock;
CWindowCreateCriticalSectionLock() : m_cslock(ATL::_AtlWinModule.m_csWindowCreate, false)
{ }
HRESULT Lock()
{
return m_cslock.Lock();
}
void Unlock()
{
m_cslock.Unlock();
}
};
///////////////////////////////////////////////////////////////////////////////
// CAppModule - module class for an application
#if (_MSC_VER == 1400) // VS2005
#pragma warning(push)
#pragma warning(disable : 4244)
#pragma warning(disable : 4312)
#endif
class CAppModule : public ATL::CComModule
{
public:
DWORD m_dwMainThreadID;
ATL::CSimpleMap<DWORD, CMessageLoop*>* m_pMsgLoopMap;
ATL::CSimpleArray<HWND>* m_pSettingChangeNotify;
CAppModule() : m_dwMainThreadID(0), m_pMsgLoopMap(NULL), m_pSettingChangeNotify(NULL)
{ }
// Overrides of CComModule::Init and Term
HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL)
{
HRESULT hRet = CComModule::Init(pObjMap, hInstance, pLibID);
if(FAILED(hRet))
return hRet;
m_dwMainThreadID = ::GetCurrentThreadId();
typedef ATL::CSimpleMap<DWORD, CMessageLoop*> _mapClass;
m_pMsgLoopMap = NULL;
ATLTRY(m_pMsgLoopMap = new _mapClass);
if(m_pMsgLoopMap == NULL)
return E_OUTOFMEMORY;
m_pSettingChangeNotify = NULL;
return hRet;
}
void Term()
{
TermSettingChangeNotify();
delete m_pMsgLoopMap;
CComModule::Term();
}
// Message loop map methods
BOOL AddMessageLoop(CMessageLoop* pMsgLoop)
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::AddMessageLoop.\n"));
ATLASSERT(FALSE);
return FALSE;
}
ATLASSERT(pMsgLoop != NULL);
ATLASSERT(m_pMsgLoopMap->Lookup(::GetCurrentThreadId()) == NULL); // not in map yet
BOOL bRet = m_pMsgLoopMap->Add(::GetCurrentThreadId(), pMsgLoop);
lock.Unlock();
return bRet;
}
BOOL RemoveMessageLoop()
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::RemoveMessageLoop.\n"));
ATLASSERT(FALSE);
return FALSE;
}
BOOL bRet = m_pMsgLoopMap->Remove(::GetCurrentThreadId());
lock.Unlock();
return bRet;
}
CMessageLoop* GetMessageLoop(DWORD dwThreadID = ::GetCurrentThreadId()) const
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::GetMessageLoop.\n"));
ATLASSERT(FALSE);
return NULL;
}
CMessageLoop* pLoop = m_pMsgLoopMap->Lookup(dwThreadID);
lock.Unlock();
return pLoop;
}
// Setting change notify methods
// Note: Call this from the main thread for MSDI apps
BOOL InitSettingChangeNotify(DLGPROC pfnDlgProc = _SettingChangeDlgProc)
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::InitSettingChangeNotify.\n"));
ATLASSERT(FALSE);
return FALSE;
}
if(m_pSettingChangeNotify == NULL)
{
typedef ATL::CSimpleArray<HWND> _notifyClass;
ATLTRY(m_pSettingChangeNotify = new _notifyClass);
ATLASSERT(m_pSettingChangeNotify != NULL);
}
BOOL bRet = (m_pSettingChangeNotify != NULL);
if(bRet && (m_pSettingChangeNotify->GetSize() == 0))
{
// init everything
_ATL_EMPTY_DLGTEMPLATE templ;
HWND hNtfWnd = ::CreateDialogIndirect(GetModuleInstance(), &templ, NULL, pfnDlgProc);
ATLASSERT(::IsWindow(hNtfWnd));
if(::IsWindow(hNtfWnd))
{
::SetWindowLongPtr(hNtfWnd, GWLP_USERDATA, (LONG_PTR)this);
bRet = m_pSettingChangeNotify->Add(hNtfWnd);
}
else
{
bRet = FALSE;
}
}
lock.Unlock();
return bRet;
}
void TermSettingChangeNotify()
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::TermSettingChangeNotify.\n"));
ATLASSERT(FALSE);
return;
}
if((m_pSettingChangeNotify != NULL) && (m_pSettingChangeNotify->GetSize() > 0))
::DestroyWindow((*m_pSettingChangeNotify)[0]);
delete m_pSettingChangeNotify;
m_pSettingChangeNotify = NULL;
lock.Unlock();
}
BOOL AddSettingChangeNotify(HWND hWnd)
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::AddSettingChangeNotify.\n"));
ATLASSERT(FALSE);
return FALSE;
}
ATLASSERT(::IsWindow(hWnd));
BOOL bRet = FALSE;
if(InitSettingChangeNotify() != FALSE)
bRet = m_pSettingChangeNotify->Add(hWnd);
lock.Unlock();
return bRet;
}
BOOL RemoveSettingChangeNotify(HWND hWnd)
{
CStaticDataInitCriticalSectionLock lock;
if(FAILED(lock.Lock()))
{
ATLTRACE2(atlTraceUI, 0, _T("ERROR : Unable to lock critical section in CAppModule::RemoveSettingChangeNotify.\n"));
ATLASSERT(FALSE);
return FALSE;
}
BOOL bRet = FALSE;
if(m_pSettingChangeNotify != NULL)
bRet = m_pSettingChangeNotify->Remove(hWnd);
lock.Unlock();
return bRet;
}
// Implementation - setting change notify dialog template and dialog procedure
struct _ATL_EMPTY_DLGTEMPLATE : DLGTEMPLATE
{
_ATL_EMPTY_DLGTEMPLATE()
{
memset(this, 0, sizeof(_ATL_EMPTY_DLGTEMPLATE));
style = WS_POPUP;
}
WORD wMenu, wClass, wTitle;
};
static INT_PTR CALLBACK _SettingChangeDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if(uMsg == WM_SETTINGCHANGE)
{
CAppModule* pModule = (CAppModule*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
ATLASSERT(pModule != NULL);
ATLASSERT(pModule->m_pSettingChangeNotify != NULL);
const UINT uTimeout = 1500; // ms
for(int i = 1; i < pModule->m_pSettingChangeNotify->GetSize(); i++)
::SendMessageTimeout((*pModule->m_pSettingChangeNotify)[i], uMsg, wParam, lParam, SMTO_ABORTIFHUNG, uTimeout, NULL);
return TRUE;
}
return FALSE;
}
};
#if (_MSC_VER == 1400) // VS2005
#pragma warning(pop)
#endif
///////////////////////////////////////////////////////////////////////////////
// CServerAppModule - module class for a COM server application
class CServerAppModule : public CAppModule
{
public:
HANDLE m_hEventShutdown;
bool m_bActivity;
DWORD m_dwTimeOut;
DWORD m_dwPause;
CServerAppModule() : m_hEventShutdown(NULL), m_bActivity(false), m_dwTimeOut(5000), m_dwPause(1000)
{ }
// Override of CAppModule::Init
HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL)
{
m_dwTimeOut = 5000;
m_dwPause = 1000;
return CAppModule::Init(pObjMap, hInstance, pLibID);
}
void Term()
{
if((m_hEventShutdown != NULL) && ::CloseHandle(m_hEventShutdown))
m_hEventShutdown = NULL;
CAppModule::Term();
}
// COM Server methods
LONG Unlock() throw()
{
LONG lRet = CComModule::Unlock();
if(lRet == 0)
{
m_bActivity = true;
::SetEvent(m_hEventShutdown); // tell monitor that we transitioned to zero
}
return lRet;
}
void MonitorShutdown()
{
for(;;)
{
::WaitForSingleObject(m_hEventShutdown, INFINITE);
DWORD dwWait = 0;
do
{
m_bActivity = false;
dwWait = ::WaitForSingleObject(m_hEventShutdown, m_dwTimeOut);
}
while(dwWait == WAIT_OBJECT_0);
// timed out
if(!m_bActivity && (m_nLockCnt == 0)) // if no activity let's really bail
{
#if defined(_WIN32_DCOM) && defined(_ATL_FREE_THREADED)
::CoSuspendClassObjects();
if(!m_bActivity && (m_nLockCnt == 0))
#endif
break;
}
}
// This handle should be valid now. If it isn't,
// check if _Module.Term was called first (it shouldn't)
if(::CloseHandle(m_hEventShutdown))
m_hEventShutdown = NULL;
::PostThreadMessage(m_dwMainThreadID, WM_QUIT, 0, 0);
}
bool StartMonitor()
{
m_hEventShutdown = ::CreateEvent(NULL, false, false, NULL);
if(m_hEventShutdown == NULL)
return false;
DWORD dwThreadID = 0;
#ifdef _MT
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, (UINT (WINAPI*)(void*))MonitorProc, this, 0, (UINT*)&dwThreadID);
#else
HANDLE hThread = ::CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID);
#endif
bool bRet = (hThread != NULL);
if(bRet)
::CloseHandle(hThread);
return bRet;
}
static DWORD WINAPI MonitorProc(void* pv)
{
CServerAppModule* p = (CServerAppModule*)pv;
p->MonitorShutdown();
return 0;
}
};
///////////////////////////////////////////////////////////////////////////////
// CRegKeyEx - not used any more, here only for compatibility with old projects
typedef ATL::CRegKey CRegKeyEx;
} // namespace WTL
///////////////////////////////////////////////////////////////////////////////
// CString forward reference (enables CString use in atluser.h and atlgdi.h)
#if (defined(_WTL_USE_CSTRING) || defined(_WTL_FORWARD_DECLARE_CSTRING)) && !defined(__ATLSTR_H__)
#include <atlstr.h>
#endif
// CString namespace
#define _CSTRING_NS ATL
// Type classes namespace
#define _WTYPES_NS
///////////////////////////////////////////////////////////////////////////////
// General DLL version helpers (removed in ATL11)
#if (_ATL_VER >= 0x0B00)
namespace ATL
{
inline HRESULT AtlGetDllVersion(HINSTANCE hInstDLL, DLLVERSIONINFO* pDllVersionInfo)
{
ATLASSERT(pDllVersionInfo != NULL);
if(pDllVersionInfo == NULL)
return E_INVALIDARG;
// We must get this function explicitly because some DLLs don't implement it.
DLLGETVERSIONPROC pfnDllGetVersion = (DLLGETVERSIONPROC)::GetProcAddress(hInstDLL, "DllGetVersion");
if(pfnDllGetVersion == NULL)
return E_NOTIMPL;
return (*pfnDllGetVersion)(pDllVersionInfo);
}
inline HRESULT AtlGetDllVersion(LPCTSTR lpstrDllName, DLLVERSIONINFO* pDllVersionInfo)
{
HINSTANCE hInstDLL = ::LoadLibrary(lpstrDllName);
if(hInstDLL == NULL)
return E_FAIL;
HRESULT hRet = AtlGetDllVersion(hInstDLL, pDllVersionInfo);
::FreeLibrary(hInstDLL);
return hRet;
}
// Common Control Versions:
// Win95/WinNT 4.0 maj=4 min=00
// IE 3.x maj=4 min=70
// IE 4.0 maj=4 min=71
inline HRESULT AtlGetCommCtrlVersion(LPDWORD pdwMajor, LPDWORD pdwMinor)
{
ATLASSERT((pdwMajor != NULL) && (pdwMinor != NULL));
if((pdwMajor == NULL) || (pdwMinor == NULL))
return E_INVALIDARG;
DLLVERSIONINFO dvi;
::ZeroMemory(&dvi, sizeof(dvi));
dvi.cbSize = sizeof(dvi);
HRESULT hRet = AtlGetDllVersion(_T("comctl32.dll"), &dvi);
if(SUCCEEDED(hRet))
{
*pdwMajor = dvi.dwMajorVersion;
*pdwMinor = dvi.dwMinorVersion;
}
else if(hRet == E_NOTIMPL)
{
// If DllGetVersion is not there, then the DLL is a version
// previous to the one shipped with IE 3.x
*pdwMajor = 4;
*pdwMinor = 0;
hRet = S_OK;
}
return hRet;
}
// Shell Versions:
// Win95/WinNT 4.0 maj=4 min=00
// IE 3.x, IE 4.0 without Web Integrated Desktop maj=4 min=00
// IE 4.0 with Web Integrated Desktop maj=4 min=71
// IE 4.01 with Web Integrated Desktop maj=4 min=72
inline HRESULT AtlGetShellVersion(LPDWORD pdwMajor, LPDWORD pdwMinor)
{
ATLASSERT((pdwMajor != NULL) && (pdwMinor != NULL));
if((pdwMajor == NULL) || (pdwMinor == NULL))
return E_INVALIDARG;
DLLVERSIONINFO dvi;
::ZeroMemory(&dvi, sizeof(dvi));
dvi.cbSize = sizeof(dvi);
HRESULT hRet = AtlGetDllVersion(_T("shell32.dll"), &dvi);
if(SUCCEEDED(hRet))
{
*pdwMajor = dvi.dwMajorVersion;
*pdwMinor = dvi.dwMinorVersion;
}
else if(hRet == E_NOTIMPL)
{
// If DllGetVersion is not there, then the DLL is a version
// previous to the one shipped with IE 4.x
*pdwMajor = 4;
*pdwMinor = 0;
hRet = S_OK;
}
return hRet;
}
} // namespace ATL
#endif // (_ATL_VER >= 0x0B00)
// These are always included
#include "atlwinx.h"
#include "atluser.h"
#include "atlgdi.h"
#ifndef _WTL_NO_AUTOMATIC_NAMESPACE
using namespace WTL;
#endif // !_WTL_NO_AUTOMATIC_NAMESPACE
#endif // __ATLAPP_H__
|