Exporting registry keys as text



I can save a key (contents) with (as I remember) the SaveKey method of Tregistry, which is a wrapper around RegSaveKey.  But this saves the contents as binary... I want to save as text in the same way as regedit does when asked to export a key branch.

Are there any API calls I can use to export a registry branch as text?

Or do I have to shell out to regedit ? Is regedit /e applicable across all OS's .. 95,98, NT, XP, 2000, ME?



Mutley2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andi4883Commented:
why don't you try to write it manually try this

procedure write;
var
   data_key : TRegDataInfo;
   i,angka2 : integer;
   angka : DWORD;
   data : array of byte;
   st1 : string;
   reg1 : TRegistry;
   File_T : textfile;
begin
  AssignFile (File_T,Edit1.Text);
  Rewrite(File_T);
  reg1 := TRegistry.Create;
  str.Clear;
  reg1.RootKey := HKEY_LOCAL_MACHINE;
  if reg1.OpenKeyReadOnly('Software\Microsoft\Internet Explorer\Main') = True then begin
    Writeln(File_T,'');
    Writeln(File_T,'[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]');
    reg1.GetValueNames(str);
    for i:=0 to str.count-1 do begin
      reg1.GetDataInfo(str[i],data_key);

      if str[i] = '' then writeln(File_T,'"default"')
      else write(File_T,'"'+str[i]+'"=');

      case data_key.RegData of
        rdUnknown : begin
                    writeln(File_T,'unknown');
                    end;

        rdString,rdExpandString  : begin
                                   writeln(File_T,'"'+reg1.ReadString(str[i])+'"');
                                   end;

        rdInteger : begin
                    write(File_T,'DWORD:');
                    writeln(File_T,(Format('%d',[reg1.ReadInteger(str[i])])));
                    end;

        rdBinary : begin
                   write(File_T,'DWORD:');
                   angka := reg1.GetDataSize(str[i]);
                   if angka > 0 then begin
                         SetLength(Data, angka);
                         angka2 := reg1.ReadBinaryData(str[i],Data[0],angka);
                         st1 := '';
                         for angka := 0 to angka2-1 do
                         begin
                           if Data[angka] < 16 then
                             st1 := st1 + Format('0%x ',[Data[angka]])
                           else
                             st1 := st1 + Format('%x ',[Data[angka]]);
                         end;
                         writeln(File_T,st1);
                     end;
                   end;
        end;
    end;
  end;
  reg1.CloseKey;
  reg1.free
  CloseFile(File_T);
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mutley2003Author Commented:

The manual approach *might* be OK
but
  I decided not to use it because of difficulty in being certain exactly what
  Microsoft has done .. there appear to be issues with

  hex vs hex(2) ..

  A somewhat arbitrary character limit on the first line of hex dumps .. variable
  numbers of elements per line

  embedded quotes
 
  dk what the @ is
 
  dk what Microsft does with rdUnknown type data


Some examples below.

I don't believe I could be sure of reproducing the format of a .reg file EXACTLY .. or at least not without a LOT of testing .. unless there are API calls to do what I want. And if that reg file is not exactly what is needed by regedit, then reimporting by regedit could cause problems.

So, at present, I think it is just as easy to spawn a process that calls regedit /e


I will award the points to Andi4483 unless someone comes up with a better answer in the next few days

thanks



  (*
  "DigitalProductId"=hex:a4,00,00,00,03,00,00,00,35,30,38,30,37,2d,30,31,39,2d,\
  34,38,37,31,37,36,32,2d,33,31,39,30,34,00,02,00,00,00,37,33,30,2d,30,30,38,\
  34,38,00,00,00,00,00,00,00,91,b3,52,82,ad,89,81,a7,28,2e,91,49,16,c6,03,00,\
  00,00,00,00,9c,86,ab,3b,41,de,04,00,01,00,00,00,00,00,00,00,00,00,00,00,00,\
  00,00,00,00,00,00,00,00,00,00,00,30,38,34,30,30,00,00,00,00,00,00,00,00,00,\
  00,00,fb,12,6d,14,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\
  00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,c9,44,1c,4d

  "UninstallString"=hex(2):4d,73,69,45,78,65,63,2e,65,78,65,20,2f,49,7b,44,39,41,\
  33,42,38,43,45,2d,30,33,33,45,2d,34,43,46,41,2d,41,39,39,30,2d,45,31,34,32,\
  44,33,35,33,44,31,37,37,7d,00

  "UninstallString"="\"C:\\WINDOWS\\UNISTB32.EXE\" /U \"E:\\lcc\\win32onlinehelp\\UNINST0.000\" \"E:\\lcc\\win32onlinehelp\\UNINST1.000\""

  @=""
  "DisplayName"="Add/Remove Pro"
  "UninstallString"="RunDll32 advpack.dll,LaunchINFSection C:\\WINDOWS\\INF\\ADRMPRO2.INF, DefaultUninstall"

  [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Controls Folder]
@=""
"Presentation Cache"=hex:84,00,00,00,01,00,00,00,00,36,46,8f,c9,2e,be,01,00,00,\
  00,00,10,e5,00,00,81,00,00,00,3e,00,00,00,43,00,00,00,43,3a,5c,50,52,4f,47,\
  52,41,4d,20,46,49,4c,45,53,5c,43,4f,4d,4d,4f,4e,20,46,49,4c,45,53,5c,53,59,\

*)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.