Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help getting the HWND of a program after a ShellExecute

1,491 views
Skip to first unread message

Jerome

unread,
Dec 16, 2002, 1:17:54 PM12/16/02
to
Hi,

I need help finding the Window Handle of a program that is
called by my VC++ 6.0 application. It calls ShellExecute,
but ShellExecute returns a HINSTANCE.

How do I get the window handle (HWND) of the program I
just called. I need to resize it and I cannot use
MoveWindow with the program's title, because I have no
sure way of knowing what the application's title will be.

I tried using EnumWindows, but the instances for the
running programs I find this way are not the same than the
one returned by ShellExecute.

Anyone knows a quick and sure way to get the window handle
of an application started by ShellExecute?

Any help will be appreciated.

Thanks,


Jerome

David Crow [MCSD]

unread,
Dec 16, 2002, 2:20:59 PM12/16/02
to
According to the documentation, "Returns a value greater than 32 if
successful, or an error value that is less than or equal to 32 otherwise.
The return value is cast as an HINSTANCE for backward compatibility with
16-bit Microsoft® Windows® applications. It is not a true HINSTANCE,
however. The only thing that can be done with the returned HINSTANCE is to
cast it to an integer and compare it with the value 32 or one of the error
codes below." is the return value actually going to be of any use to you?

"Jerome" <jerome_...@hotmail.com> wrote in message
news:01cc01c2a52f$7400f160$d2f82ecf@TK2MSFTNGXA09...

Jay Nabonne

unread,
Dec 16, 2002, 2:52:41 PM12/16/02
to

"Jerome" <jerome_...@hotmail.com> wrote in message
news:01cc01c2a52f$7400f160$d2f82ecf@TK2MSFTNGXA09...

The easiest way to find the window of an application is with the
application's process id. Unfortunately, ShellExecute doesn't give you any
information at all about the process, and ShellExecuteEx only gives you an
HPROCESS (not the process id). If you can, use CreateProcess instead. It
gives you back the process handle *and* id. You then call WaitForInputIdle
on the process handle to wait for the app to finish starting up, then
enumerate the windows and call GetWindowProcessId to locate the window with
the process id you just started. You might need to look for top-level
windows if the called app creates other, perhaps hidden, windows for
communication (e.g. COM).

HTH
Jay


Nishant S

unread,
Dec 16, 2002, 3:01:51 PM12/16/02
to
Hello Jerome,

See if this article helps :-
http://www.codeproject.com/system/newbiespawn.asp

Regards,
Nish [VC++ MVP]

"Jerome" <jerome_...@hotmail.com> wrote in message
news:01cc01c2a52f$7400f160$d2f82ecf@TK2MSFTNGXA09...

Jerome

unread,
Dec 16, 2002, 6:12:18 PM12/16/02
to
Damn!

I just implemented the CreateProcess -> FindWindow ->
GetWindowProcessID and it almost works.

If I test it with something like notepad.exe, it works
great. However, the program I am calling
is "explorer.exe", because I am opening a URL. However,
when calling "explorer.exe", the FindWindow callback
function does not find the processid of the window just
created.

Anyone know why?

Also, of less importance, is there a way to detect if
Internet Explorer is already opened and get that process's
window instead?

Joseph M. Newcomer

unread,
Dec 16, 2002, 8:39:10 PM12/16/02
to
ShellExecute doesn't return an HWND; in fact, it doesn't return much of anything useful.
ShellExecuteEx will return a handle to the process, which doesn't help much either,
because when you do EnumWindows, the GetProcessID returns only the process ID, not the
process handle. Had anyone been using the slightest shred of intelligence in the design of
ShellExecuteEx, the process ID (which is returned by CreateProcess, which is buried inside
ShellExecute(Ex)) would have been returned. Note that the HINSTANCE returned by
ShellExecute says that the only thing it is good for is being cast to an int for
comparison, and that it has no other meaning. "It is not a true HINSTANCE" are the exact
words in the documentation. So you can't use it for any purpose whatsoever unless its
value is < 32 after it is cast to an int. As the documentation states.

It turns out this is not easy; I asked this same question a few weeks ago and got several
rather complex answers, all of which my client rejected as being "too complex to put into
a service updated" (which is sort of what I recommended, as in "I could do what you ask,
but do you really want to release relatively complex and not-very-thoroughly-tested code
in a service updated?"). Check the back postings for the answers.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Joseph M. Newcomer

unread,
Dec 16, 2002, 8:40:15 PM12/16/02
to
Forget FindWindow. Just use EnumWindows. And don't forget, "just created" means the window
might not be up yet!
joe

Joseph M. Newcomer [MVP]

0 new messages