Solved

ShellExecute and WinExec : confusion about Handles

Posted on 1997-10-28
7
796 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:nicsom
  • 4
  • 2
7 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1349062
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;
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1349063
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
0
 

Author Comment

by:nicsom
ID: 1349064
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:nicsom
ID: 1349065
Adjusted points to 250
0
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 250 total points
ID: 1349066
Maybe just delaying your app for a few seconds will help
0
 

Author Comment

by:nicsom
ID: 1349067
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.
0
 

Author Comment

by:nicsom
ID: 1349068
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now