This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
statreg.h
1671 lines (1454 loc) · 37.8 KB
/
statreg.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
// This is a part of the Active Template Library.
// Copyright (C) Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Active Template Library Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Active Template Library product.
#ifndef __STATREG_H__
#define __STATREG_H__
#pragma once
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLBASE_H__
#error statreg.h requires atlbase.h to be included first
#endif
#include <atldef.h>
#if !defined(_ATL_USE_WINAPI_FAMILY_DESKTOP_APP)
#error This file is not compatible with the current WINAPI_FAMILY
#endif
#pragma warning(push)
#pragma warning(disable:4571) //catch(...) blocks compiled with /EHs do NOT catch or re-throw Structured Exceptions
#define E_ATL_REGISTRAR_DESC 0x0201
#define E_ATL_NOT_IN_MAP 0x0202
#define E_ATL_UNEXPECTED_EOS 0x0203
#define E_ATL_VALUE_SET_FAILED 0x0204
#define E_ATL_RECURSE_DELETE_FAILED 0x0205
#define E_ATL_EXPECTING_EQUAL 0x0206
#define E_ATL_CREATE_KEY_FAILED 0x0207
#define E_ATL_DELETE_KEY_FAILED 0x0208
#define E_ATL_OPEN_KEY_FAILED 0x0209
#define E_ATL_CLOSE_KEY_FAILED 0x020A
#define E_ATL_UNABLE_TO_COERCE 0x020B
#define E_ATL_BAD_HKEY 0x020C
#define E_ATL_MISSING_OPENKEY_TOKEN 0x020D
#define E_ATL_CONVERT_FAILED 0x020E
#define E_ATL_TYPE_NOT_SUPPORTED 0x020F
#define E_ATL_COULD_NOT_CONCAT 0x0210
#define E_ATL_COMPOUND_KEY 0x0211
#define E_ATL_INVALID_MAPKEY 0x0212
#define E_ATL_UNSUPPORTED_VT 0x0213
#define E_ATL_VALUE_GET_FAILED 0x0214
#define E_ATL_VALUE_TOO_LARGE 0x0215
#define E_ATL_MISSING_VALUE_DELIMETER 0x0216
#define E_ATL_DATA_NOT_BYTE_ALIGNED 0x0217
#pragma pack(push,_ATL_PACKING)
namespace ATL
{
extern __declspec(selectany) const TCHAR chDirSep = _T('\\');
extern __declspec(selectany) const TCHAR chRightBracket = _T('}');
extern __declspec(selectany) const TCHAR chLeftBracket = _T('{');
extern __declspec(selectany) const TCHAR chQuote = _T('\'');
extern __declspec(selectany) const TCHAR chEquals = _T('=');
extern __declspec(selectany) const LPCTSTR szStringVal = _T("S");
extern __declspec(selectany) const LPCTSTR multiszStringVal = _T("M");
extern __declspec(selectany) const LPCTSTR szDwordVal = _T("D");
extern __declspec(selectany) const LPCTSTR szBinaryVal = _T("B");
extern __declspec(selectany) const LPCTSTR szValToken = _T("Val");
extern __declspec(selectany) const LPCTSTR szForceRemove = _T("ForceRemove");
extern __declspec(selectany) const LPCTSTR szNoRemove = _T("NoRemove");
extern __declspec(selectany) const LPCTSTR szDelete = _T("Delete");
// Implementation helper
class CExpansionVectorEqualHelper
{
public:
static bool IsEqualKey(
_In_z_ const LPTSTR k1,
_In_z_ const LPTSTR k2)
{
if (lstrcmpi(k1, k2) == 0)
return true;
return false;
}
// Not used
static bool IsEqualValue(
_In_opt_z_ const LPCOLESTR /*v1*/,
_In_opt_z_ const LPCOLESTR /*v2*/)
{
return false;
}
};
// Implementation helper
class CExpansionVector :
public CSimpleMap<LPTSTR, LPOLESTR, CExpansionVectorEqualHelper >
{
public:
~CExpansionVector()
{
ClearReplacements();
}
ATLPREFAST_SUPPRESS(6001 6014 6211)
BOOL Add(
_In_z_ LPCTSTR lpszKey,
_In_z_ LPCOLESTR lpszValue)
{
ATLASSERT(lpszKey != NULL && lpszValue != NULL);
if (lpszKey == NULL || lpszValue == NULL)
return FALSE;
HRESULT hRes = S_OK;
size_t cbKey = (_tcslen(lpszKey)+1)*sizeof(TCHAR);
TCHAR* szKey = NULL;
szKey = _ATL_NEW TCHAR[cbKey];
CAutoVectorPtr<TCHAR> spKey;
ATLASSUME(szKey != NULL);
spKey.Attach(szKey);
size_t cbValue = (ocslen(lpszValue)+1)*sizeof(OLECHAR);
LPOLESTR szValue = NULL;
szValue = _ATL_NEW OLECHAR[cbValue];
CAutoVectorPtr<OLECHAR> spValue;
ATLASSUME(szValue != NULL);
spValue.Attach(szValue);
if (szKey == NULL || szValue == NULL)
hRes = E_OUTOFMEMORY;
else
{
Checked::memcpy_s(szKey, cbKey, lpszKey, cbKey);
Checked::memcpy_s(szValue, cbValue, lpszValue, cbValue);
if (!CSimpleMap<LPTSTR, LPOLESTR, CExpansionVectorEqualHelper>::Add(szKey, szValue))
hRes = E_OUTOFMEMORY;
}
if (SUCCEEDED(hRes))
{
spKey.Detach();
spValue.Detach();
}
return SUCCEEDED(hRes);
}
ATLPREFAST_UNSUPPRESS()
HRESULT ClearReplacements()
{
for (int i = 0; i < GetSize(); i++)
{
delete []GetKeyAt(i);
delete []GetValueAt(i);
}
RemoveAll();
return S_OK;
}
};
class CRegObject;
class CRegParser
{
public:
CRegParser(_In_ CRegObject* pRegObj);
HRESULT PreProcessBuffer(
_In_z_ LPTSTR lpszReg,
_Outptr_result_z_ LPTSTR* ppszReg);
HRESULT RegisterBuffer(
_In_z_ LPTSTR szReg,
_In_ BOOL bRegister);
protected:
static const int MAX_VALUE = 4096;
void SkipWhiteSpace();
HRESULT NextToken(_Out_writes_z_(MAX_VALUE) LPTSTR szToken);
HRESULT AddValue(
_Inout_ CRegKey& rkParent,
_In_opt_z_ LPCTSTR szValueName,
_Out_writes_z_(MAX_VALUE) LPTSTR szToken);
BOOL CanForceRemoveKey(_In_z_ LPCTSTR szKey);
BOOL HasSubKeys(_In_ HKEY hkey);
BOOL HasValues(_In_ HKEY hkey);
HRESULT RegisterSubkeys(
_Out_writes_z_(MAX_VALUE) LPTSTR szToken,
_In_ HKEY hkParent,
_In_ BOOL bRegister,
_In_ BOOL bInRecovery = FALSE);
BOOL IsSpace(_In_ TCHAR ch);
LPTSTR m_pchCur;
CRegObject* m_pRegObj;
_Ret_range_(<, 0)
HRESULT GenerateError(_In_ UINT)
{
return DISP_E_EXCEPTION;
}
//HRESULT HandleReplacements(LPTSTR& szToken);
HRESULT SkipAssignment(_Inout_updates_z_(MAX_VALUE) LPTSTR szToken);
BOOL EndOfVar()
{
return chQuote == *m_pchCur && chQuote != *CharNext(m_pchCur);
}
static LPTSTR StrChr(_In_z_ LPTSTR lpsz, _In_ TCHAR ch);
static HKEY HKeyFromString(_In_z_ LPTSTR szToken);
static BYTE ChToByte(_In_ const TCHAR ch);
static BOOL VTFromRegType(_In_z_ LPCTSTR szValueType, _Out_ VARTYPE& vt);
static const TCHAR* const rgszNeverDelete[];
static const int cbNeverDelete;
static const int MAX_TYPE = 4096;
// Implementation Helper
class CParseBuffer
{
public:
int nPos;
int nSize;
LPTSTR p;
CParseBuffer(_In_ int nInitial)
{
if (nInitial < 100)
nInitial = 1000;
nPos = 0;
nSize = nInitial;
p = (LPTSTR) ::ATL::AtlCoTaskMemCAlloc(nSize,static_cast<ULONG>(sizeof(TCHAR)));
if (p != NULL)
*p = _T('\0');
}
~CParseBuffer()
{
CoTaskMemFree(p);
}
BOOL Append(
_In_reads_(nChars) const TCHAR* pch,
_In_ int nChars)
{
ATLASSERT(p != NULL);
ATLASSUME(p != NULL);
int newSize = nPos + nChars + 1;
if ((newSize <= nPos) || (newSize <= nChars))
return FALSE;
if (newSize >= nSize)
{
while (newSize >= nSize) {
if (nSize > INT_MAX / 2)
return FALSE;
nSize *= 2;
}
LPTSTR pTemp = (LPTSTR)::ATL::AtlCoTaskMemRecalloc(p, nSize, sizeof(TCHAR));
if (pTemp == NULL)
return FALSE;
p = pTemp;
}
if ((nPos < 0) || (nPos >= nSize) || nSize - nPos > nSize)
return FALSE;
#pragma warning(push)
#pragma warning(disable: 22008)
/* Prefast false warning is fired here despite the all above checks */
Checked::memcpy_s(p + nPos, (nSize-nPos) * sizeof(TCHAR), pch, nChars * sizeof(TCHAR));
nPos += nChars;
*(p + nPos) = _T('\0');
#pragma warning(pop)
return TRUE;
}
BOOL AddChar(_In_z_ const TCHAR* pch)
{
#ifndef _UNICODE
int nChars = int(CharNext(pch) - pch);
#else
int nChars = 1;
#endif
return Append(pch, nChars);
}
BOOL AddString(_In_z_ LPCOLESTR lpsz)
{
if (lpsz == NULL)
{
return FALSE;
}
USES_CONVERSION_EX;
LPCTSTR lpszT = OLE2CT_EX(lpsz, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
if (lpszT == NULL)
{
return FALSE;
}
return Append(lpszT, (int)_tcslen(lpszT));
}
LPTSTR Detach()
{
LPTSTR lp = p;
p = NULL;
nSize = nPos = 0;
return lp;
}
};
};
class CRegObject :
public IRegistrarBase
{
public:
STDMETHOD(QueryInterface)(
const IID &,
__RPC__deref_out void ** )
{
ATLASSERT(_T("statically linked in CRegObject is not a com object. Do not callthis function"));
return E_NOTIMPL;
}
STDMETHOD_(ULONG, AddRef)(void)
{
ATLASSERT(_T("statically linked in CRegObject is not a com object. Do not callthis function"));
return 1;
}
STDMETHOD_(ULONG, Release)(void)
{
ATLASSERT(_T("statically linked in CRegObject is not a com object. Do not callthis function"));
return 0;
}
virtual ~CRegObject()
{
ClearReplacements();
}
HRESULT FinalConstruct()
{
return m_csMap.Init();
}
void FinalRelease() {}
// Map based methods
HRESULT STDMETHODCALLTYPE AddReplacement(
_In_z_ LPCOLESTR lpszKey,
_In_z_ LPCOLESTR lpszItem);
HRESULT STDMETHODCALLTYPE ClearReplacements();
LPCOLESTR StrFromMap(_In_z_ LPTSTR lpszKey);
// Register via a given mechanism
HRESULT STDMETHODCALLTYPE ResourceRegister(
_In_z_ LPCOLESTR pszFileName,
_In_ UINT nID,
_In_z_ LPCOLESTR pszType);
HRESULT STDMETHODCALLTYPE ResourceRegisterSz(
_In_z_ LPCOLESTR pszFileName,
_In_z_ LPCOLESTR pszID,
_In_z_ LPCOLESTR pszType);
HRESULT STDMETHODCALLTYPE ResourceUnregister(
_In_z_ LPCOLESTR pszFileName,
_In_ UINT nID,
_In_z_ LPCOLESTR pszType);
HRESULT STDMETHODCALLTYPE ResourceUnregisterSz(
_In_z_ LPCOLESTR pszFileName,
_In_z_ LPCOLESTR pszID,
_In_z_ LPCOLESTR pszType);
HRESULT STDMETHODCALLTYPE FileRegister(_In_z_ LPCOLESTR bstrFileName)
{
return CommonFileRegister(bstrFileName, TRUE);
}
HRESULT STDMETHODCALLTYPE FileUnregister(_In_z_ LPCOLESTR bstrFileName)
{
return CommonFileRegister(bstrFileName, FALSE);
}
HRESULT STDMETHODCALLTYPE StringRegister(_In_z_ LPCOLESTR bstrData)
{
return RegisterWithString(bstrData, TRUE);
}
HRESULT STDMETHODCALLTYPE StringUnregister(_In_z_ LPCOLESTR bstrData)
{
return RegisterWithString(bstrData, FALSE);
}
protected:
HRESULT CommonFileRegister(
_In_z_ LPCOLESTR pszFileName,
_In_ BOOL bRegister);
HRESULT RegisterFromResource(
_In_z_ LPCOLESTR pszFileName,
_In_z_ LPCTSTR pszID,
_In_z_ LPCTSTR pszType,
_In_ BOOL bRegister);
HRESULT RegisterWithString(
_In_z_ LPCOLESTR pszData,
_In_ BOOL bRegister);
_Ret_range_(<, 0)
static HRESULT GenerateError(_In_ UINT)
{
return DISP_E_EXCEPTION;
}
CExpansionVector m_RepMap;
CComObjectThreadModel::AutoDeleteCriticalSection m_csMap;
};
#pragma warning(suppress: 26165) // Macro instantiated lock object '(this->m_csMap).m_sec'
inline HRESULT STDMETHODCALLTYPE CRegObject::AddReplacement(
_In_z_ LPCOLESTR lpszKey,
_In_z_ LPCOLESTR lpszItem)
{
if (lpszKey == NULL || lpszItem == NULL)
return E_INVALIDARG;
m_csMap.Lock();
USES_CONVERSION_EX;
LPCTSTR lpszT = OLE2CT_EX(lpszKey, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if(lpszT == NULL)
return E_OUTOFMEMORY;
#endif
BOOL bRet = m_RepMap.Add(lpszT, lpszItem);
m_csMap.Unlock();
return bRet ? S_OK : E_OUTOFMEMORY;
}
#pragma warning(suppress: 6262) // Stack size of '1104' bytes is OK
inline HRESULT CRegObject::RegisterFromResource(
_In_z_ LPCOLESTR bstrFileName,
_In_z_ LPCTSTR szID,
_In_z_ LPCTSTR szType,
_In_ BOOL bRegister)
{
USES_CONVERSION_EX;
HRESULT hr;
CRegParser parser(this);
HINSTANCE hInstResDll;
HRSRC hrscReg;
HGLOBAL hReg;
DWORD dwSize;
LPSTR szRegA;
CTempBuffer<TCHAR, 1024> szReg;
LPCTSTR lpszBSTRFileName = OLE2CT_EX(bstrFileName, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if (lpszBSTRFileName == NULL)
{
return E_OUTOFMEMORY;
}
#endif // _UNICODE
hInstResDll = LoadLibraryEx(lpszBSTRFileName, NULL, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
if (NULL == hInstResDll)
{
// if library load failed using flags only valid on Vista+, fall back to using flags valid on XP
hInstResDll = LoadLibraryEx(lpszBSTRFileName, NULL, LOAD_LIBRARY_AS_DATAFILE);
}
if (NULL == hInstResDll)
{
ATLTRACE(atlTraceRegistrar, 0, _T("Failed to LoadLibrary on %Ts\n"), bstrFileName);
hr = AtlHresultFromLastError();
goto ReturnHR;
}
hrscReg =FindResource((HMODULE)hInstResDll, szID, szType);
if (NULL == hrscReg)
{
ATLTRACE(atlTraceRegistrar, 0, (HIWORD(szID) == 0) ?
_T("Failed to FindResource on ID:%d TYPE:%Ts\n") :
_T("Failed to FindResource on ID:%Ts TYPE:%Ts\n"),
szID, szType);
hr = AtlHresultFromLastError();
goto ReturnHR;
}
hReg = LoadResource((HMODULE)hInstResDll, hrscReg);
if (NULL == hReg)
{
ATLTRACE(atlTraceRegistrar, 0, _T("Failed to LoadResource\n"));
hr = AtlHresultFromLastError();
goto ReturnHR;
}
dwSize = SizeofResource((HMODULE)hInstResDll, hrscReg);
szRegA = (LPSTR)hReg;
// Allocate extra space for NULL.
if (dwSize + 1 < dwSize)
{
hr = E_OUTOFMEMORY;
goto ReturnHR;
}
ATLTRY(szReg.Allocate(dwSize + 1));
if (szReg == NULL)
{
hr = E_OUTOFMEMORY;
goto ReturnHR;
}
#ifdef _UNICODE
{
DWORD uniSize = ::MultiByteToWideChar(_AtlGetConversionACP(), 0, szRegA, dwSize, szReg, dwSize);
if (uniSize == 0)
{
hr = AtlHresultFromLastError();
goto ReturnHR;
}
// Append a NULL at the end.
szReg[uniSize] = _T('\0');
}
#else
Checked::memcpy_s(szReg, dwSize, szRegA, dwSize);
// Append a NULL at the end.
szReg[dwSize] = _T('\0');
#endif
hr = parser.RegisterBuffer(szReg, bRegister);
ReturnHR:
if (NULL != hInstResDll)
FreeLibrary((HMODULE)hInstResDll);
return hr;
}
inline HRESULT STDMETHODCALLTYPE CRegObject::ResourceRegister(
_In_z_ LPCOLESTR szFileName,
_In_ UINT nID,
_In_z_ LPCOLESTR szType)
{
USES_CONVERSION_EX;
LPCTSTR lpszT = OLE2CT_EX(szType, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if (lpszT == NULL)
{
return E_OUTOFMEMORY;
}
#endif // _UNICODE
return RegisterFromResource(szFileName, MAKEINTRESOURCE(nID), lpszT, TRUE);
}
inline HRESULT STDMETHODCALLTYPE CRegObject::ResourceRegisterSz(
_In_z_ LPCOLESTR szFileName,
_In_z_ LPCOLESTR szID,
_In_z_ LPCOLESTR szType)
{
USES_CONVERSION_EX;
if (szID == NULL || szType == NULL)
return E_INVALIDARG;
LPCTSTR lpszID = OLE2CT_EX(szID, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
LPCTSTR lpszType = OLE2CT_EX(szType, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if (lpszID == NULL || lpszType==NULL)
{
return E_OUTOFMEMORY;
}
#endif // _UNICODE
return RegisterFromResource(szFileName, lpszID, lpszType, TRUE);
}
inline HRESULT STDMETHODCALLTYPE CRegObject::ResourceUnregister(
_In_z_ LPCOLESTR szFileName,
_In_ UINT nID,
_In_z_ LPCOLESTR szType)
{
USES_CONVERSION_EX;
LPCTSTR lpszT = OLE2CT_EX(szType, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if (lpszT == NULL)
{
return E_OUTOFMEMORY;
}
#endif // _UNICODE
return RegisterFromResource(szFileName, MAKEINTRESOURCE(nID), lpszT, FALSE);
}
inline HRESULT STDMETHODCALLTYPE CRegObject::ResourceUnregisterSz(
_In_z_ LPCOLESTR szFileName,
_In_z_ LPCOLESTR szID,
_In_z_ LPCOLESTR szType)
{
USES_CONVERSION_EX;
if (szID == NULL || szType == NULL)
return E_INVALIDARG;
LPCTSTR lpszID = OLE2CT_EX(szID, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
LPCTSTR lpszType = OLE2CT_EX(szType, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if (lpszID == NULL || lpszType == NULL)
{
return E_OUTOFMEMORY;
}
#endif // _UNICODE
return RegisterFromResource(szFileName, lpszID, lpszType, FALSE);
}
inline HRESULT CRegObject::RegisterWithString(
_In_z_ LPCOLESTR bstrData,
_In_ BOOL bRegister)
{
USES_CONVERSION_EX;
CRegParser parser(this);
LPCTSTR szReg = OLE2CT_EX(bstrData, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if (szReg == NULL)
{
return E_OUTOFMEMORY;
}
#endif // _UNICODE
HRESULT hr = parser.RegisterBuffer((LPTSTR)szReg, bRegister);
return hr;
}
inline HRESULT CRegObject::ClearReplacements()
{
m_csMap.Lock();
HRESULT hr = m_RepMap.ClearReplacements();
m_csMap.Unlock();
return hr;
}
inline LPCOLESTR CRegObject::StrFromMap(_In_z_ LPTSTR lpszKey)
{
m_csMap.Lock();
LPCOLESTR lpsz = m_RepMap.Lookup(lpszKey);
if (lpsz == NULL) // not found!!
ATLTRACE(atlTraceRegistrar, 0, _T("Map Entry not found\n"));
m_csMap.Unlock();
return lpsz;
}
#pragma warning(suppress: 6262) // Stack size of '1092' bytes is OK
inline HRESULT CRegObject::CommonFileRegister(
_In_z_ LPCOLESTR bstrFileName,
_In_ BOOL bRegister)
{
USES_CONVERSION_EX;
CRegParser parser(this);
LPCTSTR lpszBSTRFileName = OLE2CT_EX(bstrFileName, _ATL_SAFE_ALLOCA_DEF_THRESHOLD);
#ifndef _UNICODE
if (lpszBSTRFileName == NULL)
{
return E_OUTOFMEMORY;
}
#endif // _UNICODE
HANDLE hFile = CreateFile(lpszBSTRFileName, GENERIC_READ, 0, NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
ATLTRACE2(atlTraceRegistrar, 0, _T("Failed to CreateFile on %Ts\n"), lpszBSTRFileName);
return AtlHresultFromLastError();
}
HRESULT hRes = S_OK;
DWORD cbRead;
CTempBuffer<char, 1024> szReg;
DWORD cbFile = GetFileSize(hFile, NULL); // No HiOrder DWORD required
if (INVALID_FILE_SIZE == cbFile)
{
hRes = AtlHresultFromLastError();
goto ReturnHR;
}
// Extra space for NULL.
ATLTRY(szReg.Allocate(cbFile + 1));
if (szReg == NULL)
{
hRes = E_OUTOFMEMORY;
goto ReturnHR;
}
if (ReadFile(hFile, szReg, cbFile, &cbRead, NULL) == 0)
{
ATLTRACE2(atlTraceRegistrar, 0, "Read Failed on file %s\n", lpszBSTRFileName);
hRes = AtlHresultFromLastError();
}
if (SUCCEEDED(hRes))
{
szReg[cbRead] = '\0';
#ifdef _UNICODE
CTempBuffer<WCHAR, 1024> szConverted;
ATLTRY(szConverted.Allocate(cbFile + 1));
if (szConverted == NULL)
{
hRes = E_OUTOFMEMORY;
goto ReturnHR;
}
if (::MultiByteToWideChar(_AtlGetConversionACP(), 0, szReg, cbFile + 1, szConverted, cbFile + 1) == 0)
{
hRes = AtlHresultFromLastError();
goto ReturnHR;
}
#else
LPTSTR szConverted = szReg;
#endif
hRes = parser.RegisterBuffer(szConverted, bRegister);
}
ReturnHR:
CloseHandle(hFile);
return hRes;
}
__declspec(selectany) const TCHAR* const CRegParser::rgszNeverDelete[] =
{
_T("AppID"),
_T("CLSID"),
_T("Component Categories"),
_T("FileType"),
_T("Interface"),
_T("Hardware"),
_T("Mime"),
_T("SAM"),
_T("SECURITY"),
_T("SYSTEM"),
_T("Software"),
_T("TypeLib")
};
__declspec(selectany) const int CRegParser::cbNeverDelete = sizeof(rgszNeverDelete) / sizeof(LPCTSTR*);
inline BOOL CRegParser::VTFromRegType(
_In_z_ LPCTSTR szValueType,
_Out_ VARTYPE& vt)
{
if (!lstrcmpi(szValueType, szStringVal))
{
vt = VT_BSTR;
return TRUE;
}
if (!lstrcmpi(szValueType, multiszStringVal))
{
vt = VT_BSTR | VT_BYREF;
return TRUE;
}
if (!lstrcmpi(szValueType, szDwordVal))
{
vt = VT_UI4;
return TRUE;
}
if (!lstrcmpi(szValueType, szBinaryVal))
{
vt = VT_UI1;
return TRUE;
}
vt = VT_EMPTY;
return FALSE;
}
inline BYTE CRegParser::ChToByte(_In_ const TCHAR ch)
{
switch (ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return (BYTE) (ch - '0');
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
return (BYTE) (10 + (ch - 'A'));
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return (BYTE) (10 + (ch - 'a'));
default:
ATLASSERT(FALSE);
ATLTRACE(atlTraceRegistrar, 0, _T("Bogus value %Tc passed as binary Hex value\n"), ch);
return 0;
}
}
inline HKEY CRegParser::HKeyFromString(_In_z_ LPTSTR szToken)
{
struct keymap
{
LPCTSTR lpsz;
HKEY hkey;
};
static const keymap map[] = {
{_T("HKCR"), HKEY_CLASSES_ROOT},
{_T("HKCU"), HKEY_CURRENT_USER},
{_T("HKLM"), HKEY_LOCAL_MACHINE},
{_T("HKU"), HKEY_USERS},
{_T("HKPD"), HKEY_PERFORMANCE_DATA},
{_T("HKDD"), HKEY_DYN_DATA},
{_T("HKCC"), HKEY_CURRENT_CONFIG},
{_T("HKEY_CLASSES_ROOT"), HKEY_CLASSES_ROOT},
{_T("HKEY_CURRENT_USER"), HKEY_CURRENT_USER},
{_T("HKEY_LOCAL_MACHINE"), HKEY_LOCAL_MACHINE},
{_T("HKEY_USERS"), HKEY_USERS},
{_T("HKEY_PERFORMANCE_DATA"), HKEY_PERFORMANCE_DATA},
{_T("HKEY_DYN_DATA"), HKEY_DYN_DATA},
{_T("HKEY_CURRENT_CONFIG"), HKEY_CURRENT_CONFIG}
};
for (int i=0;i<sizeof(map)/sizeof(keymap);i++)
{
if (!lstrcmpi(szToken, map[i].lpsz))
return map[i].hkey;
}
return NULL;
}
inline LPTSTR CRegParser::StrChr(
_In_z_ LPTSTR lpsz,
_In_ TCHAR ch)
{
LPTSTR p = NULL;
if (lpsz == NULL)
return NULL;
while (*lpsz)
{
if (*lpsz == ch)
{
p = lpsz;
break;
}
lpsz = CharNext(lpsz);
}
return p;
}
inline CRegParser::CRegParser(_In_ CRegObject* pRegObj)
{
m_pRegObj = pRegObj;
m_pchCur = NULL;
}
inline BOOL CRegParser::IsSpace(_In_ TCHAR ch)
{
switch (ch)
{
case _T(' '):
case _T('\t'):
case _T('\r'):
case _T('\n'):
return TRUE;
}
return FALSE;
}
inline void CRegParser::SkipWhiteSpace()
{
while(IsSpace(*m_pchCur))
m_pchCur = CharNext(m_pchCur);
}
ATLPREFAST_SUPPRESS(6001 6054 6385)
inline HRESULT CRegParser::NextToken(_Out_writes_z_(MAX_VALUE) LPTSTR szToken)
{
SkipWhiteSpace();
// NextToken cannot be called at EOS
if (_T('\0') == *m_pchCur)
return GenerateError(E_ATL_UNEXPECTED_EOS);
LPCTSTR szOrig = szToken;
// handle quoted value / key
if (chQuote == *m_pchCur)
{
m_pchCur = CharNext(m_pchCur);
while (_T('\0') != *m_pchCur && !EndOfVar())
{
if (chQuote == *m_pchCur) // If it is a quote that means we must skip it
m_pchCur = CharNext(m_pchCur);
LPTSTR pchPrev = m_pchCur;
m_pchCur = CharNext(m_pchCur);
INT_PTR nChars = m_pchCur - pchPrev;
// Make sure we have room for nChars plus terminating NULL
if ((szToken + nChars + 1) >= szOrig + MAX_VALUE)
return GenerateError(E_ATL_VALUE_TOO_LARGE);
for (int i = 0; i < (int)nChars; i++, szToken++, pchPrev++)
*szToken = *pchPrev;
}
if (_T('\0') == *m_pchCur)
{
ATLTRACE(atlTraceRegistrar, 0, _T("NextToken : Unexpected End of File\n"));
return GenerateError(E_ATL_UNEXPECTED_EOS);
}
*szToken = _T('\0');
m_pchCur = CharNext(m_pchCur);
}
else
{
// Handle non-quoted ie parse up till first "White Space"
while (_T('\0') != *m_pchCur && !IsSpace(*m_pchCur))
{
LPTSTR pchPrev = m_pchCur;
m_pchCur = CharNext(m_pchCur);
INT_PTR nChars = m_pchCur - pchPrev;
// Make sure we have room for nChars plus terminating NULL
if ((szToken + nChars + 1) >= szOrig + MAX_VALUE)
return GenerateError(E_ATL_VALUE_TOO_LARGE);
for (int i = 0; i < (int)nChars; i++, szToken++, pchPrev++)
*szToken = *pchPrev;
}
*szToken = _T('\0');
}
return S_OK;
}
#pragma warning(suppress: 6262) // Stack size of '4704' bytes is OK
inline HRESULT CRegParser::AddValue(
_Inout_ CRegKey& rkParent,
_In_opt_z_ LPCTSTR szValueName,
_Out_writes_z_(MAX_VALUE) LPTSTR szToken)
{
HRESULT hr;
TCHAR szValue[MAX_VALUE];
VARTYPE vt = VT_EMPTY;
LONG lRes = ERROR_SUCCESS;
UINT nIDRes = 0;
if (FAILED(hr = NextToken(szValue)))
return hr;
if (!VTFromRegType(szValue, vt))
{
ATLTRACE(atlTraceRegistrar, 0, _T("%Ts Type not supported\n"), szValue);
return GenerateError(E_ATL_TYPE_NOT_SUPPORTED);
}
SkipWhiteSpace();
if (FAILED(hr = NextToken(szValue)))
return hr;
switch (vt)
{
case VT_BSTR:
{
lRes = rkParent.SetStringValue(szValueName, szValue);
ATLTRACE(atlTraceRegistrar, 2, _T("Setting Value %Ts at %Ts\n"), szValue, !szValueName ? _T("default") : szValueName);
break;
}
case VT_BSTR | VT_BYREF:
{