DelphiFan Magazine

Top Menu

Main Menu

  • DELPHI
  • CODE SAMPLES
  • FIREMONKEY
  • DATABASE
  • RELEASES
  • VIDEOS
  • REVIEW
  • TECH NEWS

logo

DelphiFan Magazine

  • DELPHI
  • CODE SAMPLES
  • FIREMONKEY
  • DATABASE
  • RELEASES
  • VIDEOS
  • REVIEW
  • TECH NEWS
  • How to add a server application written in UNIGUI to startup?

  • UniGUI Expiry Link Anonymizer

  • UniGUI Add CAPTCHA to the WEB project

  • UniGui. Unique hybrid of Delphi and JS

  • How to transform a blob field into string lines (varchar) in SQL Firebird?

CODE SAMPLESDELPHI
Home›CODE SAMPLES›Work with External program or Files in Delphi

Work with External program or Files in Delphi

By admin
November 26, 2020
2032
0
Share:

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
 Windows, Messages, SysUtils, Variants, Classes,Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
   Button1:TButton;
   procedureButton1Click(Sender: TObject);
 private
   { Privatedeclarations }
 public
   { Publicdeclarations }
 end;

var
 Form1: TForm1;

implementation

uses ShellAPI;  //note

{$R *.dfm}

function ExecAppWait(AppName, Params: string): Boolean;
var
 ShellExInfo: TShellExecuteInfo;
begin
 FillChar(ShellExInfo, SizeOf(ShellExInfo),0);
 with ShellExInfo do begin
   cbSize :=SizeOf(ShellExInfo);
   fMask :=see_Mask_NoCloseProcess;
   Wnd :=Application.Handle;
   lpFile :=PChar(AppName);
   lpParameters:= PChar(Params);
   nShow :=sw_ShowNormal;
 end;
 Result := ShellExecuteEx(@ShellExInfo);
 if Result then
   whileWaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUTdo
   begin
    Application.ProcessMessages;
    if Application.Terminated then Break;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const    {Continuously run the following four EXE files}
 EXEFILES : array[1..4] of string =
           (‘calc.exe’, ‘mspaint.exe’, ‘Notepad.exe’, ‘wordpad.exe’);
var
 Success: Boolean;
 InstanceID: THandle;
 I : integer;
begin
 for I := Low(EXEFILES) to High(EXEFILES)do
   begin
  Application.Minimize;
   Success :=False;
   try
     Success := ExecAppWait(EXEFILES[I], ”)
  finally
    Application.Restore;
    if not Success then
      ShowMessage(Format(‘Application %d failed: %s’, [ I, EXEFILES[I]]));
   end;
 end;
end;

end.

 

Tagsdelphidelphi open fileexternal programfileopen appopen file
Previous Article

DBGrid beautification and SQLite display problem

Next Article

Delphi calls .net WebService and parameter transfer

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0
  • 0

admin

Related articles More from author

  • DELPHIFIREMONKEY

    Setting up the IDE for your first Android Application in Delphi

    September 7, 2020
    By admin
  • CODE SAMPLESDELPHI

    Delphi View DPR file

    January 1, 2021
    By admin
  • DELPHIREVIEW

    Ultimate Robust SecureBridge SSH

    September 29, 2020
    By admin
  • CODE SAMPLESDELPHI

    Check Out 30+ Delphi Units To Take Your Development

    October 8, 2020
    By admin
  • CODE SAMPLESDELPHI

    How does Delphi handle different types of files

    November 19, 2020
    By admin
  • CODE SAMPLESDELPHIFIREMONKEY

    Delphi Android permissions

    December 4, 2020
    By admin

Leave a reply Cancel reply

You may interested

  • CODE SAMPLESDELPHIFIREMONKEY

    Copy text to clipboard in Firemonkey applications

  • CODE SAMPLESDELPHI

    Several Delphi programs for obtaining Windows system information

  • CODE SAMPLESDELPHI

    Delphi View DPR file

  • LATEST REVIEWS

  • TOP REVIEWS

Timeline

  • January 27, 2021

    How to add a server application written in UNIGUI to startup?

  • January 27, 2021

    UniGUI Expiry Link Anonymizer

  • January 26, 2021

    UniGUI Add CAPTCHA to the WEB project

  • January 26, 2021

    UniGui. Unique hybrid of Delphi and JS

  • January 13, 2021

    How to transform a blob field into string lines (varchar) in SQL Firebird?

Latest Comments

Find us on Facebook

Follow Us on Instagram

logo

Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.

About us

  • 660 Pennsylvania Avenue Southeast #100 Washington, DC 20003
  • 0123456789
  • adsdelphi@gmail.com
  • Recent

  • Popular

  • Comments

  • How to add a server application written in UNIGUI to startup?

    By admin
    January 27, 2021
  • UniGUI Expiry Link Anonymizer

    By admin
    January 27, 2021
  • UniGUI Add CAPTCHA to the WEB project

    By admin
    January 26, 2021
  • UniGui. Unique hybrid of Delphi and JS

    By admin
    January 26, 2021
  • How to add a server application written in UNIGUI to startup?

    By admin
    January 27, 2021
  • Amazing Cross Platform Email App Sample In Delphi 10.4.1 FireMonkey For Android And IOS

    By admin
    August 13, 2020
  • Critical Update Available For Delphi FireMonkey 10.4.1 On Android, IOS, OSX, Windows, And Linux

    By admin
    September 4, 2020
  • Setting up the IDE for your first Android Application in Delphi

    By admin
    September 7, 2020

Follow us

Find us on Facebook

© Copyright Delphifan Forum. All rights reserved.