【发布时间】:2014-05-07 12:07:59
【问题描述】:
编辑:如果我以管理员身份运行我的应用程序,它似乎可以正常工作
我正在使用以下
unit RegExpo;
interface
uses
Windows, Registry, Classes, SysUtils;
{$I-}
{$LONGSTRINGS ON}
{
Example:
ExportRegistryBranch(HKEY_LOCAL_MACHINE,'SOFTWARE\Borland\Delphi','c:\DELPHI.REG')
}
procedure ExportRegistryBranch(RootSection: DWORD; 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: DWORD; 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);
try
values := TStringList.Create;
try
keys := TStringList.Create;
try
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[i];
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}
finally
reg.CloseKey;
end;
finally
{value names all done, no longer needed}
values.Free;
end;
{Now al values are written, we process all subkeys}
{Perform this process RECURSIVELY...}
for i := 0 to keys.Count - 1 do
ProcessBranch(root + '\' + keys[i]);
finally
keys.Free; {this branch is ready}
end;
end; { ProcessBranch}
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}
WriteLn(f, 'Windows Registry Editor Version 5.00');
reg := TRegistry.Create;
try
reg.Rootkey := RootSection;
{Call the function that writes the branch and all subbranches}
ProcessBranch(Regroot);
finally
reg.Free; {ready}
CloseFile(f);
end;
end;
结束。
无论我尝试保存哪个分支,似乎我得到的唯一信息如下:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Installer\patches]
关于我缺少什么的任何想法,或者有人可以推荐一种更好的方法来导出注册表分支吗?
【问题讨论】:
-
向
reg发送此操作可能更容易 -
@David 我有这个工作,但希望有一个函数可以在代码中完成,而不是使用 shellapi
-
你试过使用调试器吗?你知道怎么用吗?由于您编写了代码,因此您必须对其所做的事情有所期望。做一些调试器来找出代码与您的期望不同的地方。
-
老实说,我还在学习 Delphi 和整个过程,有点像一场火灾 :-) 所以对于这个,我还没有使用调试器
-
好了,是时候学习调试了。