Solved

Determining hWnd from Process ID....

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

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

Suggested Solutions

Title # Comments Views Activity
Access 2013 combo box not working 3 56
vb6 connector to SQL Server 2 41
Send outlook email from VBS Script 2 58
adding "ungroup sheets" to existing vbs code 5 52
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

679 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