-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.h
More file actions
913 lines (792 loc) · 23.1 KB
/
registry.h
File metadata and controls
913 lines (792 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
/*
Project: WINLIB
Module: registry.h
Description: Registry keys
Author: Martin Gäckler
Address: Hofmannsthalweg 14, A-4030 Linz
Web: https://www.gaeckler.at/
Copyright: (c) 1988-2026 Martin Gäckler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
THIS SOFTWARE IS PROVIDED BY Martin Gäckler, Linz, Austria ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef GAK_WINLIB_REGISTRY_H
#define GAK_WINLIB_REGISTRY_H
// --------------------------------------------------------------------- //
// ----- switches ------------------------------------------------------ //
// --------------------------------------------------------------------- //
#ifndef STRICT
#define STRICT 1
#endif
// --------------------------------------------------------------------- //
// ----- includes ------------------------------------------------------ //
// --------------------------------------------------------------------- //
#include <windows.h>
#include <gak/CopyProtection.h>
#include <gak/string.h>
#include <gak/array.h>
#include <gak/types.h>
#include <WINLIB/WINLIB.H>
// --------------------------------------------------------------------- //
// ----- imported datas ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module switches ----------------------------------------------- //
// --------------------------------------------------------------------- //
#ifdef __BORLANDC__
# pragma option -RT-
# pragma option -b
# pragma option -a4
# pragma option -pc
#endif
namespace winlib
{
// --------------------------------------------------------------------- //
// ----- constants ----------------------------------------------------- //
// --------------------------------------------------------------------- //
#ifndef KEY_WOW64_32KEY // for Borland C++ Builder
# define KEY_WOW64_32KEY (0x0200)
# define KEY_WOW64_64KEY (0x0100)
# define KEY_WOW64_RES (0x0300)
#endif
// --------------------------------------------------------------------- //
// ----- macros -------------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- type definitions ---------------------------------------------- //
// --------------------------------------------------------------------- //
enum RegistryType
{
rtMISSING = REG_NONE-2,
rtERROR = REG_NONE-1,
rtNONE = REG_NONE,
rtSTRING = REG_SZ,
rtENV = REG_EXPAND_SZ,
rtBINRAY = REG_BINARY,
rtINTEGER = REG_DWORD,
rtINT64 = REG_QWORD
};
enum ReadSuccess
{
rsOK,
rsBadType,
rsBadSize,
rsNotFound,
rsError,
};
struct RegValuePair
{
RegistryType type;
gak::STRING valueName, valueBuffer;
};
typedef gak::Array<RegValuePair> RegValuePairs;
// --------------------------------------------------------------------- //
// ----- class definitions --------------------------------------------- //
// --------------------------------------------------------------------- //
class EnvSTRING : public gak::STRING
{
public:
EnvSTRING( const gak::STRING &src ) : gak::STRING( src ) {}
EnvSTRING() : gak::STRING() {}
gak::STRING expand() const
{
DWORD newSize = ExpandEnvironmentStrings( c_str(), nullptr, 0 );
STRING expanded;
expanded.setMinSize(newSize+1);
newSize = ExpandEnvironmentStrings( c_str(), LPSTR(expanded.c_str()), newSize );
expanded.setActSize( newSize );
return expanded;
}
};
/*
Traits for registry types, string, expandedStrings, long and 64 bit long
Here we defined fopr each supported C++ types the registry types used
*/
template <typename REGTYPE>
class RegTraits
{
// this is private, to ensure this will be not used for
// unspecalized types
static const RegistryType s_registryType = rtNONE;
};
template <>
struct RegTraits<gak::STRING>
{
static const RegistryType s_registryType = rtSTRING;
};
template <>
struct RegTraits<EnvSTRING>
{
static const RegistryType s_registryType = rtENV;
};
template <>
struct RegTraits<long>
{
static const RegistryType s_registryType = rtINTEGER;
};
template <>
struct RegTraits<gak::int64>
{
static const RegistryType s_registryType = rtINT64;
};
/*
Traits for reading registry values
the default can be used for integer like types
*/
template <typename RESULTTYPE>
class ResultTraits : public RegTraits<RESULTTYPE>
{
RESULTTYPE m_valueBuff;
public:
static const bool s_fixBufferSize = true;
ResultTraits() {}
size_t expand( size_t valueSize )
{
return valueSize;
}
void setBufferSize( size_t )
{
}
size_t size() const
{
return sizeof( m_valueBuff );
}
void *getBuffer() const
{
return (void *)&m_valueBuff;
}
void setActSize( size_t )
{
}
RESULTTYPE GetValue()
{
return m_valueBuff;
}
static size_t GetValueSize(const RESULTTYPE &value)
{
return sizeof(RESULTTYPE);
}
};
/*
The special for STRING
*/
template <>
class ResultTraits<gak::STRING> : public RegTraits<gak::STRING>
{
size_t m_minSize;
gak::STRING m_valueBuff;
void release()
{
m_valueBuff.release();
m_minSize = 0;
}
public:
static const bool s_fixBufferSize = false;
ResultTraits() : m_minSize(0) {}
size_t expand( size_t valueSize )
{
setActSize(valueSize);
gak::STRING newBuffer = m_valueBuff;
DWORD newSize = ExpandEnvironmentStrings( newBuffer, nullptr, 0 );
release(); // this ensures, that this is a real new buffer
setBufferSize(newSize+1);
return ExpandEnvironmentStrings( newBuffer, LPSTR(m_valueBuff.c_str()), newSize );
}
void setBufferSize( size_t size )
{
if( size > m_minSize ) // never shrink the buffer
{
m_minSize = size;
m_valueBuff.release(); // do not copy the existing buffer -> create a new one
/// TODO: analyse what is faster: memory mnagement or block copying
m_valueBuff.setMinSize( size+1 );
}
}
size_t size() const
{
return m_minSize;
}
void *getBuffer() const
{
return (void *)m_valueBuff.c_str();
}
void setActSize( size_t size )
{
if( !size )
m_valueBuff.release();
else
{
if( !m_valueBuff.c_str()[size-1] )
--size;
m_valueBuff.setActSize(size);
}
}
gak::STRING GetValue()
{
return m_valueBuff;
}
static size_t GetValueSize(const gak::STRING &value)
{
return strlen(value)+1;
}
};
//
// Envstring are read like normal strings but use another type
//
template <>
class ResultTraits<EnvSTRING> : public ResultTraits<gak::STRING>
{
public:
static const RegistryType s_registryType = rtENV;
};
/*
Traits for writeing registry values
the default can be used for integer like types
*/
template <typename INPUTTYPE>
struct InputTraits : public RegTraits<INPUTTYPE>
{
const INPUTTYPE m_value;
InputTraits(const INPUTTYPE &value) : m_value(value) {}
size_t GetValueSize()
{
return sizeof(INPUTTYPE);
}
LPBYTE GetAdress() const
{
return LPBYTE(&m_value);
}
};
template <>
struct InputTraits<gak::STRING> : public RegTraits<gak::STRING>
{
const gak::STRING &m_value;
InputTraits(const gak::STRING &value) : m_value(value) {}
size_t GetValueSize()
{
return m_value.isNullPtr() ? 0 : m_value.strlen()+1;
}
LPBYTE GetAdress() const
{
return LPBYTE(m_value.c_str());
}
};
//
// char pointer use the same registry type like STRING
//
template <>
struct InputTraits<const char*> : public RegTraits<gak::STRING>
{
const char *m_value;
InputTraits(const char *value) : m_value(value) {}
size_t GetValueSize()
{
return m_value ? strlen(m_value)+1 : 0;
}
LPBYTE GetAdress() const
{
return LPBYTE(m_value);
}
};
//
// Envstring are written like normal strings but use another type
//
template <>
struct InputTraits<EnvSTRING> : public InputTraits<gak::STRING>
{
static const RegistryType s_registryType = rtENV;
InputTraits(const EnvSTRING &value) : InputTraits<gak::STRING>(value) {}
};
/*
==================================================================
the main registry class for reading and writing
==================================================================
*/
class Registry : public gak::CopyProtection
{
protected:
HKEY m_key;
long m_perm;
private:
long createKey2( HKEY parent, const char *name, unsigned long perm );
long openKey2( HKEY parent, const char *name, unsigned long perm );
static RegistryType queryValue( HKEY key, const char *var, void *buffer, size_t *io_size );
static long getValueSize(HKEY key, const char *var, size_t *size)
{
DWORD valueSize = 0;
long openResult = RegQueryValueEx(
key, var, nullptr, nullptr, nullptr, &valueSize
);
*size = valueSize;
return openResult;
}
template <class RESULTTYPE>
static ReadSuccess readValue( HKEY key, const char *var, RESULTTYPE *result, long perm );
template <class INPUTTYPE>
static long writeValue( HKEY key, const char *var, const INPUTTYPE &i_value )
{
typedef InputTraits<INPUTTYPE> tInputTraits;
tInputTraits value(i_value);
DWORD valueSize = DWORD(value.GetValueSize());
return RegSetValueEx( key, var, 0, tInputTraits::s_registryType, value.GetAdress(), valueSize );
}
static long setKeyValue( HKEY key, const char *keyName, RegistryType type, const void *data, size_t len )
{
// Unike ...Ex RegSetValue creates a new key with a nameless value
return RegSetValue( key, keyName, type, LPCSTR(data), DWORD(len) );
}
static long setValueEx( HKEY key, const char *var, RegistryType type, const void *data, size_t len )
{
// Unike ...Ex RegSetValue creates a new key with a nameless value
return RegSetValueEx( key, var, 0, type, LPBYTE(data), DWORD(len) );
}
public:
Registry() : m_key(0), m_perm(0) {}
~Registry()
{
close();
}
operator bool () const
{
return m_key != 0;
}
void close()
{
if( m_key )
{
RegCloseKey( m_key );
m_key = 0;
}
}
/*
creating new keys
*/
/// create a public key under Local HKEY_LOCAL_MACHINE
long createPublic( const char *name, unsigned long perm=KEY_ALL_ACCESS|KEY_WOW64_64KEY )
{
return createKey2( HKEY_LOCAL_MACHINE, name, perm );
}
/// create a private key under Local HKEY_CURRENT_USER
long createPrivate( const char *name, unsigned long perm=KEY_ALL_ACCESS|KEY_WOW64_64KEY )
{
return createKey2( HKEY_CURRENT_USER, name, perm );
}
/// open a public or private key
long createKey( bool publicKey, const char *name, unsigned long perm=KEY_ALL_ACCESS|KEY_WOW64_64KEY )
{
return publicKey ? createPublic( name, perm ) : createPrivate( name, perm );
}
/// create a sub key under an open key
long createKey( const Registry &parent, const char *name, unsigned long perm=KEY_ALL_ACCESS|KEY_WOW64_64KEY )
{
return createKey2( parent.m_key, name, perm );
}
/*
Opening existing keys
*/
/// open a private key from Local HKEY_CURRENT_USER
long openUsers( const char *name, unsigned long perm=KEY_READ|KEY_WOW64_64KEY )
{
return openKey2( HKEY_USERS, name, perm );
}
/// open a private key from Local HKEY_CURRENT_USER
long openPrivate( const char *name, unsigned long perm=KEY_READ|KEY_WOW64_64KEY )
{
return openKey2( HKEY_CURRENT_USER, name, perm );
}
/// open a public key from Local HKEY_LOCAL_MACHINE
long openPublic( const char *name, unsigned long perm=KEY_READ|KEY_WOW64_64KEY )
{
return openKey2( HKEY_LOCAL_MACHINE, name, perm );
}
/// open a public or private key
long openKey( bool publicKey, const char *name, unsigned long perm=KEY_READ|KEY_WOW64_64KEY )
{
return publicKey ? openPublic( name, perm ) : openPrivate( name, perm );
}
/// open a sub key from an open key
long openSubkey( const Registry &parent, const char *name, unsigned long perm=KEY_READ|KEY_WOW64_64KEY )
{
return openKey2( parent.m_key, name, perm );
}
/*
deleting existing keys/values
*/
#ifndef __BORLANDC__ // my old Borland C++ Builder does not know that windows function
/// delete a sub key you may decide to delete the 64 or 32 bit path
/// TODO: find out whether it is usefull to decide between a 64 and 32 bit path
long deleteSubkeyEx( const char *keyName, unsigned long perm ) const
{
return RegDeleteKeyEx( m_key, keyName, perm, 0 );
}
#endif
/// delete a sub key
long deleteSubkey( const char *keyName ) const
{
return RegDeleteKey( m_key, keyName );
}
/// delete a value
long deleteValue( const char *name ) const
{
return RegDeleteValue( m_key, name );
}
/*
Reading values whithout allocation
These function do not allocate the buffer
*/
/// load a value into an existing buffer
RegistryType queryValue( const char *var, void *buffer, size_t *io_size ) const
{
return queryValue( m_key, var, buffer, io_size );
}
/// load a nameless value into an existing buffer
RegistryType queryValue( void *buffer, size_t *io_size ) const
{
return queryValue( m_key, nullptr, buffer, io_size );
}
/// get the content size of a value
long getValueSize(const char *var, size_t *size) const
{
return getValueSize(m_key, var, size);
}
/*
Reading values whith allocation
*/
/// reading a value from any key
template <class RESULTTYPE>
ReadSuccess readValue( const char *var, RESULTTYPE *result ) const
{
return readValue( m_key, var, result, m_perm );
}
/// reading a nameless value from any key
template <class RESULTTYPE>
ReadSuccess readValue( RESULTTYPE *result ) const
{
return readValue( m_key, nullptr, result, m_perm );
}
/*
Writing
*/
/// write a value of any type
template <class INPUTTYPE>
long writeValue( const char *var, const INPUTTYPE &i_value ) const
{
return writeValue( m_key, var, i_value );
}
/*
legacy writing
*/
/// write a key with unamed value given type and size
long setKeyValue( const char *keyName, RegistryType type, const void *data, size_t len ) const
{
return setKeyValue( m_key, keyName, type, data, len );
}
/// write an unnamed value with given type and size
long setKeyValue( RegistryType type, const void *data, size_t len ) const
{
return setValueEx( m_key, nullptr, type, data, len );
}
/// write a key with unamed text value
long setKeyValue( const char *keyName, const gak::STRING &data ) const
{
return setKeyValue( m_key, keyName, rtSTRING, data.c_str(), data.size()+1 );
}
/// write an unnamed text value
long setKeyValue( const gak::STRING &data ) const
{
return setValueEx( m_key, nullptr, rtSTRING, data.c_str(), data.size()+1 );
}
/*
querying key and value names
*/
class key_iterator
{
friend class Registry;
const Registry &m_reg;
DWORD m_numKeys, m_maxTitleSize, m_index;
bool m_loaded;
STRING m_name;
void next()
{
++m_index;
m_name.release();
m_loaded = false;
}
key_iterator( const Registry ® );
public:
const STRING &operator * ();
const STRING *operator -> ()
{
operator *();
return &m_name;
}
const key_iterator & operator ++() // pre-inkrement
{
next();
return *this;
}
key_iterator operator ++(int) // post-inkrement
{
key_iterator old = *this;
next();
return old;
}
bool operator == ( const key_iterator &other )
{
return m_index == other.m_index;
}
bool operator != ( const key_iterator &other )
{
return m_index != other.m_index;
}
};
key_iterator kbegin() const
{
return key_iterator( *this );
}
key_iterator kend() const
{
key_iterator end = kbegin();
end.m_index = end.m_numKeys;
return end;
}
class value_iterator
{
friend class Registry;
const Registry &m_reg;
DWORD m_numValues, m_maxTitleSize, m_maxValueSize, m_index;
bool m_loaded;
RegValuePair m_value;
void next()
{
++m_index;
m_value.type = rtERROR;
m_value.valueName.release();
m_value.valueBuffer.release();
m_loaded = false;
}
value_iterator( const Registry ® );
public:
const RegValuePair &operator * ();
const RegValuePair *operator -> ()
{
operator *();
return &m_value;
}
const value_iterator & operator ++() // pre-inkrement
{
next();
return *this;
}
value_iterator operator ++(int) // post-inkrement
{
value_iterator old = *this;
next();
return old;
}
bool operator == ( const value_iterator &other )
{
return m_index == other.m_index;
}
bool operator != ( const value_iterator &other )
{
return m_index != other.m_index;
}
};
value_iterator vbegin() const
{
return value_iterator( *this );
}
value_iterator vend() const
{
value_iterator end = vbegin();
end.m_index = end.m_numValues;
return end;
}
#ifdef __BORLANDC__
friend class Registry::key_iterator;
friend class Registry::value_iterator;
#endif
/*
old functions for testing only
*/
void _getKeyNames( gak::ArrayOfStrings *keyNames );
void _getValueNames( gak::ArrayOfStrings *valueNames );
void _getValuePairs( RegValuePairs *valuePairs );
// for testing, only can be used to create values of type REG_EXPAND_SZ
long setValueEx( const char *var, RegistryType type, const void *data, size_t len ) const
{
return setValueEx( m_key, var, type, data, len );
}
#if 0
/// this method is useful, when migrating old code that still uses the windows registry api
HKEY get() const
{
return m_key;
}
#endif
};
template <HKEY ROOT_KEY>
class RegistryRoot : public Registry
{
public:
RegistryRoot()
{
m_key = ROOT_KEY;
}
~RegistryRoot()
{
m_key = 0;
}
};
typedef RegistryRoot<HKEY_CLASSES_ROOT> _RegistryClassesRoot;
typedef RegistryRoot<HKEY_LOCAL_MACHINE> RegistryLocalMachine;
typedef RegistryRoot<HKEY_CURRENT_USER> RegistryCurrentUser;
typedef RegistryRoot<HKEY_USERS> RegistryUsers;
class RegistryClassesRoot : public Registry
{
public:
RegistryClassesRoot( bool publicKey = true )
{
openKey( publicKey, "Software\\Classes" );
}
};
// --------------------------------------------------------------------- //
// ----- exported datas ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module static data -------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class static data --------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- prototypes ---------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module functions ---------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class inlines ------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class constructors/destructors -------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class static functions ---------------------------------------- //
// --------------------------------------------------------------------- //
template <class RESULTTYPE>
ReadSuccess Registry::readValue( HKEY key, const char *var, RESULTTYPE *result, long perm )
{
typedef ResultTraits<RESULTTYPE> tResultTraits;
tResultTraits valueBuff;
ReadSuccess retCode = rsNotFound;
if( !tResultTraits::s_fixBufferSize )
{
size_t size;
long error = getValueSize( key, var, &size );
if( error == ERROR_SUCCESS )
{
if( size )
valueBuff.setBufferSize(size+1); // reserve 1 byte extra space for the trailing 0 byte
else
{
retCode = rsOK;
*result = RESULTTYPE();
}
}
else if( error != ERROR_FILE_NOT_FOUND )
{
retCode = rsError;
}
}
size_t valueSize = valueBuff.size();
if( valueSize )
{
RegistryType typeVar = queryValue( key, var, valueBuff.getBuffer(), &valueSize );
if( typeVar == rtENV && rtSTRING == tResultTraits::s_registryType )
{
valueSize = valueBuff.expand( valueSize );
typeVar = rtSTRING;
}
if( valueSize > 0
&& typeVar == tResultTraits::s_registryType )
{
if( !tResultTraits::s_fixBufferSize )
{
valueBuff.setActSize(valueSize);
retCode = rsOK;
}
else if(valueSize == valueBuff.size() )
{
retCode = rsOK;
}
else
{
retCode = rsBadSize;
}
if( retCode == rsOK )
{
*result = valueBuff.GetValue();
}
}
else if( var && typeVar == rtMISSING )
{
Registry subKey;
if( subKey.openKey2( key, var, perm ) == ERROR_SUCCESS )
{
retCode = readValue(subKey.m_key, nullptr, result, perm );
}
}
else if( typeVar == rtERROR )
retCode = rsError;
else
retCode = rsBadType;
}
else if( var && retCode == rsNotFound )
{
Registry subKey;
if( subKey.openKey2( key, var, perm ) == ERROR_SUCCESS )
{
retCode = readValue(subKey.m_key, nullptr, result, perm );
}
}
return retCode;
}
// --------------------------------------------------------------------- //
// ----- class privates ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class protected ----------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class virtuals ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class publics ------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- entry points -------------------------------------------------- //
// --------------------------------------------------------------------- //
} // namespace winlib
#ifdef __BORLANDC__
# pragma option -RT.
# pragma option -b.
# pragma option -a.
# pragma option -p.
#endif
#endif // GAK_WINLIB_REGISTRY_H