Solved

Determining hWnd from Process ID....

Posted on 1998-07-17
3
175 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

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

22 Experts available now in Live!

Get 1:1 Help Now