Solved

Determining hWnd from Process ID....

Posted on 1998-07-17
3
174 Views
Last Modified: 2010-05-03
HI there,

My application uses the Shell command to instigate an instance of a sub-programme.  My question is this - the Shell command returns a double which is the process ID created - is there any way of finding the Window with which the process is associated.  Ideally i'd like to retrieve the hWnd value.

I want to do this so I can post a WM_CLOSE message to the window in order to terminate it gracefully.

Pete
0
Comment
Question by:peterwest
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
ID: 1466074
There is no call to get the "window" for a process: there may be none, there may be more than one. Instead you have to use EnumWindows to enumerate all top level windows in the system. In your callback procedure, call GetWindowThreadProcessId to get the ProcessID of the thread which owns the window. When the process IDs match, you've got your window.

I'll post a comment in a moment with some sample code.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1466075
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Integer

ProcessID& = Shell("notepad")
ret = EnumWindows(AddressOf EnumWindowsProc, ProcessID&)
If ret = 0 Then MsgBox "failed"

' This must be in a module
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

Public Function EnumWindowsProc(ByVal hWind As Long, ByVal lParam As Long) As Boolean
Dim WProcessID&,ThreadID&

 ThreadID& = GetWindowThreadProcessId(hWind, WProcessID&)
 If WProcessID& = lParam Then
  ' send WM_CLOSE here
 End If
 EnumWindowsProc = True  'set to false if you don't want any more enumerated
End Function

That will do it... you will probably get more windows than you expect, but you are at the mercy of EnumWindows as to what windows you get. You might be able to winnow them down with something like:
If IsWindowEnabled(hWind) <> False and If IsWindowVisible(hWind) <> False and If IsIconic(hWind) = False Then

Hope this helps, good luck!
0
 
LVL 2

Author Comment

by:peterwest
ID: 1466076
Hi again,

Sorry for the delay in grading your answer - i've tested the code and it worked fine.

Cheers

Pete
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

10 Experts available now in Live!

Get 1:1 Help Now