Delphi RegExpo错误数据

Delphi RegExpo错误数据,delphi,registry,Delphi,Registry,编辑:如果我以管理员的身份运行我的应用程序,看起来效果不错 我正在使用下面的 unit RegExpo; interface uses Windows, Registry, Classes, SysUtils; {$I-} {$LONGSTRINGS ON} { Example: ExportRegistryBranch(HKEY_LOCAL_MACHINE,'SOFTWARE\Borland\Delphi','c:\DELPHI.REG') } procedure Expo

编辑:如果我以管理员的身份运行我的应用程序,看起来效果不错

我正在使用下面的

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;

关于我遗漏了什么,或者有人能推荐一种更好的方法来导出注册表分支吗?

错误在于代码请求对注册表的完全访问。相反,它需要请求读取访问:

reg := TRegistry.Create(KEY_READ);
在打开新钥匙之前,“reg.CloseKey;”的关闭丢失

Writeln(f, '[' + s + '\' + root + ']'); { write section name in brackets }

// open in readmode
reg.CloseKey;
reg.Access := KEY_READ;
reg.OpenKey(root, False);

可能更容易进行
reg
操作this@David我有这样的功能,但希望有一个函数在代码中完成它,而不是使用ShellApi。您尝试过使用调试器吗?你知道怎么用吗?既然你写了代码,你就必须对它的功能有所期待。做一些调试器来找出代码偏离您期望的地方。诚实地说,您仍然在学习Delphi和整个过程,有点像是一次火试:-)所以对于这一次,不,我没有使用调试器yetWell,是时候让您学习如何调试了。
Writeln(f, '[' + s + '\' + root + ']'); { write section name in brackets }

// open in readmode
reg.CloseKey;
reg.Access := KEY_READ;
reg.OpenKey(root, False);