Title: Running files / programs from your application
Question: How to run files / programs from your application
Answer:
//--------------------------------------------------------------------
// Running programs / files from your application
// Reference: microsoft MSDN library
//--------------------------------------------------------------------
// Just a form with two buttons on it.
// One button is called: "WinExecBtn" and the other button is called
// "CreateProcessBtn".
//--------------------------------------------------------------------
// Date: 20 Jly 2001 13:23: Added: WaitForSingleObject and CloseHandle
//--------------------------------------------------------------------
Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Type
TForm1 = Class(TForm)
CreateProcessBtn : TButton;
WinExecBtn : TButton;
OpenDialog1 : TOpenDialog;
Procedure WinExecBtnClick(Sender : TObject);
Procedure CreateProcessBtnClick(Sender : TObject);
Private
{ Private declarations }
Public
{ Public declarations }
End;
Var
Form1: TForm1;
Implementation
{$R *.DFM}
//--------------------------------------------------------------------
// Running programs / files from your application
//
// DOS program:
// ------------
// WinExec(PChar('COMMAND.COM /C d:\path\executable [paramstring]',SW_NORMAL);
//
// Windows program:
// ----------------
// WinExec(PChar('d:\path\executable [paramstring]',SW_NORMAL);
//
// LNK, URL, HTM, HTML, PDF, XLS, DOC files:
// -----------------------------------------
// ShellExecute(0,nil,PChar(FileName),nil,nil,SW_SHOWNORMAL);
//
// MicroSoft states however:
// "(...) Note: This function is provided only for compatibility with
// 16-bit Windows. Win32-based applications should use the
// CreateProcess function. (..)"
// See for this statement the "WinExec" link below.
// So we have to start programs with "CreateProcess" in future.
//--------------------------------------------------------------------
// The old method
// Tested on W95 SP1 and WinNT 4 SP6
//--------------------------------------------------------------------
Procedure TForm1.WinExecBtnClick(Sender: TObject);
Begin
OpenDialog1.Title := 'Choose process to open...';
OpenDialog1.Filter := 'Executables|*.exe|All files|*.*';
OpenDialog1.FilterIndex := 0;
If Opendialog1.Execute then
Begin
{DOS application WinExec(
PChar('COMMAND.COM / C ' +
OpenDialog1.Filename + ' ' +
ParamStr),SW_NORMAL);}
{Windows application}
WinExec(PChar(OpenDialog1.Filename { + ' ' + ParamStr}),
SW_NORMAL);
End;
End;
//--------------------------------------------------------------------
// The new method
// Tested on W95 SP1 and WinNT 4 SP6
//--------------------------------------------------------------------
Procedure TForm1.CreateProcessBtnClick(Sender: TObject);
Var
STARTUPINFO : _STARTUPINFOA;
PROCESS_INFORMATION : _PROCESS_INFORMATION;
Begin
OpenDialog1.Title := 'Choose process to open...';
OpenDialog1.Filter := 'Executables|*.exe|All files|*.*';
OpenDialog1.FilterIndex := 0;
If Opendialog1.Execute then
Begin
// Get StartUp Information
GetStartUpInfo(STARTUPINFO);
// Enable using wShowWindow value
// STARTUPINFO.dwFlags := STARTF_USESHOWWINDOW;
// hide child window
// STARTUPINFO.wShowWindow := SW_HIDE;
// application
CreateProcess(PChar(OpenDialog1.Filename),
// command line
'',
// lpProcessAttributes
nil,
// lpThreadAttributes
nil,
// Handle to inheritance option
TRUE,
// Creation flags
CREATE_DEFAULT_ERROR_MODE,
// New environment block
nil,
// Current directory name
nil,
// Startup information
STARTUPINFO,
// Process information
PROCESS_INFORMATION);
// Wait for (child) process to terminate
WaitForSingleObject(PROCESS_INFORMATION.hProcess,INFINITE);
// Close handles
Closehandle(PROCESS_INFORMATION.hProcess);
CloseHandle(PROCESS_INFORMATION.hThread);
End;
End;
//--------------------------------------------------------------------
End. {of Unit1}
//====================================================================
// For further information on:
// ---------------------------
//
// MDSN library home:
// http://msdn.microsoft.com/library/default.asp
//
// Function CreateProcess:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
// dllproc/hh/winbase/prothred_9dpv.asp
//
// Structure PROCESS_INFORMATION:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
// dllproc/hh/winbase/prothred_71pu.asp
//
// Structure STARTUPINFO:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
// dllproc/hh/winbase/prothred_7sdu.asp
//
// Procedure GetStartUpInfo:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
// dllproc/hh/winbase/prothred_2x9r.asp
//
// Function WinExec:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/
// dllproc/hh/winbase/prothred_36w3.asp
//====================================================================