Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ShellExecute and WinExec : confusion about Handles

Posted on 1997-10-28
7
Medium Priority
?
834 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
Technology Partners: 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 500 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

Industry Leaders: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 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