Link to home
Start Free TrialLog in
Avatar of hakre
hakre

asked on

How To Get hWnd from Process Handle from ShellExecuteEx

Starting a process and the Enumerating is an easy task, but it does not match for me and I don't know why.

I start an exe with ShellExecuteEx and i finally get a Process Handle back into my Struct. I want to checkout the first WindowHandle it has after 1 sec.

How do I get this hWnd. Enumerating all Desktop Childs lead to nothing at all, the ProcessHandle is in value < 255 ever, the processIds I get there are really big. Any Ideas how to convert this processhandle to a processid to then get the hWnd by enumerating windows ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of pjknibbs
pjknibbs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zoppo
Hi hakre,

first I think you should (if possible) use CreateProcess instead of ShellExecuteEx.
With CreateProcess you recieve both the process handle and the process ID.

Then it should be simply possible to enumerate through all windows and compare
each window's process ID with the process ID you recieved from ShellExecuteEx
using GetWindowThreadProcessId.

hope that helps,

ZOPPO
Avatar of Madshi
Madshi

Yep, the suggestions are right. Use CreateProcess directly. Maybe FindExecutable helps to find what exe you need to start.

Basically it's possible to convert a process handle to a process ID (and I know how), but it's undocumented, so better go the official way and use CreateProcess.

Regards, Madshi.
I needed to do same thing and ultimately decided to use WM_ACTIVATEAPP. After shelling other program, it takes focus away and I receive WM_ACTIVATEAPP trigger:

wpmbeg     WM_ACTIVATEAPP
           ;if we're being deactivated and request to show sk or pf helper, show it
           cmp       [wparam],FALSE                                  ;are we being deactivated?
           jne       @@out                                           ;no-leave
           call      GetForegroundWindow                             ;get foreground window
@@gp:      mov       [temp.hwnd1],eax                                ;save here
           call      GetParent,eax                                   ;get parent
           cmp       eax,NULL                                        ;already gotten?
           jne       @@gp                                            ;no-keep going
           call      GetWindowThreadProcessId,[temp.hwnd1],offset temp.handle1 ;get process id of foreground hwnd
           call      GetWindowThreadProcessId,[hwnd],offset temp.handle2       ;get process id of our hwnd
           mov       eax,[temp.handle1]                                
           cmp       eax,[temp.handle2]                              ;make sure not one of our windows
           je        @@out                                           ;leave if so

           ;show pf helper if requested
           cmp       [main.showpfhelpernow],TRUE                     ;show pf helper?
           jne       @@ssk                                           ;no-leave
           mov       [main.showpfhelpernow],FALSE                    ;clear request
           push      [temp.hwnd1]
           call      pfhelper_dentry,[temp.hwnd1],[main.showpfhelpercfg]
           pop       [temp.hwnd1]

@@ssk:     ;show sk helper if requested
           cmp       [main.showskhelpernow],TRUE                     ;show sk helper?
           jne       @@out                                           ;no-leave
           mov       [main.showskhelpernow],FALSE                    ;clear request
           call      skhelper_dentry,[temp.hwnd1],[main.showskhelpercfg]

@@out:     ;default process
           call      DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]   ;call default handler
           ret
wpmend     WM_ACTIVATEAPP

 
Maybe a bit off-topic, but why are you posting an asm-listing?!? Why would anyone want to write this type of code in asm anyway -- no gain in speed whatsoever. Might even run slower than compiler-optimized-c++-code. On the other hand you're losing clearity. If you can't do without hardcore coding you could at least use those neat highlevel abbreviations provided by MASM, for example.
It's from an asm program written for windows. Sorry didn't have time to convert to your c++ dude. I thought this was Windows Programming topic area. Sorry to violate your rules.
And I thought the whole idea behind 'experts exchange' was some sort of knowledge transport -- you're not exactly making it easy for a wide range of people to comprehend. And yes, this is 'Windows Programming', thus I wouldn't want to see asm, since this is something from the past [except for few areas]. Just a suggestion: Download Intel's software-optimization-guide and go through your code again once you've read it. Then please come back and tell us if writing this code in c++-wouldn't have given you better performance. Not that it matters in this code snippet anyway, but it keeps stalling the pipeline.
Thanks for your time.

.f
bite me