You are on page 1of 3

Running another program with Delphi

Running another program is divided into two kinds:


1. Run a program without waiting for the program is closed
2. Program will run when other programs are closed

1. Run a program without waiting for the program is closed


By using the functions of the unit “ShellApi” named “ShellExecute”.
ShellExecute function structure:
HINSTANCE ShellExecute (
   HWND hwnd,    // handle to parent window
     LPCTSTR lpOperation,    // pointer to string that specifies operation to perform
   LPCTSTR lpFile,    // pointer to filename or folder name string
     LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters 
     LPCTSTR lpDirectory,    // pointer to string that specifies default directory
   INT nShowCmd *)     // whether file is shown when opened
    );

*) nShowCmd SW_HIDE, if we want to run the program but the program does not appear on the
screen
SW_MAXIMIZE, if we want to run the program but in a state maximize.
SW_MINIMIZE, if we want to run the program but in a state minimize.
SW_SHOW, if we want to run the program with a normal state.

*note : REMEMBER! LPCTSTR is a pointer type STRING, so to fill them we must add PCHAR (), if we
leave it out then just fill nil

Example :

Ridwan Syarif S. Cyber jurnalis™ 1


2. Program will run when other programs are closed
by creating a function named WinExecAndWait32 :

DO NOT FORGET TO ADDING SysUtils unit, ShellAPI, Windows

structure function:
function WinExecAndWait32(
FileName: string;
Visibility: Integer): Longword;
*description:
Filename folder and filename that will be called
Visibility same as the number 1 nShowCmd
Longword results from the calling program

Ridwan Syarif S. Cyber jurnalis™ 2


Example :
to call notepad.exe and wait until the user closed the notepad.
program Project2;

{$APPTYPE CONSOLE}
uses
  SysUtils, ShellAPI, Windows;

function WinExecAndWait32(FileName: string; Visibility: Integer): Longword;


var { by Pat Ritchey }
  zAppName: array[0..512] of Char;
  zCurDir: array[0..255] of Char;
  WorkDir: string;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  StrPCopy(zAppName, FileName);
  GetDir(0, WorkDir);
  StrPCopy(zCurDir, WorkDir);
  FillChar(StartupInfo, SizeOf(StartupInfo), #0);
  StartupInfo.cb := SizeOf(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  if not CreateProcess(nil,
    zAppName, // pointer to command line string
    nil, // pointer to process security attributes
    nil, // pointer to thread security attributes
    False, // handle inheritance flag
    CREATE_NEW_CONSOLE or // creation flags
    NORMAL_PRIORITY_CLASS,
    nil, //pointer to new environment block
    nil, // pointer to current directory name
    StartupInfo, // pointer to STARTUPINFO
    ProcessInfo) {// pointer to PROCESS_INF} then Result := WAIT_FAILED
  else
  begin
    repeat
       // Application.ProcessMessages;  hilangkan // ini jika menggunakan aplikasi
visual
       Sleep(100);
    until WaitForSingleObject(ProcessInfo.hProcess, 100) <> WAIT_TIMEOUT;
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
  end;
end;

begin
   WinExecAndWait32('c:\windows\notepad.exe',SW_SHOWNORMAL);
end.

GOOD LUCK !!

Ridwan Syarif S

Yahoo messenger : cyberjournalist_gudboy


Scribd : http://www.scribd.com/people/view/13475431-readwan-gudboy

Ridwan Syarif S. Cyber jurnalis™ 3

You might also like