Solved

How To Get hWnd from Process Handle from ShellExecuteEx

Posted on 2002-05-13
11
2,044 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 31

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now