Solved

Determining hWnd from Process ID....

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 63
Visual Studio search word table and return Cell index 8 60
VBA Shell can't Find Word document 11 92
Export Data to Different .csv Files 26 103
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

815 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

7 Experts available now in Live!

Get 1:1 Help Now