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 Answers
16
votes
16
votes
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=
HKLM
. Otherwise you won't be able to import it. – David Heffernan