Click to See Complete Forum and Search --> : Shell()'s TaskID to hWnd?
How do I get the window handle of a shelled program's main window from the taskid that the shell function returns?
E.G. If I use the following code:lTaskID = Shell("pbrush.exe", vbNormalFocus)I need to use lTaskID to get the handle of the window entitled "Untitled - Paint".
I can't simply use FindWindow because the FindWindow function would usually run before the shelled program has finished loading up. That is the method I'm currently using, along with the Sleep function, but it's far from reliable.
I can use the ShellExecute API directly is that is easier.
hitcgar
Sep 29th, 2000, 12:43 PM
From an example on MSDN using ShellExecute...
'in module
Public Const GW_HWNDNEXT = 2
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwprocessid As Long) As Long
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim idProc As Long
' Get PID for this HWnd
GetWindowThreadProcessId hwnd, idProc
ProcIDFromWnd = idProc
End Function
Function GetWinHandle(hInstance As Long) As Long
Dim tempHwnd As Long
' Grab the first window handle that Windows finds:
tempHwnd = FindWindow(vbNullString, vbNullString)
' Loop until you find a match or there are no more window handles:
Do Until tempHwnd = 0
' Check if no parent for this window
If GetParent(tempHwnd) = 0 Then
' Check for PID match
If hInstance = ProcIDFromWnd(tempHwnd) Then
' Return found handle
GetWinHandle = tempHwnd
' Exit search loop
Exit Do
End If
End If
' Get the next window handle
tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
Loop
End Function
Cheers, matey!
That code looks promising.
I'll give it a try on Monday when I'm back in work. :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.