Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How To Get hWnd from Process Handle from ShellExecuteEx

Posted on 2002-05-13
11
Medium Priority
?
2,140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 12

Accepted Solution

by:
pjknibbs earned 200 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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 …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 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