Solved

ShellExecute and WinExec : confusion about Handles

Posted on 1997-10-28
7
795 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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