1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . R E G I S T R Y --
9 -- Copyright (C) 2001-2003 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 ------------------------------------------------------------------------------
36 with GNAT
.Directory_Operations
;
38 package body GNAT
.Registry
is
43 ------------------------------
44 -- Binding to the Win32 API --
45 ------------------------------
47 subtype LONG
is Interfaces
.C
.long
;
48 subtype ULONG
is Interfaces
.C
.unsigned_long
;
49 subtype DWORD
is ULONG
;
51 type PULONG
is access all ULONG
;
52 subtype PDWORD
is PULONG
;
53 subtype LPDWORD
is PDWORD
;
55 subtype Error_Code
is LONG
;
57 subtype REGSAM
is LONG
;
59 type PHKEY
is access all HKEY
;
61 ERROR_SUCCESS
: constant Error_Code
:= 0;
63 REG_SZ
: constant := 1;
64 REG_EXPAND_SZ
: constant := 2;
66 function RegCloseKey
(Key
: HKEY
) return LONG
;
67 pragma Import
(Stdcall
, RegCloseKey
, "RegCloseKey");
69 function RegCreateKeyEx
76 lpSecurityAttributes
: Address
;
78 lpdwDisposition
: LPDWORD
)
80 pragma Import
(Stdcall
, RegCreateKeyEx
, "RegCreateKeyExA");
86 pragma Import
(Stdcall
, RegDeleteKey
, "RegDeleteKeyA");
88 function RegDeleteValue
90 lpValueName
: Address
)
92 pragma Import
(Stdcall
, RegDeleteValue
, "RegDeleteValueA");
97 lpValueName
: Address
;
98 lpcbValueName
: LPDWORD
;
104 pragma Import
(Stdcall
, RegEnumValue
, "RegEnumValueA");
106 function RegOpenKeyEx
113 pragma Import
(Stdcall
, RegOpenKeyEx
, "RegOpenKeyExA");
115 function RegQueryValueEx
117 lpValueName
: Address
;
118 lpReserved
: LPDWORD
;
123 pragma Import
(Stdcall
, RegQueryValueEx
, "RegQueryValueExA");
125 function RegSetValueEx
127 lpValueName
: Address
;
133 pragma Import
(Stdcall
, RegSetValueEx
, "RegSetValueExA");
135 ---------------------
136 -- Local Constants --
137 ---------------------
139 Max_Key_Size
: constant := 1_024
;
140 -- Maximum number of characters for a registry key
142 Max_Value_Size
: constant := 2_048
;
143 -- Maximum number of characters for a key's value
145 -----------------------
146 -- Local Subprograms --
147 -----------------------
149 function To_C_Mode
(Mode
: Key_Mode
) return REGSAM
;
150 -- Returns the Win32 mode value for the Key_Mode value.
152 procedure Check_Result
(Result
: LONG
; Message
: String);
153 -- Checks value Result and raise the exception Registry_Error if it is not
154 -- equal to ERROR_SUCCESS. Message and the error value (Result) is added
155 -- to the exception message.
161 procedure Check_Result
(Result
: LONG
; Message
: String) is
165 if Result
/= ERROR_SUCCESS
then
166 Exceptions
.Raise_Exception
167 (Registry_Error
'Identity,
168 Message
& " (" & LONG
'Image (Result
) & ')');
176 procedure Close_Key
(Key
: HKEY
) is
180 Result
:= RegCloseKey
(Key
);
181 Check_Result
(Result
, "Close_Key");
191 Mode
: Key_Mode
:= Read_Write
)
197 REG_OPTION_NON_VOLATILE
: constant := 16#
0#
;
199 C_Sub_Key
: constant String := Sub_Key
& ASCII
.Nul
;
200 C_Class
: constant String := "" & ASCII
.Nul
;
201 C_Mode
: constant REGSAM
:= To_C_Mode
(Mode
);
203 New_Key
: aliased HKEY
;
205 Dispos
: aliased DWORD
;
208 Result
:= RegCreateKeyEx
210 C_Sub_Key
(C_Sub_Key
'First)'Address,
212 C_Class
(C_Class
'First)'Address,
213 REG_OPTION_NON_VOLATILE
,
216 New_Key
'Unchecked_Access,
217 Dispos
'Unchecked_Access);
219 Check_Result
(Result
, "Create_Key " & Sub_Key
);
227 procedure Delete_Key
(From_Key
: HKEY
; Sub_Key
: String) is
228 C_Sub_Key
: constant String := Sub_Key
& ASCII
.Nul
;
232 Result
:= RegDeleteKey
(From_Key
, C_Sub_Key
(C_Sub_Key
'First)'Address);
233 Check_Result
(Result
, "Delete_Key " & Sub_Key
);
240 procedure Delete_Value
(From_Key
: HKEY
; Sub_Key
: String) is
241 C_Sub_Key
: constant String := Sub_Key
& ASCII
.Nul
;
245 Result
:= RegDeleteValue
(From_Key
, C_Sub_Key
(C_Sub_Key
'First)'Address);
246 Check_Result
(Result
, "Delete_Value " & Sub_Key
);
249 -------------------------
250 -- For_Every_Key_Value --
251 -------------------------
253 procedure For_Every_Key_Value
255 Expand
: Boolean := False)
257 use GNAT
.Directory_Operations
;
264 Sub_Key
: String (1 .. Max_Key_Size
);
265 pragma Warnings
(Off
, Sub_Key
);
267 Value
: String (1 .. Max_Value_Size
);
268 pragma Warnings
(Off
, Value
);
270 Size_Sub_Key
: aliased ULONG
;
271 Size_Value
: aliased ULONG
;
272 Type_Sub_Key
: aliased DWORD
;
278 Size_Sub_Key
:= Sub_Key
'Length;
279 Size_Value
:= Value
'Length;
281 Result
:= RegEnumValue
284 Size_Sub_Key
'Unchecked_Access,
286 Type_Sub_Key
'Unchecked_Access,
288 Size_Value
'Unchecked_Access);
290 exit when not (Result
= ERROR_SUCCESS
);
294 if Type_Sub_Key
= REG_EXPAND_SZ
and then Expand
then
295 Action
(Natural (Index
) + 1,
296 Sub_Key
(1 .. Integer (Size_Sub_Key
)),
297 Directory_Operations
.Expand_Path
298 (Value
(1 .. Integer (Size_Value
) - 1),
299 Directory_Operations
.DOS
),
302 elsif Type_Sub_Key
= REG_SZ
or else Type_Sub_Key
= REG_EXPAND_SZ
then
303 Action
(Natural (Index
) + 1,
304 Sub_Key
(1 .. Integer (Size_Sub_Key
)),
305 Value
(1 .. Integer (Size_Value
) - 1),
313 end For_Every_Key_Value
;
327 New_Key
:= Open_Key
(From_Key
, Sub_Key
);
330 -- We have been able to open the key so it exists
335 when Registry_Error
=>
337 -- An error occurred, the key was not found
349 Mode
: Key_Mode
:= Read_Only
)
354 C_Sub_Key
: constant String := Sub_Key
& ASCII
.Nul
;
355 C_Mode
: constant REGSAM
:= To_C_Mode
(Mode
);
357 New_Key
: aliased HKEY
;
361 Result
:= RegOpenKeyEx
363 C_Sub_Key
(C_Sub_Key
'First)'Address,
366 New_Key
'Unchecked_Access);
368 Check_Result
(Result
, "Open_Key " & Sub_Key
);
379 Expand
: Boolean := False)
382 use GNAT
.Directory_Operations
;
386 Value
: String (1 .. Max_Value_Size
);
387 pragma Warnings
(Off
, Value
);
389 Size_Value
: aliased ULONG
;
390 Type_Value
: aliased DWORD
;
392 C_Sub_Key
: constant String := Sub_Key
& ASCII
.Nul
;
396 Size_Value
:= Value
'Length;
398 Result
:= RegQueryValueEx
400 C_Sub_Key
(C_Sub_Key
'First)'Address,
402 Type_Value
'Unchecked_Access,
403 Value
(Value
'First)'Address,
404 Size_Value
'Unchecked_Access);
406 Check_Result
(Result
, "Query_Value " & Sub_Key
& " key");
408 if Type_Value
= REG_EXPAND_SZ
and then Expand
then
409 return Directory_Operations
.Expand_Path
410 (Value
(1 .. Integer (Size_Value
- 1)), Directory_Operations
.DOS
);
412 return Value
(1 .. Integer (Size_Value
- 1));
425 C_Sub_Key
: constant String := Sub_Key
& ASCII
.Nul
;
426 C_Value
: constant String := Value
& ASCII
.Nul
;
431 Result
:= RegSetValueEx
433 C_Sub_Key
(C_Sub_Key
'First)'Address,
436 C_Value
(C_Value
'First)'Address,
439 Check_Result
(Result
, "Set_Value " & Sub_Key
& " key");
446 function To_C_Mode
(Mode
: Key_Mode
) return REGSAM
is
449 KEY_READ
: constant := 16#
20019#
;
450 KEY_WRITE
: constant := 16#
20006#
;
458 return KEY_READ
+ KEY_WRITE
;