procedure TForm1.CharMapButtonClick(Sender: TObject);
{with one line call up the (home-made) character map application...}
var
returnValue: Integer;
begin
returnValue := WinExec('MyCharMap', SW_SHOWNORMAL);
{note that Delphi will understand the constants below in place of the numeric
values they represent even though WE have not defined them anywhere...}
case returnValue of
0: Application.MessageBox('The system is out of memory or resources', 'Out Of Memory', mb_OK);
ERROR_FILE_NOT_FOUND: Application.MessageBox('Cannot run MyCharMap. MyCharMap.exe is not in the current directory, as it should be', 'No EXE File', mb_OK);
ERROR_BAD_FORMAT: Application.MessageBox('MyCharMap.exe is corrupt or invalid', 'Improper Exe File', mb_OK);
end;
{note that if the value returned from WinExec() is higher than 31, then
all is well, but just handling the above errors should be sufficient...}
end;
at risk of repeating oneself...
{IF we wanted to run the character map application as an 'external' .exe
file then this is what we would do...
with one line call up the (home-made) character map application...
note that Delphi will understand the constants below in place of the numeric
values they represent even though WE have not defined them anywhere...
note that if the value returned from WinExec() is higher than 31, then
all is well, but just handling the above errors should be sufficient...
var
returnValue: Integer;
begin
returnValue := WinExec('MyChMap.exe', SW_SHOWNORMAL);
case returnValue of
0: Application.MessageBox('The system is out of memory or resources', 'Out Of Memory', mb_OK);
ERROR_FILE_NOT_FOUND: Application.MessageBox('Cannot run MyCharMap. MyCharMap.exe is not in the current directory, as it should be', 'No EXE File', mb_OK);
ERROR_BAD_FORMAT: Application.MessageBox('MyCharMap.exe is corrupt or invalid', 'Improper Exe File', mb_OK);
end;}
OR
USING SHELL EXECUTE:
procedure TMainForm.ModulesMenuAppointmentsClick(Sender: TObject);
var
theHandle: HWND;
modulesPath: String;
newExeFileName: String;
begin
modulesPath := exeFilePath + '\Modules\';
newExeFileName := modulesPath + 'Appointments.exe';
theHandle := ProcessExecuteAsync(PChar(newExeFileName), SW_SHOW);
ShellExecute(theHandle, 'open', PChar(newExeFileName), '', '', sw_ShowNormal);
if theHandle <> 0 then TerminateProcess(theHandle, 0);
end;
function TMainForm.ProcessExecuteAsync(CommandLine: String; cShow: Word): HWND;
{a safer method of running exe's than using WinExec (which is discouraged)-
this function is called every time we use ShellExecute elsewhere...}
var
rslt: LongBool;
StartupInfo: TStartUpInfo;
ProcessInfo: TProcessInformation;
Saved_Cursor: TCursor;
begin
result := 0;
Saved_Cursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
try
FillChar(StartupInfo, SizeOf(TStartupInfo),0);
with StartupInfo do
begin
cb := SizeOf(TStartupInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := cShow;
end;
rslt := CreateProcess(nil,PChar(CommandLine),nil,nil,False,
NORMAL_PRIORITY_CLASS or DETACHED_PROCESS, nil, nil,
StartupInfo, ProcessInfo);
if rslt then
with ProcessInfo do begin
result := hProcess; // this is the processes handle
end;
finally {wrap up}
Screen.Cursor := Saved_Cursor;
end;
end;
*************************************************************
From: "Nikolay Simeonov"
To:
Subject: Re: Executing a command
Date sent: Fri, 16 Apr 1999 22:56:00 +0200
Send reply to: Delphi@Kyler.com
>From the Delphi Help:
The WinExec function runs the specified application.
This function is provided for compatibility with earlier versions of
Windows. For Win32-based applications, use the CreateProcess function.
(however this looks much simpler to use)
UINT WinExec(
LPCSTR lpCmdLine, // address of command line
UINT uCmdShow // window style for new application
);
One more from my toolbox, that certainly works:
function ExecAndWait(const Filename, Params: string;
WindowState: word): boolean;
var
SUInfo: TStartupInfo;
ProcInfo: TProcessInformation;
CmdLine: string;
begin
// Enclose filename in quotes to take care of long
// filenames with spaces.
CmdLine := '"' + Filename + '" ' + Params;
FillChar(SUInfo, SizeOf(SUInfo), #0);
with SUInfo do begin
cb := SizeOf(SUInfo);
dwFlags := STARTF_USESHOWWINDOW;
wShowWindow := WindowState;
end;
Result := CreateProcess(NIL, PChar(CmdLine), NIL, NIL, FALSE,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, NIL,
PChar(ExtractFilePath(Filename)), SUInfo, ProcInfo);
// Wait for it to finish.
if Result then
repeat
Application.ProcessMessages;
until WaitForSingleObject(ProcInfo.hProcess, 250) <> WAIT_TIMEOUT;
end;
HTH,
Nikolay
----- Original Message -----
From: Jager, Andre
To:
Sent: 16 April 1999 16:37
Subject: Executing a command
I want to run a command from within a Delphi program. It doesn't work.
Operating system returns file not found or path not found error. Who has an
idea.
I want to delete a file and run the following procedure:
RunProgram ( 'cmd.exe', '/C c:\test.txt' );
>>>
procedure RunProgram ( sProgramName, sCommandLine : string );
var
FStartupInfo: TStartupInfo;
FProcessInfo: TProcessInformation;
begin
Windows.ZeroMemory ( @FStartupInfo, sizeof (TStartupInfo));
Windows.ZeroMemory ( @FProcessInfo, sizeof (TProcessInformation));
FStartupInfo.cb := sizeof(TStartupInfo);
Win32Check (
windows.CreateProcess ( pChar ( sProgramName ),
pChar ( sCommandLine ),
nil,
nil,
false,
0,
nil,
nil,
FStartupInfo,
FProcessInfo
)
);
end;
<<<
Andr_ Jager