tyfing
asked on
Reg.SaveKey
How can i save all the 'data' of HKEY_LOCAL_MACHINE\Softwar e\C-Cube
into a file C:\Windows\Desktop\back-up .reg when i lcick Button 1 ?
into a file C:\Windows\Desktop\back-up
ASKER
i have tried this b4, doesn't work. :(
Hello
try this
uses
Registry;
procedure TForm1.Button1Click(Sender : TObject);
var
Reg: TRegistry;
SL: TStringList;
begin
try
Reg :=TRegistry.Create;
SL :=TStringList.Create;
Reg.RootKey:=HKey_Local_Ma chine;
if Reg.OpenKey('\software\ora cle',False ) then
begin
Reg.GetValueNames(SL);
SL.SaveToFile('C:\Windows\ Desktop\ba ck-up.reg' );
end;
finally
SL.Free;
Reg.Free;
end;
end;
try this
uses
Registry;
procedure TForm1.Button1Click(Sender
var
Reg: TRegistry;
SL: TStringList;
begin
try
Reg :=TRegistry.Create;
SL :=TStringList.Create;
Reg.RootKey:=HKey_Local_Ma
if Reg.OpenKey('\software\ora
begin
Reg.GetValueNames(SL);
SL.SaveToFile('C:\Windows\
end;
finally
SL.Free;
Reg.Free;
end;
end;
sorry
replace '\software\oracle' with '\Software\C-Cube'
replace '\software\oracle' with '\Software\C-Cube'
ASKER
the .reg file is created but nothing inside, u didn't link sl to anything i think
GetValueNames load all the values in the C-Cube and save it in the, then the SL save it to the file
do yo have sub key in the C-Cube?, if so, this will not work with you
you need to get all the keys first with Reg.GetKeyNames then get the values of each key using GetValueNames
do yo have sub key in the C-Cube?, if so, this will not work with you
you need to get all the keys first with Reg.GetKeyNames then get the values of each key using GetValueNames
ASKER
wow......how ? i do have subkeys
Here is the code I used to export registry data in "Registry 4" format. It gives the same result as Registry Editor's export command.
-------------------------- ---------- ---------
unit RegistryExport;
interface
uses Windows,Classes,Registry,S ysUtils;
const
ExportHeader = 'REGEDIT4';
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
implementation
type
TSubstitution = record
Character : char;
Substitution : String;
end;
const
SubstitutionsConst : array [1..4] of TSubstitution =
( (Character : #10; Substitution : '\n'),
(Character : #13; Substitution : '\r'),
(Character : '"'; Substitution : '\"'),
(Character : '\'; Substitution : '\\')
);
var
Substitutions : array [1..255] of String;
procedure Initialize;
var
i : Integer;
begin
for i:=low(Substitutions) to high(Substitutions) do
Substitutions[i]:='';
for i:=low(SubstitutionsConst) to high(SubstitutionsConst) do
Substitutions[ord(Substitu tionsConst [i].Charac ter)]:=
SubstitutionsConst[i].Subs titution;
end;
function RegistryRootKeyName ( Key : HKEY ) : string;
begin
case Key of
$80000000 : Result:='HKEY_CLASSES_ROOT ';
$80000001 : Result:='HKEY_CURRENT_USER ';
$80000002 : Result:='HKEY_LOCAL_MACHIN E';
$80000003 : Result:='HKEY_USERS';
$80000004 : Result:='HKEY_PERFORMANCE_ DATA';
$80000005 : Result:='HKEY_CURRENT_CONF IG';
$80000006 : Result:='HKEY_DYN_DATA';
else
Result:='';
end;
end;
function NormalizeString( s : String ) : String;
var
i : Integer;
subst : String;
begin
SetLength(Result,Length(s) ); //Try to minimize reallocations
Result:='';
for i:=1 to Length(s) do
begin
subst:=Substitutions[ord(s [i])];
if subst<>'' then
Result:=Result+subst
else
Result:=Result+s[i];
end;
end;
function ConvertValueToStr(Reg : TRegistry; ValueName : String) : String;
var
DataType : TRegDataType;
DataSize : Integer;
Buffer : pointer;
p : ^byte;
b : byte;
i : Integer;
begin
DataType:=Reg.GetDataType( ValueName) ;
case DataType of
rdString,
rdExpandString :
Result := '"'+NormalizeString(Reg.Re adString(V alueName)) +'"';
rdInteger : Result := Format('dword:%.8x',[Reg.R eadInteger (ValueName )]);
rdBinary : begin
DataSize := Reg.GetDataSize(ValueName) ;
GetMem(Buffer,Datasize);
try
if Reg.ReadBinaryData(ValueNa me,Buffer^ ,Datasize) =Datasize then
begin
Result:='hex:';
p:=Buffer;
for i:=0 to Datasize-1 do
begin
b:=p^;
if i<Datasize-1 then
Result:=Result+Format('%.2 x,',[b])
else //the last byte, no comma
Result:=Result+Format('%.2 x',[b]);
if (i mod 16 = 15) then
Result:=Result+'\'+#13#10;
inc(p);
end;
end;
finally
Freemem(Buffer,Datasize);
end;
end;
end;
end;
procedure PrepareData(Reg : TRegistry; Data : TStrings );
var
Values : TStringList;
Keys : TStringList;
CurPath : String;
s : String;
i : Integer;
begin
Values := TStringList.Create;
Keys := TStringList.Create;
Keys.Add(Reg.CurrentPath);
try
while Keys.Count>0 do
begin
if Reg.OpenKey('\'+Keys[0],Fa lse) then
begin
CurPath:=Reg.CurrentPath;
Reg.GetValueNames(Values);
Data.Add(Format('[%s\%s]', [RegistryR ootKeyName (Reg.RootK ey),CurPat h]));
for i:=0 to Values.Count-1 do
begin
if Values[i]='' then
s:='@'
else
s:='"'+Values[i]+'"';
Data.Add(Format( '%s=%s',
[s,ConvertValueToStr(Reg,V alues[i])] ));
end;
Data.Add('');
Reg.GetKeyNames(Values); //Use values as temporary storage
for i:=0 to Values.Count-1 do
Keys.Insert(1,Keys[0]+'\'+ Values[i]) ;
Values.Clear;
end;
Keys.Delete(0);
end;
finally
Keys.Free;
Values.Free;
end;
end;
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
var
Reg : TRegistry;
ExportData : TStringList;
Ok : Boolean;
begin
if FileExists(FileName) and not Overwrite then
exit;
Reg := TRegistry.Create;
ExportData := TStringList.Create;
try
Reg.RootKey:=RootKey;
if Reg.OpenKey(Key,False) then
begin
ExportData.Add(ExportHeade r);
ExportData.Add('');
PrepareData(Reg,ExportData );
Ok:=not FileExists(FileName);
if not Ok then
Ok:=DeleteFile(FileName);
if Ok then
ExportData.SaveToFile(File Name);
end;
finally
ExportData.Free;
Reg.Free;
end;
end;
initialization
Initialize;
end.
--------------------------
unit RegistryExport;
interface
uses Windows,Classes,Registry,S
const
ExportHeader = 'REGEDIT4';
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
implementation
type
TSubstitution = record
Character : char;
Substitution : String;
end;
const
SubstitutionsConst : array [1..4] of TSubstitution =
( (Character : #10; Substitution : '\n'),
(Character : #13; Substitution : '\r'),
(Character : '"'; Substitution : '\"'),
(Character : '\'; Substitution : '\\')
);
var
Substitutions : array [1..255] of String;
procedure Initialize;
var
i : Integer;
begin
for i:=low(Substitutions) to high(Substitutions) do
Substitutions[i]:='';
for i:=low(SubstitutionsConst)
Substitutions[ord(Substitu
SubstitutionsConst[i].Subs
end;
function RegistryRootKeyName ( Key : HKEY ) : string;
begin
case Key of
$80000000 : Result:='HKEY_CLASSES_ROOT
$80000001 : Result:='HKEY_CURRENT_USER
$80000002 : Result:='HKEY_LOCAL_MACHIN
$80000003 : Result:='HKEY_USERS';
$80000004 : Result:='HKEY_PERFORMANCE_
$80000005 : Result:='HKEY_CURRENT_CONF
$80000006 : Result:='HKEY_DYN_DATA';
else
Result:='';
end;
end;
function NormalizeString( s : String ) : String;
var
i : Integer;
subst : String;
begin
SetLength(Result,Length(s)
Result:='';
for i:=1 to Length(s) do
begin
subst:=Substitutions[ord(s
if subst<>'' then
Result:=Result+subst
else
Result:=Result+s[i];
end;
end;
function ConvertValueToStr(Reg : TRegistry; ValueName : String) : String;
var
DataType : TRegDataType;
DataSize : Integer;
Buffer : pointer;
p : ^byte;
b : byte;
i : Integer;
begin
DataType:=Reg.GetDataType(
case DataType of
rdString,
rdExpandString :
Result := '"'+NormalizeString(Reg.Re
rdInteger : Result := Format('dword:%.8x',[Reg.R
rdBinary : begin
DataSize := Reg.GetDataSize(ValueName)
GetMem(Buffer,Datasize);
try
if Reg.ReadBinaryData(ValueNa
begin
Result:='hex:';
p:=Buffer;
for i:=0 to Datasize-1 do
begin
b:=p^;
if i<Datasize-1 then
Result:=Result+Format('%.2
else //the last byte, no comma
Result:=Result+Format('%.2
if (i mod 16 = 15) then
Result:=Result+'\'+#13#10;
inc(p);
end;
end;
finally
Freemem(Buffer,Datasize);
end;
end;
end;
end;
procedure PrepareData(Reg : TRegistry; Data : TStrings );
var
Values : TStringList;
Keys : TStringList;
CurPath : String;
s : String;
i : Integer;
begin
Values := TStringList.Create;
Keys := TStringList.Create;
Keys.Add(Reg.CurrentPath);
try
while Keys.Count>0 do
begin
if Reg.OpenKey('\'+Keys[0],Fa
begin
CurPath:=Reg.CurrentPath;
Reg.GetValueNames(Values);
Data.Add(Format('[%s\%s]',
for i:=0 to Values.Count-1 do
begin
if Values[i]='' then
s:='@'
else
s:='"'+Values[i]+'"';
Data.Add(Format( '%s=%s',
[s,ConvertValueToStr(Reg,V
end;
Data.Add('');
Reg.GetKeyNames(Values); //Use values as temporary storage
for i:=0 to Values.Count-1 do
Keys.Insert(1,Keys[0]+'\'+
Values.Clear;
end;
Keys.Delete(0);
end;
finally
Keys.Free;
Values.Free;
end;
end;
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
var
Reg : TRegistry;
ExportData : TStringList;
Ok : Boolean;
begin
if FileExists(FileName) and not Overwrite then
exit;
Reg := TRegistry.Create;
ExportData := TStringList.Create;
try
Reg.RootKey:=RootKey;
if Reg.OpenKey(Key,False) then
begin
ExportData.Add(ExportHeade
ExportData.Add('');
PrepareData(Reg,ExportData
Ok:=not FileExists(FileName);
if not Ok then
Ok:=DeleteFile(FileName);
if Ok then
ExportData.SaveToFile(File
end;
finally
ExportData.Free;
Reg.Free;
end;
end;
initialization
Initialize;
end.
ASKER
how do i use the whole chunk of stuff ?
Here is the code I used to export registry data in "Registry 4" format. It gives the same result as Registry Editor's export command.
-------------------------- ---------- ---------
unit RegistryExport;
interface
uses Windows,Classes,Registry,S ysUtils;
const
ExportHeader = 'REGEDIT4';
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
implementation
type
TSubstitution = record
Character : char;
Substitution : String;
end;
const
SubstitutionsConst : array [1..4] of TSubstitution =
( (Character : #10; Substitution : '\n'),
(Character : #13; Substitution : '\r'),
(Character : '"'; Substitution : '\"'),
(Character : '\'; Substitution : '\\')
);
var
Substitutions : array [1..255] of String;
procedure Initialize;
var
i : Integer;
begin
for i:=low(Substitutions) to high(Substitutions) do
Substitutions[i]:='';
for i:=low(SubstitutionsConst) to high(SubstitutionsConst) do
Substitutions[ord(Substitu tionsConst [i].Charac ter)]:=
SubstitutionsConst[i].Subs titution;
end;
function RegistryRootKeyName ( Key : HKEY ) : string;
begin
case Key of
$80000000 : Result:='HKEY_CLASSES_ROOT ';
$80000001 : Result:='HKEY_CURRENT_USER ';
$80000002 : Result:='HKEY_LOCAL_MACHIN E';
$80000003 : Result:='HKEY_USERS';
$80000004 : Result:='HKEY_PERFORMANCE_ DATA';
$80000005 : Result:='HKEY_CURRENT_CONF IG';
$80000006 : Result:='HKEY_DYN_DATA';
else
Result:='';
end;
end;
function NormalizeString( s : String ) : String;
var
i : Integer;
subst : String;
begin
SetLength(Result,Length(s) ); //Try to minimize reallocations
Result:='';
for i:=1 to Length(s) do
begin
subst:=Substitutions[ord(s [i])];
if subst<>'' then
Result:=Result+subst
else
Result:=Result+s[i];
end;
end;
function ConvertValueToStr(Reg : TRegistry; ValueName : String) : String;
var
DataType : TRegDataType;
DataSize : Integer;
Buffer : pointer;
p : ^byte;
b : byte;
i : Integer;
begin
DataType:=Reg.GetDataType( ValueName) ;
case DataType of
rdString,
rdExpandString :
Result := '"'+NormalizeString(Reg.Re adString(V alueName)) +'"';
rdInteger : Result := Format('dword:%.8x',[Reg.R eadInteger (ValueName )]);
rdBinary : begin
DataSize := Reg.GetDataSize(ValueName) ;
GetMem(Buffer,Datasize);
try
if Reg.ReadBinaryData(ValueNa me,Buffer^ ,Datasize) =Datasize then
begin
Result:='hex:';
p:=Buffer;
for i:=0 to Datasize-1 do
begin
b:=p^;
if i<Datasize-1 then
Result:=Result+Format('%.2 x,',[b])
else //the last byte, no comma
Result:=Result+Format('%.2 x',[b]);
if (i mod 16 = 15) then
Result:=Result+'\'+#13#10;
inc(p);
end;
end;
finally
Freemem(Buffer,Datasize);
end;
end;
end;
end;
procedure PrepareData(Reg : TRegistry; Data : TStrings );
var
Values : TStringList;
Keys : TStringList;
CurPath : String;
s : String;
i : Integer;
begin
Values := TStringList.Create;
Keys := TStringList.Create;
Keys.Add(Reg.CurrentPath);
try
while Keys.Count>0 do
begin
if Reg.OpenKey('\'+Keys[0],Fa lse) then
begin
CurPath:=Reg.CurrentPath;
Reg.GetValueNames(Values);
Data.Add(Format('[%s\%s]', [RegistryR ootKeyName (Reg.RootK ey),CurPat h]));
for i:=0 to Values.Count-1 do
begin
if Values[i]='' then
s:='@'
else
s:='"'+Values[i]+'"';
Data.Add(Format( '%s=%s',
[s,ConvertValueToStr(Reg,V alues[i])] ));
end;
Data.Add('');
Reg.GetKeyNames(Values); //Use values as temporary storage
for i:=0 to Values.Count-1 do
Keys.Insert(1,Keys[0]+'\'+ Values[i]) ;
Values.Clear;
end;
Keys.Delete(0);
end;
finally
Keys.Free;
Values.Free;
end;
end;
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
var
Reg : TRegistry;
ExportData : TStringList;
Ok : Boolean;
begin
if FileExists(FileName) and not Overwrite then
exit;
Reg := TRegistry.Create;
ExportData := TStringList.Create;
try
Reg.RootKey:=RootKey;
if Reg.OpenKey(Key,False) then
begin
ExportData.Add(ExportHeade r);
ExportData.Add('');
PrepareData(Reg,ExportData );
Ok:=not FileExists(FileName);
if not Ok then
Ok:=DeleteFile(FileName);
if Ok then
ExportData.SaveToFile(File Name);
end;
finally
ExportData.Free;
Reg.Free;
end;
end;
initialization
Initialize;
end.
--------------------------
unit RegistryExport;
interface
uses Windows,Classes,Registry,S
const
ExportHeader = 'REGEDIT4';
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
implementation
type
TSubstitution = record
Character : char;
Substitution : String;
end;
const
SubstitutionsConst : array [1..4] of TSubstitution =
( (Character : #10; Substitution : '\n'),
(Character : #13; Substitution : '\r'),
(Character : '"'; Substitution : '\"'),
(Character : '\'; Substitution : '\\')
);
var
Substitutions : array [1..255] of String;
procedure Initialize;
var
i : Integer;
begin
for i:=low(Substitutions) to high(Substitutions) do
Substitutions[i]:='';
for i:=low(SubstitutionsConst)
Substitutions[ord(Substitu
SubstitutionsConst[i].Subs
end;
function RegistryRootKeyName ( Key : HKEY ) : string;
begin
case Key of
$80000000 : Result:='HKEY_CLASSES_ROOT
$80000001 : Result:='HKEY_CURRENT_USER
$80000002 : Result:='HKEY_LOCAL_MACHIN
$80000003 : Result:='HKEY_USERS';
$80000004 : Result:='HKEY_PERFORMANCE_
$80000005 : Result:='HKEY_CURRENT_CONF
$80000006 : Result:='HKEY_DYN_DATA';
else
Result:='';
end;
end;
function NormalizeString( s : String ) : String;
var
i : Integer;
subst : String;
begin
SetLength(Result,Length(s)
Result:='';
for i:=1 to Length(s) do
begin
subst:=Substitutions[ord(s
if subst<>'' then
Result:=Result+subst
else
Result:=Result+s[i];
end;
end;
function ConvertValueToStr(Reg : TRegistry; ValueName : String) : String;
var
DataType : TRegDataType;
DataSize : Integer;
Buffer : pointer;
p : ^byte;
b : byte;
i : Integer;
begin
DataType:=Reg.GetDataType(
case DataType of
rdString,
rdExpandString :
Result := '"'+NormalizeString(Reg.Re
rdInteger : Result := Format('dword:%.8x',[Reg.R
rdBinary : begin
DataSize := Reg.GetDataSize(ValueName)
GetMem(Buffer,Datasize);
try
if Reg.ReadBinaryData(ValueNa
begin
Result:='hex:';
p:=Buffer;
for i:=0 to Datasize-1 do
begin
b:=p^;
if i<Datasize-1 then
Result:=Result+Format('%.2
else //the last byte, no comma
Result:=Result+Format('%.2
if (i mod 16 = 15) then
Result:=Result+'\'+#13#10;
inc(p);
end;
end;
finally
Freemem(Buffer,Datasize);
end;
end;
end;
end;
procedure PrepareData(Reg : TRegistry; Data : TStrings );
var
Values : TStringList;
Keys : TStringList;
CurPath : String;
s : String;
i : Integer;
begin
Values := TStringList.Create;
Keys := TStringList.Create;
Keys.Add(Reg.CurrentPath);
try
while Keys.Count>0 do
begin
if Reg.OpenKey('\'+Keys[0],Fa
begin
CurPath:=Reg.CurrentPath;
Reg.GetValueNames(Values);
Data.Add(Format('[%s\%s]',
for i:=0 to Values.Count-1 do
begin
if Values[i]='' then
s:='@'
else
s:='"'+Values[i]+'"';
Data.Add(Format( '%s=%s',
[s,ConvertValueToStr(Reg,V
end;
Data.Add('');
Reg.GetKeyNames(Values); //Use values as temporary storage
for i:=0 to Values.Count-1 do
Keys.Insert(1,Keys[0]+'\'+
Values.Clear;
end;
Keys.Delete(0);
end;
finally
Keys.Free;
Values.Free;
end;
end;
procedure ExportKey ( RootKey : HKEY; Key : String;
FileName : String; Overwrite : Boolean );
var
Reg : TRegistry;
ExportData : TStringList;
Ok : Boolean;
begin
if FileExists(FileName) and not Overwrite then
exit;
Reg := TRegistry.Create;
ExportData := TStringList.Create;
try
Reg.RootKey:=RootKey;
if Reg.OpenKey(Key,False) then
begin
ExportData.Add(ExportHeade
ExportData.Add('');
PrepareData(Reg,ExportData
Ok:=not FileExists(FileName);
if not Ok then
Ok:=DeleteFile(FileName);
if Ok then
ExportData.SaveToFile(File
end;
finally
ExportData.Free;
Reg.Free;
end;
end;
initialization
Initialize;
end.
Oops... sorry for the duplicate.
First, save all the code above into separate unit called RegistryExport.pas
After that, if you want to export HKEY_LOCAL_MACHINE\Softwar e\Microsof t registry key including all subkeys just write
ExportKey(HKEY_LOCAL_MACHI NE,'\Softw are\Micros oft',True) ;
First, save all the code above into separate unit called RegistryExport.pas
After that, if you want to export HKEY_LOCAL_MACHINE\Softwar
ExportKey(HKEY_LOCAL_MACHI
My mistake, forgot the filename :
ExportKey(HKEY_LOCAL_MACHI NE,'\Softw are\Micros oft','C:\D ata.REG',T rue);
ExportKey(HKEY_LOCAL_MACHI
ASKER
can u send me the .pas file ? send it to rifei812hotmail.com
ASKER
i have receive the file but when i enter
ExportKey(HKEY_LOCAL_MACHI NE,'\Softw are\Micros oft','C:\D ata.REG',T rue);
i get an error that says 'Undeclared identifier ExportKey'
i have added the .pas to my project
ExportKey(HKEY_LOCAL_MACHI
i get an error that says 'Undeclared identifier ExportKey'
i have added the .pas to my project
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how do i import the data in c:\data.reg back into the registry if i need to do so ?
I don't have code for this. You can use regedit.exe.
example:
var
R: TRegistry;
begin
R:=TRegistry.Create;
R.RootKey:=HKEY_LOCAL_MACH
R.SaveKey('\Software\C-Cub
R.Free;
end;