Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing Batch Files

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Does anyone know how to execute a .bat or .cmd file from within a Delphi application? ExecuteFile and ShellExecute do not work for me.
 
copy the STO_ShellExecute to your project see Interpretation for an exemple how to use it with params

uses
Windows, ......., ShellApi;

function TFormBegin.Interpretation():Boolean;
var ExitCode: DWORD;
rf:TextFile;
i:integer;
msg:string;
reponse:Boolean;
prog,args:String;
begin
reponse := TRUE;
DeleteFile(ReponseFile);
i := 1;
while (i<=StrLen(PChar(BatFile))) and (BatFile<>' ') do begin
prog := prog + BatFile;
inc(i);
end;
while (i<=StrLen(PChar(BatFile))) do begin
args := args + BatFile;
inc(i);
end;

//execution du fichier &quot;BAT&quot;
STO_ShellExecute(prog,args,200000,False,ExitCode);

//traitement du reponse
AssignFile(rf,ReponseFile);
{$I-}
Reset(rf);
{$I+}
if IOResult<>0 then begin
showmessage('Fichier '+ReponseFile+' non trouvé');
reponse := FALSE;
end
else begin
readln(rf,i);
readln(rf,msg);
if i>0 then begin
ShowMessage(msg);
reponse := FALSE;
end
else for i:=1 to Nb_exec do
ShellExecute(0,'open',PChar(ExecFile),'','',SW_NORMAL);
CloseFile(rf);
end;

if reponse then Timer1.Enabled := TRUE;
Interpretation := reponse;
end;

////////////////////////////////////////////////////////////////
// AppName: name (including path) of the application
// AppArgs: command line arguments
// Wait: 0 = don't wait on application
// >0 = wait until application has finished (maximum in milliseconds)
// <0 = wait until application has started (maximum in milliseconds)
// Hide: True = application runs invisible in the background
// ExitCode: exitcode of the application (only avaiable if Wait <> 0)
//
function STO_ShellExecute(const AppName, AppArgs: String; const Wait: Integer;
const Hide: Boolean; var ExitCode: DWORD): Boolean;
var
myStartupInfo: TStartupInfo;
myProcessInfo: TProcessInformation;
sAppName: String;
iWaitRes: Integer;
begin
// initialize the startupinfo
FillChar(myStartupInfo, SizeOf(TStartupInfo), 0);
myStartupInfo.cb := Sizeof(TStartupInfo);
myStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
if Hide then // hide application
myStartupInfo.wShowWindow := SW_HIDE
else // show application
myStartupInfo.wShowWindow := SW_SHOWNORMAL;

// prepare applicationname
sAppName := AppName;
if (Length(sAppName) > 0) and (sAppName[1] <> '&quot;') then
sAppName := '&quot;' + sAppName + '&quot;';

// start process
ExitCode := 0;
Result := CreateProcess(nil, PChar(sAppName + ' ' + AppArgs), nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(AppName)),
myStartupInfo, myProcessInfo);

// could process be started ?
if Result then
begin
// wait on process ?
if (Wait <> 0) then
begin
if (Wait > 0) then // wait until process terminates
iWaitRes := WaitForSingleObject(myProcessInfo.hProcess, Wait)
else // wait until process has been started
iWaitRes := WaitForInputIdle(myProcessInfo.hProcess, Abs(Wait));
// timeout reached ?
if iWaitRes = WAIT_TIMEOUT then
begin
Result := False;
TerminateProcess(myProcessInfo.hProcess, 1);
end;
// get exitcode
GetExitCodeProcess(myProcessInfo.hProcess, ExitCode);
end;
CloseHandle(myProcessInfo.hProcess);
end;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top