Solved

How To Get hWnd from Process Handle from ShellExecuteEx

Posted on 2002-05-13
11
2,034 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:hakre
11 Comments
 
LVL 12

Accepted Solution

by:
pjknibbs earned 50 total points
ID: 7007862
I'm not sure how you convert a process handle to an ID. In this situation I'd probably use the Registry (HKEY_CLASSES_ROOT hive) to determine which application to use to launch the relevant document, then start it using CreateProcess()--this returns both a handle and an ID for the process it launches.

This approach is definitely safer anyway, because if you use ShellExecuteEx to launch a document and the relevant application for handling that document is already open, it'll just open the document in a new window in the running application and will return NULL as the process handle--something you almost certainly don't want to happen!
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 7008073
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
0
 
LVL 20

Expert Comment

by:Madshi
ID: 7011889
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.
0
 
LVL 1

Expert Comment

by:mortonsw
ID: 7031130
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

 
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 8

Expert Comment

by:fl0yd
ID: 7035845
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.
0
 
LVL 1

Expert Comment

by:mortonsw
ID: 7036403
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.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7036856
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
0
 
LVL 1

Expert Comment

by:mortonsw
ID: 7037541
bite me
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

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

18 Experts available now in Live!

Get 1:1 Help Now