Solved

ShellExecute and WinExec : confusion about Handles

Posted on 1997-10-28
7
815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 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