Link to home
Start Free TrialLog in
Avatar of nicsom
nicsom

asked on

ShellExecute and WinExec : confusion about Handles

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,
Nico.
Avatar of ZifNab
ZifNab

Hi nicsom,

I don't know about shellexecute. But I believe there exists a component which can give you a correct handle. It's called XSpawn. It was shareware, now it's freeware. It uses a totally new made library. Have a look at that component. I use that component for my D1 applications. The problem is that you have to install extra files with your program.
I haven't tested it for your program (about handle) so you 've to look at it for yourself

Have fun,
ZifNab;
You can terminate a proccesse by using the TerminateProccess API.
Using SendMessage won't work, cause ShellExecute returns the handle of the proccess and SendMessage requires a window's handle
Avatar of nicsom

ASKER

Sorry Mike,

Your suggestion was already tried out, but doesn't work, because you have to supply a task handle, not an instance handle.

I've solved part of the problem by using the API function GETWINWORD, comparing the instance handles and retrieving the task handle.

The only problem that remains is that the task handle becomes available as soon as windows updates its administration. So i have to place (for instance) a messagebox after the shellexecute to make the task handle available. Not to pretty, huh?

Do you have any idea how to ORDER windows to update its administration without bothering the user with it?

Greetz,
Nic.
Avatar of nicsom

ASKER

Adjusted points to 250
ASKER CERTIFIED SOLUTION
Avatar of MikeP090797
MikeP090797

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nicsom

ASKER

Just delaying is not the answer, I found a callback routine which searches for the right handle. But it can only get this handle when i call the windows API-function MESSAGEBOX just before the call back routine. If i don't, the routine returns the wrong handle.
It looks like Windows has to get control after launching the app, to update its window-administration (however that is called).
As you may realize, my users don't want to click the OK button every time my delphi-app launches another app.

Greetz,
Nic.
Avatar of nicsom

ASKER

Just delaying is not the answer, I found a callback routine which searches for the right handle. But it can only get this handle when i call the windows API-function MESSAGEBOX just before the call back routine. If i don't, the routine returns the wrong handle.
It looks like Windows has to get control after launching the app, to update its window-administration (however that is called).
As you may realize, my users don't want to click the OK button every time my delphi-app launches another app.

Greetz,
Nic.