You may use ShellExecute to run DOS command in Delphi, but it causes lots of run-time error in Windows 10+ based on my experience, because system requires to have some sort of permission. Custom function RunAsAdminAndWaitForCompletion() will help you to clear that issue.
function RunAsAdminAndWaitForCompletion(IsAdmin: Boolean; hWnd: HWND; filename: string; Parameters: string): Boolean;
{
See Step 3: Redesign for UAC Compatibility (UAC)
http://msdn.microsoft.com/en-us/library/bb756922.aspx
}
var
sei: TShellExecuteInfo;
ExitCode: DWORD;
begin
ZeroMemory(@sei, SizeOf(sei));
sei.cbSize := SizeOf(TShellExecuteInfo);
sei.Wnd := hwnd;
sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI or SEE_MASK_NOCLOSEPROCESS;
if IsAdmin=True then sei.lpVerb := PChar('runas');
sei.lpFile := PChar(Filename); // PAnsiChar;
if parameters <> '' then
sei.lpParameters := PChar(parameters); // PAnsiChar;
sei.nShow := SW_SHOWNORMAL; //Integer;
if ShellExecuteEx(@sei) then begin
repeat
Application.ProcessMessages;
GetExitCodeProcess(sei.hProcess, ExitCode) ;
until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
end;
end;
Below is the sample code to run above
// Run DOS command in admin permission RunAsAdminAndWaitForCompletion( True, HWND(Handle), 'notepad.exe', ''); // Run DOS command without admin permission RunAsAdminAndWaitForCompletion( False, HWND(Handle), 'notepad.exe', '');