CreateProcess will returns hProcess and hTread. but ShellExecuteEx will only returns hProcess...?
thanks in advance,
ahmoy law
In article <3eab...@newsgroups.borland.com>, Ahmoy law wrote:
> function ShellExecAndWait ( const AppName, Param: string ): boolean;
> var
> ShellExeInfo: SHELLEXECUTEINFO;
> begin
> FillChar ( ShellExeInfo, 0, sizeof(ShellExeInfo) );
> ShellExeInfo.cbSize := sizeof(ShellExeInfo);
> ShellExeInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
> ShellExeInfo.lpVerb := 'open';
> ShellExeInfo.lpFile := PChar(AppName);
> ShellExeInfo.lpParameters := PChar(Param);
>
> result := FALSE;
> if ( ShellExecuteEx ( @ShellExeInfo ) ) then begin
> if ( WaitForSingleObject ( ShellExeInfo.hProcess, INFINITE ) <>
> WAIT_FAILED ) then
> if ( CloseHandle ( ShellExeInfo.hProcess ) ) then //
> <-------------- problem!!! returns FALSE
And what does SysErrorMessage( GetLastError ) tell you?
Your code organization is wrong, by the way. You need to close the handle
whether the wait succeeded or not.
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be
unit appopen;
interface
uses
WinTypes, WinProcs, SysUtils;
{ WindowState is one of the SW_xxx constants.
Look up ShowWindow in the API help for a list.}
function ExecAndWait(const Filename, Params: string;
WindowState: word): boolean; forward;
implementation
function ExecAndWait(const Filename, Params: string;
WindowState: word): boolean;
{$IFDEF WIN32}
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
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
{$ELSE}
var
InstanceID : THandle;
Buff: array[0..255] of char;
begin
StrPCopy(Buff, Filename + ' ' + Params);
InstanceID := WinExec(Buff, WindowState);
if InstanceID < 32 then
{ a value less than 32 indicates an Exec error }
Result := FALSE
else begin
Result := TRUE;
repeat
Application.ProcessMessages;
until Application.Terminated or
(GetModuleUsage(InstanceID) = 0);
end;
{$ENDIF}
end;
end.