一个关于注册表的问题,请高手指教(100分)

  • 主题发起人 主题发起人 zding
  • 开始时间 开始时间
Z

zding

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位仁兄,怎么在我的程序中实现导出注册表的功能?
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=0602724
htw (2001-8-21 9:49:00)
--------------------------------------------------------------
1.调用regedit uses shellapi
shellexecute(handle,'open','regedit.exe',' /e YourWantExportKey YourBackUpFilename','',sw_hide)

2.用windoews API函数RegSaveKey或RegRestoreKey

The RegSaveKey function saves the specified key and all of its subkeys
and values to a new file.

LONG RegSaveKey(
HKEY hKey, // handle of key where save begins
LPCTSTR lpFile, // address of filename to save to
LPSECURITY_ATTRIBUTES lpSecurityAttributes // address of security structure
);

The RegRestoreKey function reads the registry information in a specified file
and copies it over the specified key. This registry information may be in the
form of a key and multiple levels of subkeys.

LONG RegRestoreKey(
HKEY hKey, // handle of key where restore begins
LPCTSTR lpFile, // address of filename containing saved tree
DWORD dwFlags // optional flags
);

下面是代码
procedure TFormMain.ButtonExportClick(Sender: TObject);
var
RootKey,phKey: hKey;
KeyName,sKeyFileName: String;
FileName: array [0..255] of char;
begin
RootKey := HKEY_CURRENT_USER;
KeyName := 'software/mysoft/abc';
RegOpenKeyEx(RootKey, PChar(KeyName), 0, KEY_ALL_ACCESS, phKey);
sKeyFileName := 'c:/tempReg';
StrPCopy(FileName,sKeyFileName); //用pchar也行
if RegSaveKey(phKey, FileName, nil)= 0 then
ShowMessage('注册表分支已被导出。')
else
ShowMessage('注册表分支没有被导出。');
RegCloseKey(phKey);
end;

procedure TFormMain.ButtonImportClick(Sender: TObject);
var //从文件导回到注册表
RootKey,phKey: hKey;
KeyName,sKeyFileName: String;
FileName: array [0..255] of char;
begin
RootKey := HKEY_CURRENT_USER;
KeyName := 'software/mySoft/abc';
RegOpenKeyEx(RootKey, PChar(KeyName), 0, KEY_ALL_ACCESS, phKey);
sKeyFileName := 'c:/tempReg';
StrPCopy(FileName,sKeyFileName);
if RegRestoreKey(phKey, FileName,0)= 0 then
ShowMessage('注册表指定分支已恢复。')
else
ShowMessage('注册表指定分支没有恢复,操作失败。');
RegCloseKey(phKey);
end;


3.另外, 还可以用TRegistry的savekey和restorekey
var
reg : Tregistry;
begin
reg := Tregistry.Create;
reg.rootkey := HKEY_CURRENT_USER;
reg.Savekey('/Software/NetAnts','d:/test1/NetAnts');
end;

http://www.delphibbs.com/delphibbs/dispq.asp?lid=0781322
djw (2001-12-12 8:55:00)
-----------------------------------------------------------------
给你个控件:

unit regexpo; interface uses winprocs,wintypes,registry,classes,sysutils;
{$I-}
{$LONGSTRINGS ON}
{
Regexpo
Function : This unit allows you to backup a branch of the registry into a *.REG file,
that is compatible with "regedit".
Double-clicking such a file in the explorer will import it.
Example: ExportRegistryBranch (hkey_local_machine,'SOFTWARE/Borland/Delphi','A:/DELPHI.REG')
}
Procedure ExportRegistryBranch (rootsection : Integer; regroot:String; filename:String);
implementation
Function dblBackSlash(t:string):string;
var k:longint;
begin
result:=t; {Strings are not allowed to have}
for k:=length(t) downto 1 do {single backslashes}
if result[k]='/' then insert('/',result,k);
end;

Procedure ExportRegistryBranch (rootsection : Integer;
regroot:String;
filename:String);

var reg:tregistry;
f:textfile;
p:PCHAR;

Procedure ProcessBranch(root:string); {recursive sub-procedure}
var values,keys:tstringlist; i,j,k:longint;
s,t:string; {longstrings are on the heap, not on the stack!}
begin
Writeln(f); {write blank line}
case rootsection of
HKEY_CLASSES_ROOT :s:='HKEY_CLASSES_ROOT';
HKEY_CURRENT_USER :s:='HKEY_CURRENT_USER';
HKEY_LOCAL_MACHINE :s:='HKEY_LOCAL_MACHINE';
HKEY_USERS :s:='HKEY_USERS';
HKEY_PERFORMANCE_DATA:s:='HKEY_PERFORMANCE_DATA';
HKEY_CURRENT_CONFIG :s:='HKEY_CURRENT_CONFIG';
HKEY_DYN_DATA :s:='HKEY_DYN_DATA';
end;
Writeln(f,'['+s+'/'+root+']'); {write section name in brackets}


reg.OpenKey(root,false);
values:=tstringlist.create;
keys:=tstringlist.create;
reg.getvaluenames (values); {get all value names}
reg.getkeynames (keys); {get all sub-branches}

for i:=0 to values.count-1 do {write all the values first}
begin
s:=values;
t:=s; {s=value name}
if s=''
then s:='@' {empty means "default value", write as @}
else s:='"' + s + '"'; {else put in quotes}
write(f,dblbackslash(s)+'='); {write the name of the key to the file}

Case reg.Getdatatype(t) of {What type of data is it?}

rdString,rdExpandString: {String-type}
Writeln(f,'"' + dblbackslash(reg.readstring(t)+'"'));

rdInteger : {32-bit unsigned long integer}
Writeln(f,'dword:' + inttohex(reg.readinteger(t),8));


{write an array of hex bytes if data is "binary." Perform a line feed
after approx. 25 numbers so the line length stays within limits}

rdBinary :
begin
write(f,'hex:');
j:=reg.getdatasize(t); {determine size}
getmem(p,j); {Allocate memory}
reg.ReadBinaryData(t,p^,J); {read in the data, treat as pchar}
for k:=0 to j-1 do
begin
Write(f,inttohex(byte(p[k]),2)); {Write byte as hex}
if k<>j-1 then {not yet last byte?}
begin
write(f,','); {then write Comma}
if (k>0) and ((k mod 25)=0) {line too long?}
then writeln(f,'/'); {then write Backslash + lf}
end; {if}
end; {for}
freemem(p,j); {free the memory}
writeln(f); {Linefeed}
end;
ELSE writeln(f,'""'); {write an empty string if datatype illegal/unknown}
end;{case}
end; {for}

reg.closekey;

{value names all done, no longer needed}
values.free;

{Now al values are written, we process all subkeys}
{Perform this process RECURSIVELY...}
for i:=0 to keys.count-1 do
ProcessBranch(root+'/'+keys);

keys.free; {this branch is ready}
end;

begin
if regroot[length(regroot)]='/' then {No trailing backslash}
setlength(regroot,length(regroot)-1);
Assignfile(f,filename); {create a text file}
rewrite(f);
IF ioresult<>0 then EXIT;
Writeln(f,'REGEDIT4'); {"magic key" for regedit}

reg:=tregistry.create;
try
reg.rootkey:=rootsection;
ProcessBranch(regroot); {Call the function that writes the branch and all subbranches}
finally
reg.free; {ready}
close(f);
end;
end;
end.

 
后退
顶部