1+ #include < windows.h>
2+ #include < winnls.h>
3+ #include < shobjidl.h>
4+ #include < objbase.h>
5+ #include < objidl.h>
6+ #include < shlguid.h>
7+ #include " data.h"
8+
9+ HKEY app_root = DEF_ROOT;
10+
11+
12+
13+
14+ /* ___________________________________________________________________
15+ | regKeyCrOp:
16+ | Attempts to create new registry key, if already exists, opens it
17+ |
18+ | sub_k: Path to the new key from application root key
19+ |
20+ | Return value:
21+ | Handle to created or opened key
22+ |____________________________________________________________________*/
23+ HKEY regKeyCrOp (LPCWSTR sub_k);
24+
25+
26+ /* ___________________________________________________________________
27+ | regKeyOp:
28+ | Opens already exising key
29+ |
30+ | sub_k: Path to the key from application root key
31+ |
32+ | Return value:
33+ | Key exists -> Handle to the key
34+ | Key does not exist -> NULL
35+ |____________________________________________________________________*/
36+ HKEY regKeyOp (LPCWSTR sub_k);
37+
38+
39+ /* ___________________________________________________________________
40+ | regKeyOp:
41+ | Destroys registry key and all it's sub-keys/values
42+ |
43+ | sub_k: Path to the key from application root key
44+ |____________________________________________________________________*/
45+ void regKeyDel (LPCWSTR sub_k);
46+
47+
48+
49+
50+ bool regChk (LPCWSTR app_key, LPCWSTR name)
51+ {
52+ HKEY key = regKeyOp (app_key);
53+
54+ // Get Registry Value ============================================================================
55+ LSTATUS res = RegGetValue (
56+ key, // [I] Handle to opened key (KEY_QUERY_VALUE acc. req.)
57+ NULL , // [I|O] Name of the registry subkey to be retrieved
58+ name, // [I|O] Name of the value (if NULL or "" get def. value)
59+ RRF_RT_ANY, // [I|O] Flags comb. of data types to set filter (+ opt.)
60+ NULL , // [O|O] Ptr to variable that recieves value type
61+ NULL , // [O|O] Ptr to var that receive data of the value || NULL
62+ NULL ); // [IO|O] Ptr sz. B -> N. of B cop. (NULL only pvData = NULL)
63+ // ===============================================================================================
64+
65+ RegCloseKey (key);
66+ return !res;
67+ }
68+
69+ DWORD regGet (LPCWSTR app_key, LPCWSTR name)
70+ {
71+ HKEY key = regKeyOp (app_key);
72+ DWORD dat = 0 , sz = sizeof (dat);
73+
74+ // Get Registry Value ============================================================================
75+ RegGetValue (
76+ key, // [I] Handle to opened key (KEY_QUERY_VALUE acc. req.)
77+ NULL , // [I|O] Name of the registry subkey to be retrieved
78+ name, // [I|O] Name of the value (if NULL or "" get def. value)
79+ RRF_RT_REG_DWORD, // [I|O] Flags comb. of data types to set filter (+ opt.)
80+ NULL , // [O|O] Ptr to variable that recieves value type
81+ &dat, // [O|O] Ptr to var that receive data of the value || NULL
82+ &sz); // [IO|O] Ptr sz. B -> N. of B cop. (NULL only pvData = NULL)
83+ // ===============================================================================================
84+
85+ RegCloseKey (key);
86+ return dat;
87+ }
88+
89+ DWORD regGet (LPCWSTR app_key, LPCWSTR name, LPWSTR *out)
90+ {
91+ HKEY key = regKeyOp (app_key);
92+ DWORD sz = 0 ;
93+
94+ // Get Registry Value Data size ==================================================================
95+ RegGetValue (
96+ key, // [I] Handle to opened key (KEY_QUERY_VALUE acc. req.)
97+ NULL , // [I|O] Name of the registry subkey to be retrieved
98+ name, // [I|O] Name of the value (if NULL or "" get def. value)
99+ RRF_RT_REG_SZ, // [I|O] Flags comb. of data types to set filter (+ opt.)
100+ NULL , // [O|O] Ptr to variable that recieves value type
101+ NULL , // [O|O] Ptr to var that receive data of the value || NULL
102+ &sz); // [IO|O] Ptr sz. B -> N. of B cop. (NULL only pvData = NULL)
103+ // ===============================================================================================
104+
105+ *out = (LPWSTR)HeapAlloc (GetProcessHeap (), NULL , sz + sizeof (wchar_t ));
106+
107+ // Get Registry Value ============================================================================
108+ RegGetValue (
109+ key, // [I] Handle to opened key (KEY_QUERY_VALUE acc. req.)
110+ NULL , // [I|O] Name of the registry subkey to be retrieved
111+ name, // [I|O] Name of the value (if NULL or "" get def. value)
112+ RRF_RT_REG_SZ, // [I|O] Flags comb. of data types to set filter (+ opt.)
113+ NULL , // [O|O] Ptr to variable that recieves value type
114+ *out, // [O|O] Ptr to var that receive data of the value || NULL
115+ &sz); // [IO|O] Ptr sz. B -> N. of B cop. (NULL only pvData = NULL)
116+ // ===============================================================================================
117+
118+ RegCloseKey (key);
119+ return sz;
120+ }
121+
122+ void regSet (LPCWSTR app_key, LPCWSTR name)
123+ {
124+ HKEY key = regKeyCrOp (app_key);
125+
126+ // Set Registry Value ============================================================================
127+ RegSetValueEx (
128+ key, // [I] Handle to opened key (KEY_SET_VALUE acc. req.)
129+ name, // [I|O] Value name (if not exist, will be created)
130+ 0 , // Reserved, must be 0
131+ REG_SZ, // [I] Type of value beeing set
132+ NULL , // [I] Ptr to the data that will be stored
133+ NULL ); // [I] Sizeof data in bytes (for strings must include \0)
134+ // ===============================================================================================
135+
136+ RegCloseKey (key);
137+ }
138+
139+ void regSet (LPCWSTR app_key, LPCWSTR name, DWORD data)
140+ {
141+ HKEY key = regKeyCrOp (app_key);
142+
143+ // Set Registry Value ============================================================================
144+ RegSetValueEx (
145+ key, // [I] Handle to opened key (KEY_SET_VALUE acc. req.)
146+ name, // [I|O] Value name (if not exist, will be created)
147+ 0 , // Reserved, must be 0
148+ REG_DWORD, // [I] Type of value beeing set
149+ (const BYTE*)&data, // [I] Ptr to the data that will be stored
150+ sizeof (data)); // [I] Sizeof data in bytes (for strings must include \0)
151+ // ===============================================================================================
152+
153+ RegCloseKey (key);
154+ }
155+
156+ void regSet (LPCWSTR app_key, LPCWSTR name, LPCWSTR data)
157+ {
158+ HKEY key = regKeyCrOp (app_key);
159+ DWORD s = DWORD (wcslen (data)+1 ) * sizeof (wchar_t );
160+
161+ // Set Registry Value ============================================================================
162+ RegSetValueEx (
163+ key, // [I] Handle to opened key (KEY_SET_VALUE acc. req.)
164+ name, // [I|O] Value name (if not exist, will be created)
165+ 0 , // Reserved, must be 0
166+ REG_SZ, // [I] Type of value beeing set
167+ (const BYTE*)data, // [I] Ptr to the data that will be stored
168+ s); // [I] Sizeof data in bytes (for strings must include \0)
169+ // ===============================================================================================
170+
171+ RegCloseKey (key);
172+ }
173+
174+ void regDel (LPCWSTR app_key, LPCWSTR name)
175+ {
176+ HKEY key = regKeyOp (app_key);
177+
178+ // Delete Registry Value =========================================================================
179+ RegDeleteValue (
180+ key, // [I] Handle to opened key (KEY_SET_VALUE acc. req.)
181+ name); // [I|O] Value name to be removed
182+ // ===============================================================================================
183+
184+ RegCloseKey (key);
185+ }
186+
187+ void regSetRoot (HKEY root)
188+ {
189+ app_root = root;
190+ }
191+
192+ HKEY regKeyCrOp (LPCWSTR sub_k)
193+ {
194+ REGSAM sam = KEY_SET_VALUE | KEY_QUERY_VALUE;
195+ HKEY key = NULL ;
196+
197+ // Create Registry Key ===========================================================================
198+ RegCreateKeyEx (
199+ app_root, // [I] Handle to open reg key, or root key
200+ sub_k, // [I] Subkey name relative to 1st param (32 lvl deep)
201+ 0 , // Reserved, must be 0
202+ NULL , // [I|O] Class type of this key (OOP shenenigans?)
203+ REG_OPTION_NON_VOLATILE, // [I] Key options (0 -> std non-volatile key)
204+ sam, // [I] Security and Access Mask
205+ NULL , // [I|O] Ptr to SECURITY_ATTRIBUTES (handle inheritability)
206+ &key, // [O] Ptr to HKEY variable that will hold returned handle
207+ NULL ); // [O|O] Dis-pos -> CREATED_NEW_KEY | OPENED_EXISTING_KEY
208+ // ===============================================================================================
209+
210+ return key;
211+ }
212+
213+ HKEY regKeyOp (LPCWSTR sub_k)
214+ {
215+ REGSAM sam = KEY_SET_VALUE | KEY_QUERY_VALUE;
216+ HKEY key = NULL ;
217+
218+ // Open Registry Key =============================================================================
219+ RegOpenKeyEx (
220+ app_root, // [I] Handle to open reg key, or root key
221+ sub_k, // [I|O] Name of the registry subkey to be opened
222+ 0 , // [I] Options -> 0 || REG_OPTION_OPEN_LINK
223+ sam, // [I] Security and Access Mask
224+ &key); // [O] Ptr to variable that receives opened key handle
225+ // ===============================================================================================
226+
227+ return key;
228+ }
229+
230+ void regKeyDel (LPCWSTR sub_k)
231+ {
232+ // Delete Registry Key ===========================================================================
233+ RegDeleteKeyEx (
234+ HKEY_CLASSES_ROOT, // [I] Handle to open reg key, or root key
235+ sub_k, // [I] Subkey name to be deleted, relative to 1st param
236+ KEY_WOW64_64KEY, // [I] Key platform -> KEY_WOW64_32KEY | KEY_WOW64_64KEY
237+ 0 ); // Reserved, must be 0
238+ // ===============================================================================================
239+ }
0 commit comments