Article ID: 127030
Article Last Modified on 12/9/2003
' Constants used
Global Const GW_HWNDNEXT = 2
Global Const GWW_HINSTANCE = (-6)
' Enter each of the following declarations as one, single line:
Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer
Declare Function GetWindow Lib "User" (ByVal hWnd As Integer,
ByVal wCmd As Integer) As Integer
Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer,
ByVal nIndex As Integer) As Integer
Declare Function FindWindow Lib "User" (ByVal lpClassName As Any,
ByVal lpWindowName As Any) As Integer
Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer,
ByVal lpString As String, ByVal aint As Integer) As Integer
Function GetWinHandle (hInstance%) As Integer
' Function receives an instance handle as a parameter and returns
' the windows handle of the window with a matching instance handle.
Dim tempHwnd%
' Grab the first window handle that Window's finds:
tempHwnd% = FindWindow(0&, 0&)
' Loop until there are no more window handles:
Do Until tempHwnd% = 0
' Check if you have the applications Parent window:
If GetParent(tempHwnd%) = 0 Then
' Check the instance handle for the app:
If hInstance% = GetWindowWord(tempHwnd%, GWW_HINSTANCE) Then
' Found a match:
GetWinHandle = tempHwnd%
Exit Do
End If
End If
tempHwnd% = GetWindow(tempHwnd%, GW_HWNDNEXT)
Loop
End Function
Sub Command1_Click ()
Dim hInst As Integer ' Instance handle from Shell function.
Dim hWndApp As Integer ' Window handle from GetWinHandle.
Dim buffer As String ' Holds caption of Window.
Dim numChars As Integer ' Count of bytes returned.
' Shell to an application and get its window handle:
hInst = Shell("calc.exe")
hWndApp = GetWinHandle(hInst)
' Verify that you have the correct handle by displaying
' its window caption in a message box:
buffer = Space$(128)
numChars = GetWindowText(hWndApp, buffer, Len(buffer))
MsgBox "You shelled to the Application: " & Left$(buffer, numChars)
End Sub
Additional query words: 2.00 3.00
Keywords: kbcode KB127030