Hi all!
Is it possible to get the window caption of an active process given its ".exe name"? If it's possible, how to do it?
Thanks for any reply.
Printable View
Hi all!
Is it possible to get the window caption of an active process given its ".exe name"? If it's possible, how to do it?
Thanks for any reply.
Take a look at this LINKQuote:
Originally Posted by Momotae
It does not work for meQuote:
Originally Posted by Mark Gambo
I tried to use GetWindowText from the returned hWnd, and it does not work... (returns empty string, or returns "M"...)
VB Code:
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 Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch 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, LN As Long, WindowText As String lhRet = hWndFromProcID(lProcessID) LN = GetWindowTextLength(lhRet) WindowText = String(LN + 10, 0) GetWindowText lhRet, WindowText, Len(WindowText) Debug.Print WindowText '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
Works for me, thanks Mark...
Very Nice, it work for me too, Thx :)