Hello everybody
i'm using the code below to read/write into Windows registry. I had no
problems in delphi 7 / windows XP
now i'm porting my application in Delphi 2007 / Windows Vista
When i try to read a key in the registry (it exists for sure cause i can see
it using regedit) it's not found in my application
The code i use is:
keyvalue:=GetRVStr(hkLocalMachine,'MYCOMPANYNAME\MYAPPLICATION','MYKEY','');
The same application compiled in Delphi 7 can read the key correctly. Seems
to me that the problem is in Delphi 2007. Maybe have to change something in
the code ?
Debugging the code i see the function Registry.OpenKey(Key,false) returns
false
thank you for any help
Roberto
unit fn_Registry;
interface
uses
Windows,
SysUtils,
Classes,
Registry;
type
TRootKeys =
(hkClassesRoot,hkCurrentConfig,hkCurrentUser,hkDynData,hkLocalMachine,hkUsers);
EIRCannotWriteValue = class(Exception);
function RegKeyExists(Root: TRootKeys;Key: String): Boolean;
procedure WriteRVInteger(Root: Trootkeys;Key,Ident: String;Value: Integer);
procedure WriteRVStr(Root: TRootKeys;Key,Ident,Value: String);
function GetRVStr(Root: TRootKeys;Key,Ident,Default: String): String;
function GetRVInteger(Root: TRootKeys;Key,Ident: String;Default: Integer):
Integer;
implementation
procedure SetTheRoot(ARegistry: TRegistry;Root: TRootKeys);
begin
case Root of
hkClassesRoot: ARegistry.RootKey := HKEY_CLASSES_ROOT;
hkCurrentUser: ARegistry.RootKey := HKEY_CURRENT_USER;
hkLocalMachine: ARegistry.RootKey := HKEY_LOCAL_MACHINE;
hkUsers: ARegistry.RootKey := HKEY_USERS;
hkCurrentConfig: ARegistry.RootKey := HKEY_CURRENT_CONFIG;
hkDynData: ARegistry.RootKey := HKEY_DYN_DATA;
end;
end;
procedure WriteRVInteger(Root: Trootkeys;Key,Ident: String;Value: Integer);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
try
SetTheRoot(Registry,Root);
if not Registry.OpenKey(Key,TRUE) then raise
EIRCannotWriteValue.Create(Key);
try
Registry.WriteInteger(Ident,Value);
except
raise EIRCannotWriteValue.Create(Key);
end;
finally
Registry.Free;
end;
end;
procedure WriteRVStr(Root: TRootKeys;Key,Ident,Value: String);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
try
SetTheRoot(Registry,Root);
if not Registry.OpenKey(Key,TRUE) then
raise EIRCannotWriteValue.Create(Format('Cannot install into registry.
Key: %s Ident: %s Value %s',[Key,Ident,Value]));
try
Registry.WriteString(Ident,Value);
except
raise EIRCannotWriteValue.Create(Format('Cannot install into registry.
Key: %s Ident: %s Value %s',[Key,Ident,Value]));
end;
finally
Registry.Free;
end;
end;
function RegKeyExists(Root: TRootKeys;Key: String): Boolean;
var
Registry: TRegistry;
begin
Result := False;
Registry := TRegistry.Create;
try
SetTheRoot(Registry,Root);
result:=Registry.KeyExists(Key);
finally
Registry.Free;
end;
end;
function GetRVStr(Root: TRootKeys;Key,Ident,Default: String): String;
var
Registry: TRegistry;
begin
Result := Default;
Registry := TRegistry.Create;
try
SetTheRoot(Registry,Root);
if not Registry.KeyExists(Key) then Exit;
if not Registry.OpenKey(Key,FALSE) then Exit;
if not Registry.ValueExists(Ident) then Exit;
try
Result := Registry.ReadString(Ident);
except
Result := Default;
end;
finally
Registry.Free;
end;
end;
function GetRVInteger(Root: TRootKeys;Key,Ident: String;Default: Integer):
Integer;
var
Registry: TRegistry;
begin
Result := Default;
Registry := TRegistry.Create;
try
SetTheRoot(Registry,Root);
if not Registry.KeyExists(Key) then Exit;
if not Registry.OpenKey(Key,FALSE) then Exit;
if not Registry.ValueExists(Ident) then Exit;
try
Result := Registry.ReadInteger(Ident);
except
Result := Default;
end;
finally
Registry.Free;
end;
end;
end.