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?
Asked
Active
Viewed 2.6k times
9
-
6Don't execute the reg file. Use the registry API directly. – Preet Sangha Jan 16 '12 at 08:13
-
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 Jan 16 '12 at 08:24
-
For example, how will you deal with the registry redirector on 64 bit systems? – David Heffernan Jan 16 '12 at 08:31
-
I trust this reg key is not in `HKLM`. Otherwise you won't be able to import it. – David Heffernan Jan 16 '12 at 09:06
-
1also, you will need admin rights to run Regedit in UAC environment. UAC warning dialog will popup for sure, asking the user to confirm. – kobik Jan 16 '12 at 10:57
-
@kobik that's another reason to prefer `reg import` – David Heffernan Jan 16 '12 at 11:02
-
@DavidHeffernan, `reg` looks like a designated tool for this task. but should be tested in UAC enabled OS. – kobik Jan 16 '12 at 11:27
-
Sounds like your application is setting registry keys and values that belong in an installer rather than executed from your application. This also avoids problems with UAC when calling `regedit.exe` or `reg.exe`. If you really need to do this, please use the registry API as often regedit/reg are blocked in Enterprise environments and even if they're not you have no way to check if it succeeded or do error handling (that's what API's are for). – Remko May 13 '20 at 22:47
4 Answers
17
Use the /s command-line switch.

Stephane
- 3,173
- 3
- 29
- 42
-
And how do I do it in Delphi? Do I still use ShellExecute? An example would be appreciated – Mikayil Abdullayev Jan 16 '12 at 08:30
-
4ShellExecute would work fine. Myself I would use `reg import` rather then `regedit`. – David Heffernan Jan 16 '12 at 09:06
-
2
-
1
-
I was unable to find the new location of that document and therefore will remove the link – Stephane Feb 27 '23 at 15:03
17
It's possible. Two methods are:
- %windir%\system32\regedit.exe /s file.reg
- %windir%\system32\reg.exe import file.reg
Either will silently import file.reg into the registry.

drf
- 8,461
- 32
- 50
-
this worked, except it now prompts that you have to be in Admin privileges, so just as many prompts. – BoB3K Feb 10 '21 at 19:29
-
For the 64-bit era use regedt32.exe instead, and you do need to run as elevated admin as per comment above. – Symo Jul 16 '22 at 14:08
3
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;

PresleyDias
- 3,657
- 6
- 36
- 62
0
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=

user15261314
- 11
- 1