Board index » delphi » CONFUSION : ShellExecute, WinExec and returned HANDLES !!!?!!?!?

CONFUSION : ShellExecute, WinExec and returned HANDLES !!!?!!?!?

Hi, i'm Nico from the Netherlands.

I have a problem in Delphi 1.0, but i think it is very strongly related
to the Windows API.

Using ShellExecute is built a generic function to start standard
applications (like MS Word, etc.). This function returns the application
handle (that ShellExecute returns) and a possible errormessage.

With this handle i try to close the standard application using :
SendMessage(Handle,WM_CLOSE,0,0);
To my surprise this didn't work!

While testing a few options (making sure i didn't do anything wrong), i
found out that ShellExecute and WinExec return different handles:
ShellExecute : an Instance Handle,
WinExec      : a Handle.

The handle that WinExec works as far as SendKeys(Handle,WM_CLOSE,0,0);
is concerned.

So, you'll think whats the problem, just use WinExec instead of
ShellExecute!
No, I really want to use ShellExecute because it has a few advantages
over WinExec (like setting de default directory).

Who can help me closing an application with the handle I get back from
a  ShellExecute ? I don't know wether there is a function that converts
an instance handle to a application handle ?

Greetz,
Nic.

 

Re:CONFUSION : ShellExecute, WinExec and returned HANDLES !!!?!!?!?


Sommeling, Nico schrieb:

Quote
> Hi, i'm Nico from the Netherlands.

> I have a problem in Delphi 1.0, but i think it is very strongly
> related
> to the Windows API.

> Using ShellExecute is built a generic function to start standard
> applications (like MS Word, etc.). This function returns the
> application
> handle (that ShellExecute returns) and a possible errormessage.

> With this handle i try to close the standard application using :
> SendMessage(Handle,WM_CLOSE,0,0);
> To my surprise this didn't work!

> While testing a few options (making sure i didn't do anything wrong),
> i
> found out that ShellExecute and WinExec return different handles:
> ShellExecute : an Instance Handle,
> WinExec      : a Handle.

> The handle that WinExec works as far as SendKeys(Handle,WM_CLOSE,0,0);

> is concerned.

> So, you'll think whats the problem, just use WinExec instead of
> ShellExecute!
> No, I really want to use ShellExecute because it has a few advantages
> over WinExec (like setting de default directory).

> Who can help me closing an application with the handle I get back from

> a  ShellExecute ? I don't know wether there is a function that
> converts
> an instance handle to a application handle ?

> Greetz,
> Nic.

ShellExecute return the instance handle of the process. This handle is
the segment where the application is loaded. You can not use this handle
to send messages.

WinExec returns the module handle of the module what is executed (exe
file).

To terminate the executed application show this following code:

uses WinTypes, WinProcs, ToolHelp;
...
...
procedure TerminateApplication;
var TaskEntry: TTaskEntry;
const    ApplicationName='MyApp';    {Name of the Application without
".exe"}

begin
 TaskEntry.dwSize:=SizeOf(TTaskEntry);
 if TaskFirst(@TaskEntry) then begin
    if Uppercase(StrPas(TaskEntry.szModule))=ApplicationName then begin
        TerminateApp(TaskEntry.hTask,NO_UAE_BOX);
        Exit;
   end;
   While TaskNext(@TaskEntry) do begin
        if Uppercase(StrPas(TaskEntry.szModule))=ApplicationName then
begin
            TerminateApp(TaskEntry.hTask,NO_UAE_BOX);
               Break;
           end;
       end;
   end;
end;

Regards Bernd

Other Threads