8
votes

In my program I check for a registry key at startup and if somehow it does not exist I execute the reg file located in the application folder wit the help of ShellExecute command. How can I avoid getting confimation messages when executing this command. Is there a way to do that or as per security reasons it's not possible?

4
Don't execute the reg file. Use the registry API directly.Preet Sangha
I'm sure the advice you gave is valuable but could you please explain as to why I shouldn't do it this way? And one more thing, the registry file I want to execute contains tons of entries which were automatically inserted by a data aware grid components. If I try to write it manually wouldn't it mean a waste of time?Mikayil Abdullayev
For example, how will you deal with the registry redirector on 64 bit systems?David Heffernan
I trust this reg key is not in HKLM. Otherwise you won't be able to import it.David Heffernan
also, you will need admin rights to run Regedit in UAC environment. UAC warning dialog will popup for sure, asking the user to confirm.kobik

4 Answers

16
votes

Use the /s command-line switch. (see http://support.microsoft.com/kb/82821)

16
votes

It's possible. Two methods are:

  1. %windir%\system32\regedit.exe /s file.reg
  2. %windir%\system32\reg.exe import file.reg

Either will silently import file.reg into the registry.

3
votes

try this for importing the *.reg file,

  procedure ImportRegistry;
       var
        strProgram :String ;
        strCommand :String ;
        fileOne   :String ;
      begin

fileOne:=ExtractFilePath(Application.ExeName)+  'my_Resources\Default.reg';
strProgram := 'REGEDIT' ;
strProgram := strProgram + #0 ;
strCommand := '/SC /C ' + ExtractShortPathName(fileOne) ;
strCommand := strCommand + #0 ;

if ShellExecute(0,nil,@strProgram[1],@strCommand[1],nil,SW_HIDE) <= 32 then
  begin
        ShowMessage(SysErrorMessage(GetLastError)) ; //if there is any error in importing
  end;


end;

Also you can try this link unitEXRegistry.pas

This unitEXRegistry.pas unit has very useful functions to export registry file and also import silently the exported *.reg file

       procedure exportREgis;
        var
         texpr : TExRegistry;
        begin
         texpr:=TExRegistry.Create;
         texpr.RootKey:=HKEY_CURRENT_USER;
         texpr.OpenKeyReadOnly('\MyKey');
         texpr.ExportKey (ExtractFilePath(Application.ExeName)+'ExportedReg.reg');
         texpr.Free; 
       end;

Then to import you can use(silently)

     procedure TForm1.Button1Click(Sender: TObject);
        var
         texpr : TExRegistry;
        begin
          texpr:=TExRegistry.Create;
          texpr.ImportRegFile('c:\myReg.reg');
          texpr.Free;
       end;
0
votes

Apparently there's a bug in REG IMPORT - it writes the success message to STDERR instead of STDOUT.

The following .bat code solves the problem. The success message is discarded, but the failure message is displayed.

SET RegError=%TEMP%\RegError.txt
REG IMPORT "%Settings.reg%" 2>"%RegError%" && DEL /Q "%RegError%" || @(ECHO Error importing %Settings.reg%: & TYPE "%RegError%" & PAUSE)
SET RegError=