2014-05-07 81 views
0

編輯:好像它,如果我跑我的應用程序作爲管理員德爾福RegExpo不正確的數據

工作正常,我用以下

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] 

上任何想法我丟失或有人可以推薦一個更好的方式來導出註冊表分支?

+0

可能會更容易掏出來做到這一點 –

+0

@大衛我有工作,但希望有一個功能做在代碼中不使用ShellAPI的 – ondrovic

+0

你有沒有嘗試過使用調試器?你知道如何使用它嗎?由於您編寫了代碼,因此您必須對它的作用有所期待。做一些調試器來確定代碼與期望不同的地方。 –

回答

1

錯誤在於代碼請求完全訪問註冊表。相反,它需要請求讀訪問:到`reg`

reg := TRegistry.Create(KEY_READ);