Work with External program or Files in Delphi

WinAPI: ShellExecute-Open an external program or file
ShellExecute(
HWnd:HWND; {specify parent window handle}
Operation: PChar; {Specify actions, such as: open, print}
FileName:PChar; {Specify the file or program to open}
Parameters: PChar; {Specify parameters for the program to be opened; if the file is opened, it should be nil}
Directory: PChar; {default directory}
ShowCmd:Integer {open option}
): HINST; {Successful execution will return the application handle; if this value <= 32, it means execution error}
//The possible errors of the return value are:
{Insufficient memory}
ERROR_FILE_NOT_FOUND = 2; {File name error}
ERROR_PATH_NOT_FOUND = 3; {path name error}
ERROR_BAD_FORMAT = 11; {EXE file is invalid}
SE_ERR_SHARE = 26; {Sharing error occurred}
SE_ERR_ASSOCINCOMPLETE = 27; {File name is incomplete or invalid}
SE_ERR_DDETIMEOUT = 28; {timeout}
SE_ERR_DDEFAIL = 29; {DDE transaction failed}
SE_ERR_DDEBUSY = 30; {Processing other DDE transactions and cannot complete the DDE transaction}
SE_ERR_NOASSOC = 31; {There is no associated application}
//ShowCmd parameter optional values:
SW_HIDE = 0; {hide}
SW_SHOWNORMAL = 1; {Display with the nearest size and position, activate}
SW_NORMAL = 1; {same as SW_SHOWNORMAL}
SW_SHOWMINIMIZED = 2; {minimize, activate}
SW_SHOWMAXIMIZED = 3; {maximize, activate}
SW_MAXIMIZE = 3; {same as SW_SHOWMAXIMIZED}
SW_SHOWNOACTIVATE = 4; {Display with the latest size and position, not activated}
SW_SHOW = 5; {same as SW_SHOWNORMAL}
SW_MINIMIZE = 6; {Minimize, not activate}
SW_SHOWMINNOACTIVE = 7; {same as SW_MINIMIZE}
SW_SHOWNA = 8; {same as SW_SHOWNOACTIVATE}
SW_RESTORE = 9; {same as SW_SHOWNORMAL}
SW_SHOWDEFAULT = 10; {same as SW_SHOWNORMAL}
SW_MAX = 10; {same as SW_SHOWNORMAL}
——————————————————————————–
// Give examples to illustrate more problems (don’t forget uses ShellAPI;):
{For example, open a file with Notepad}
begin
ShellExecute(Handle,’open’,’notepad.exe’,’C:\WINDOWS\DelphiFan.Txt’, nil, SW_SHOWNORMAL);
end;
{The first parameter is used as the parent window of the error prompt window, it cannot be nil, it can be 0 (that is, the desktop window)}
begin
ShellExecute(0,’open’,’notepad.exe’,’C:\WINDOWS\DelphiFan.Txt’, nil, SW_SHOWNORMAL);
end;
{If the second parameter is nil, it will be open by default}
begin
ShellExecute(0, nil,’notepad.exe’,’C:\WINDOWS\DelphiFan.Txt’, nil, SW_SHOWNORMAL);
end;
{File path can be placed in parameter five}
begin
ShellExecute(0, nil,’notepad.exe’,’DelphiFan.Txt’,’C:\WINDOWS’, SW_SHOWNORMAL);
end;
{Specify parameter three directly as the file to be opened, and the file will be opened with the corresponding default program; the number of times parameter four should be nil}
begin
ShellExecute(0, nil,’DelphiFan.Txt’, nil,’C:\WINDOWS’, SW_SHOWNORMAL);
end;
{If the file is in: program directory/current directory/System32/Windows/PATH environment variable, parameter five can also be nil}
begin
ShellExecute(0, nil,’DelphiFan.Txt’, nil, nil,SW_SHOWNORMAL);
end;
{If parameter three is a file, you can use the parameter two command to print}
begin
ShellExecute(0,’print’,’DelphiFan.Txt’, nil,nil, 1);
end;
{Open webpage with IE}
begin
ShellExecute(Handle,’open’,’IExplore.EXE’,’about:blank’, nil, SW_SHOWNORMAL);
end;
{Open webpage with Firefox}
begin
ShellExecute(Handle,’open’,’firefox.exe’,’about:blank’, nil, SW_SHOWNORMAL);
end;
{Open webpage with default browser}
begin
ShellExecute(Handle,’open’,’Explorer.exe’,’about:blank’, nil, SW_SHOWNORMAL);
end;
{Still use the default browser to open the webpage}
begin
ShellExecute(0, nil,’https://www.delphifan.com’,nil, nil, 1);
end;
// (waiting to execute an external program and then execute another program)
unit Unit1;
interface
uses
type
var
implementation
uses ShellAPI;
{$R *.dfm}
function ExecAppWait(AppName, Params: string): Boolean;
var
begin
end;
procedure TForm1.Button1Click(Sender: TObject);
const
var
begin
end;
end.