Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I get hWnd from a Process ID???

41 views
Skip to first unread message

Joseph W. Foster

unread,
Aug 7, 1998, 3:00:00 AM8/7/98
to
I'm building a VB 5.0 class that will be an application launcher
responsible for starting and maintaining a collection of applications. One
of the most important functions is when requesting an already running
application, make the application the active window in Win 95/98 & NT
4.0/5.0.

I'm using VB Shell() to initiate the application, which returns a process
ID. I have unsuccessfully tried just about every combination of API calls
to obtain the hWnd using the process ID, so that I can use the
SendMessage() function to cause the application to take focus. There is no
way to know the window caption, so I can't use FindWindow() to get the
hWnd.

I've also tried OpenProcess() to start the applications, but it is of no
help. It would be nice if the STARTUPINFO structure returned the
information that the application starts up with. That way, I could get the
caption, then use FindWindow() and I'm done, but that would make too much
sense...

Any help or suggestions would be greatly appreciated.

Joe Foster

Joseph W. Foster

unread,
Aug 7, 1998, 3:00:00 AM8/7/98
to

Joe LeVasseur

unread,
Aug 7, 1998, 3:00:00 AM8/7/98
to
Joe- (Nice name)
You aren't related to that Joe "We are the BORG"
Foster, are you? Anyway, something like this should work.
Joe
________snip__________________________________
Option Explicit

Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5

Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" _
(ByVal hwnd As Long) As Long

Private lProcessID As Long

Private Sub Command1_Click()
lProcessID = Shell("Notepad.exe", vbNormalFocus)
End Sub

Private Sub Command2_Click()
Dim lhRet As Long
lhRet = hWndFromProcID(lProcessID)
Call BringWindowToTop(lhRet)
End Sub

Public Function hWndFromProcID(ProcID As Long) As Long
Dim lhTmp As Long
Dim lRetVal As Long
Dim lCurrentProcID As Long
'----------------------------
On Error GoTo errhWndFromProcID
lhTmp = GetDesktopWindow()
lhTmp = GetWindow(lhTmp, GW_CHILD)
Do While lhTmp <> 0
lRetVal = GetWindowThreadProcessId(lhTmp, lCurrentProcID)
If lCurrentProcID = ProcID Then
hWndFromProcID = lhTmp
Exit Do
End If
lhTmp = GetWindow(lhTmp, GW_HWNDNEXT)
Loop
Exit Function
errhWndFromProcID:
hWndFromProcID = 0
End Function
__________snip_______________________________

--
***********************************************************
Joe LeVasseur lvas...@tiac.net
Microsoft Developer MVP- Visual Basic
Check out Yankee Clipper Plus- August 98 PCComputing
Mag "Amazing Free Stuff: Programs and Power Toys"
http://www.tiac.net/users/lvasseur/ycphome.html
"He preaches well that lives well, quoth Sancho; that's
all the divinity I understand." Miguel de Cervantes
**********************************************************
PS- Please reply to the newsgroup- except in the
case of flames, insults, etc. (Don't bother.)

Joseph W. Foster <Foste...@home.com> wrote in message
01bdc204$07abcfc0$3e70...@ci672803-a.nash1.tn.home.com...

0 new messages