Solved

ShellExecute window...

Posted on 2001-07-31
11
896 Views
Last Modified: 2012-06-27
I open files using ShellExecute command. The command opens application, according to file type. Can I get window handle of starting application?

For example,
 I open txt file.
 Notepad started.
 Now I want get window handle of notepad (to change size, location etc. of the window)
0
Comment
Question by:Slavak
11 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 6337729
If you know how the title and/or class of the new window will be, you can use FindWindow. If you don't know that, using ShellExecute(Ex) is not the correct solution. In that case you should use FindExecutable to find the exe which is registered for the file type (in your example *.txt). Then you should call CreateProcess. You'll get a processID from CreateProcess. After CreateProcess returned you should wait a bit to let the application start. Then you can use EnumWindows to enumerate all windows. For each enumerated window you can call GetWindowThreadProcessID to get the processID of the enumerated window. If the processID matches the processID you got from CreateProcess, you have a window of the newly created process. It's a pain to do it like that, but there is no other way (I know).

Regards, Madshi.
0
 
LVL 5

Expert Comment

by:Gwena
ID: 6338803
Listening :-)
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6339060
If you use ShellExecuteEx to execute it, you will need to pass in a PShellExecuteInfo. The function will return back the ShellExecuteInfo structure, which contains hProcess, a handle to the newly started application (of course, you need to set PShellExecuteInfo^.fMask to include SEE_MASK_NOCLOSEPROCESS). Using that handle, you can call stuff like what Madshi said.

(Hi, Madshi!)



DragonSlayer
0
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!

 
LVL 20

Expert Comment

by:Madshi
ID: 6339793
Hi DragonSlayer!  :-)

Unfortunately it's not fully correct, what you said. You can get a process *HANDLE* from ShellExecuteEx. But what we need for comparing enumerated windows is a process *ID*. That are two different beasts. And there is no official way to get the ID from the handle (though I'm having hacked functions that do that... :-).

Regards, Madshi.
0
 
LVL 3

Author Comment

by:Slavak
ID: 6339818
Thanks, Madshi.

I thinked about the way, but hoped for better way to do it.
This way has 2 major problems:
-- how much is "a bit" (time between calls to CreateProcess and EnumWindows)
-- it can be too slow

still hope for better solution :)

0
 
LVL 3

Author Comment

by:Slavak
ID: 6339826
I can compromise with the solution if I can GET EVENT  that started application ready
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 6341913
Well, you can call WaitForInputIdle(processHandle, INFINITE). That at least brings you nearer to the time point, where you can enumerate the windows. How about this logic?

(1) CreateProcess
(2) WaitForInputIdle
(3) Loop for 5 seconds
(4) In the loop check for a taskbar window (a window of that new process, which appears on the taskbar), if you don't find one, sleep for 100ms, then check again
(5) As soon as you find a taskbar window, break the loop
(6) If you don't find a taskbar in 5 seconds, either give back 0, or the first non-taskbar window

Is not nice, I know. But there is no real message/event, that an application has fully loaded.

It should not be slow, in case the application really will create a taskbar window. Then the logic from above should work very fine. But in case the application will never create taskbar window, you'll wait 5 seconds and nevertheless find no window...   :-(

Regards, Madshi.
0
 
LVL 3

Author Comment

by:Slavak
ID: 6343712
There is more elegant (but more difficult too) way:
(1) Create process
(2) Set hook on CreateWindow(ex) function in the process.

Demerit of this way is that hook function should be in dll.

I should minimize executable file(s) size.

May be you can suggest another way.

Thanks
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6343767
Well, but in your exe file you need the window handle, don't you? So you would in your exe file still need to wait for your dll to send the window handle to you, or am I wrong? So basically I see no advantage, the only advantage is that you don't need to poll, but get notified about the window creation. But polling in a loop with a sleep(100) in the loop is really no problem at all.

For what purpose do you need the window handle? What do you want to do with it? Maybe we can find a better way, if I know what you really want to do.
0
 
LVL 3

Author Comment

by:Slavak
ID: 6343876
I want set position and size of the window.

For this, no need return event from hook to application. I can pass required values when I set the hook, and after first top window, remove the hook.

I think about way to open file in my window, like com objects in TOLEContainer.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6343913
Hmmm... Okay, in such a situation a hook could be solution, a bit work, but possible. However, if you look at how e.g. Delphi's windows look like (multiple top-level windows, main window, edit window, object inspector, project manager, etc) you'll run into problems with what you want to do...   :-(

Regards, Madshi.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

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