Solved

ShellExecute WaitForSingleObject Troubles with MDI-apps.

Posted on 2002-03-13
8
381 Views
Last Modified: 2010-04-05
Hi,
I want to run other applications from within my app and detect when they are closed. I know how to write the code (ShellExecuteEx and use the hProcess Pointer with WaitForSingleObjectEx). However I have two problems with this implementation:
First problem: when I close MSWord, it takes more than 5 secs. until my application detects Word is closed (probably Word does a lot of finalization after making its main window invisible). However I want to have a dialog popped up in my program the moment the Plugin Application (e.g. Word) is closed and the user returns to my program asking him to upload his document to a server.
Second problem: some applications like Word, Acrobat Reader, Internet Explorer (pity they are so popular) do not return a hProcess handle when opening more than one document. So I cannot use the WaitForSingleObject API call. Is there a possibility to get a handle to the document itself so I can detect when the user closes them?

Thanks in advance,
Bart.

PS I'm searching for a general solution, not a MSWord specific solution.
0
Comment
Question by:tijskex
8 Comments
 
LVL 4

Expert Comment

by:nestorua
ID: 6862220
HI,
I think you want to know the moment the child window of the main window of the external process is about
to close. For this task I recommend you to use Hooks.
Sincerely,
Nestorua.
0
 

Author Comment

by:tijskex
ID: 6863745
Can you be a bit more concrete? How do I use these hooks?
How do I detect when the which child window is related to my document file and how do I detect when it is closed?
0
 
LVL 20

Accepted Solution

by:
Madshi earned 200 total points
ID: 6864820
You have a real problem here. If ShellExecute does not return a valid process handle, you can only guess which new window belongs to your ShellExecute call. You could enumerate all windows (see EnumWindows) before your ShellExecute call and after and then compare which are new. But the new ones might come from an already running application, which just thought it wants to popup a new window. It doesn't need to be from your ShellExecute call. Furthermore often an application needs its time to initialize before it shows its window. So you have to wait some seconds until you look for new windows. This increases the possibility of wrong new window detects, of course. It's all quite ugly, unfortunately there is no 100% solution in this case, I'm sorry. You'll have to find the solution which is less bad than the others...   :-(

Regards, Madshi.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:nestorua
ID: 6865062
HI,
With the hook WH_CBT you can catch the moment when the Child window is about to close and forbid it or do what do you want to before allowing it to close.
Sincerely,
Nestorua.
0
 

Author Comment

by:tijskex
ID: 6867496
My fear is that Madshi is right, because I don't think I can ever be sure from which window I receive messages. I mean: if I open two documents and I receive a close hook, I do not know which of the two documents is closed (in case of Word I guess I could look at the caption, but this is no general solution). So probably I will have to make a combination of all kinds of solutions (e.g. in case application is WinWord to this, else to that...).

I leave the question open for a while in case someone has a better solution.





 
0
 
LVL 4

Expert Comment

by:nestorua
ID: 6868700
HI,
You receive the message from that window which is about to close, so take its handle and work with it.
Sincerely,
Nestorua.
0
 

Author Comment

by:tijskex
ID: 6876145
The problem is just to know which window belongs to which document. When I open two documents in a MDI-program (like word), and then I receive a WH_CBT message, how can I know which of the two documents is closed. So I'm looking for a method that returns me the window handle of the document  I opened with ShellExecuteEx. In that way when I receive a WH_CBT message I can compare it with a list of open documents and my program know which document is closed.
0
 

Expert Comment

by:CleanupPing
ID: 9343274
tijskex:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 327
Delphi: how to send PJL commands to printer 3 109
JAudiorecorder record freezing the app 29 76
Adoquery sql  left join does not work 25 99
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 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