Ada Programming/Libraries/GNAT.Registry

From Wikibooks, open books for an open world
Jump to: navigation, search

Ada Lovelace 1838.jpg


This package provides a high level binding to the Windows registry. It is possible to do simple things like reading a key value and creating a new key. This binding handles only key values of type Standard.String. For a full registry API (but at a lower level of abstraction) refer to the Win32.Winreg package provided with the Win32Ada binding.

An example of how to use this package:

with GNAT.registry;
with Ada.Text_IO;
with Ada.Exceptions;
with Ada.Strings.Unbounded;

use GNAT.registry;
use Ada.Text_IO;
use Ada.Exceptions;
use Ada.Strings.Unbounded;

-- This is an example of how to manipulate the windows registry using
-- the GNAT.registry class. It reads in the value of a key if it exists,
-- otherwise, it creates a new key, modifies its value, and then removes the key.

procedure Registry_Manip is

        reg : HKEY;
        key : String := "SOFTWARE\Raytheon";
        subKey : String := "URL";
        subKeyValue : String := "";
        currentValue : Unbounded_String;


        Put_Line("Creating registry key: " & key);
        reg := Create_Key(HKEY_CURRENT_USER, key);

        Put_Line("Testing to see if key already exists (key_exists() doesn't work)");
                currentValue := To_Unbounded_String(Query_Value(reg, subKey));
                        when Registry_Error =>
                                currentValue := To_Unbounded_String(""); --No Key Found
        if (currentValue /= "") then
                Put_Line("Key Exists! Value: " & To_String(currentValue));
                Put_Line("Key does not Exist.");
        end if;

        Put_Line("Added registry sub-key: " & subKey);
        Put_Line("      with a value of: " & subKeyValue);
        Set_Value(reg, subKey, subKeyValue);
--      Put_Line("Deleting registry sub-key: " & subKey);
--      Delete_Value(reg, subKey);
--      Put_Line("Deleting registry key: " & key);
--      Delete_Key(HKEY_CURRENT_USER, key);

        Put_Line("Closing the key");

        -- This will catch all Registry_Error exceptions that may be thrown     
                when E : others =>
           Put_Line(Exception_Name (E) & ": " & Exception_Message (E));

end Registry_Manip;

See also[edit]


External links[edit]