Solved

How To Get hWnd from Process Handle from ShellExecuteEx

Posted on 2002-05-13
11
2,066 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio Exe Issue, 12 34
Vb.Net Loading Word 11 105
Windows 10 Scheduled Tasks 11 99
Example of WebAPI 7 34
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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