?
Solved

exporting parts of the registry

Posted on 1999-12-17
2
Medium Priority
?
242 Views
Last Modified: 2010-04-06
I want to be able to export a key i specify to a .reg file speicified by the user, later to be imported into the registry by calling the fileexecute procedure. What I need is the exact coding to do this.

take care

hatecapletters
0
Comment
Question by:hatecapletters
2 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 300 total points
ID: 2290929
hi
try this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,Registry;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Procedure ExportRegistryBranch (rootsection : Integer; regroot:String;
filename:String);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


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 tfORM1.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[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}

       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[i]);

       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}
    end;
    CLOSEFILE(F);
    end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ExportRegistryBranch(hkey_local_machine,'SOFTWARE\Borland\Delphi','c:\delphireg.reg')


end;

procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.lines.loadfromfile('c:\delphireg.reg');
end;

end.

ps always backup before testing registry stuff (although ive tested and it seems quite safe :-)
Regards Barry
0
 

Author Comment

by:hatecapletters
ID: 2292519
works like a charm, except that i had to escape all quotes in the stringvalues  ;-)

hatecapletters
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question