Execute Application via CreateProcess - Delphi

DarkCoderSc
Jean-Pierre LESUEUR
uses
System.SysUtils, Winapi.Windows;
// ...
procedure RunApplication(const AProgram : String; const AProgramArgument : String = ''; const AVisible : Boolean = True);
begin
var AStartupInfo : TStartupInfoW;
var AProcessInfo : TProcessInformation;
ZeroMemory(@AStartupInfo, SizeOf(TStartupInfoW));
AStartupInfo.cb := SizeOf(TStartupInfoW);
var ACommandLine := AProgram;
if not String.IsNullOrWhiteSpace(AProgramArgument) then
ACommandLine := Format('%s "%s"', [
AProgram,
AProgramArgument
]);
UniqueString(ACommandLine);
var AFlags := 0;
if not AVisible then begin
AFlags := AFlags or STARTF_USESHOWWINDOW;
AStartupInfo.wShowWindow := SW_HIDE;
end;
if not CreateProcessW(
nil,
PWideChar(ACommandLine),
nil,
nil,
False,
AFlags,
nil,
nil,
AStartupInfo,
AProcessInfo
) then
raise EWindowsException.Create('CreateProcessW');
WriteLn(Format('Process Created: [pid:%d(0x%p)]', [
AProcessInfo.dwProcessId,
Pointer(AProcessInfo.dwProcessId)
]));
end;
// ...
RunApplication('notepad.exe');
// ...
RunApplication('notepad.exe', 'c:\Temp\file1.txt');
// ...
RunApplication('notepad.exe', '', False);
// ...
Creating and researching code snippets takes time and effort. You’re welcome to share them through your own platforms, but please don’t forget to credit the original author, here: Jean-Pierre LESUEUR.
Depends On
Implemented By Technique
Featured Windows API
Created
May 9, 2025
Last Revised
May 14, 2025