Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help on ShellExecuteEx and WaitForSingleObject

149 views
Skip to first unread message

ahmoy_law

unread,
May 1, 2003, 9:08:55 PM5/1/03
to

errorcode = 6, 'the handle is invalid' on the CloseHandle(hProcess).

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


Christoph Söllner

unread,
May 2, 2003, 4:22:16 AM5/2/03
to
Try this, it works fine with my apps:
note: WindowState can be SW_SHOW or SW_HIDE;
I didn't try the values, maybe they do fine
as well. hope this helps.


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.


0 new messages