An application does not in itself have a window handle. Only windows do. Each form in a VB application is a window, and thus has it's own window handle, which is exposed at the form's hWnd property.
I agree with strongm. This will close any application using API calls by getting the handle of the desired window.
Put this in a module:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
Declare Function sendmessagebystring Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Declare Function getwindow Lib "user32" Alias "GetWindow" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Public Const WM_CLOSE = &H10
Public Const GW_CHILD = 5
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_MAX = 5
Public Const GW_OWNER = 4
Function FindWindowByTitle(Title As String)
Dim a, b, Caption
a = getwindow(Form1.hWnd, GW_OWNER)
Caption = GetCaption(a)
If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
FindWindowByTitle = b
Exit Function
End If
b = a
Do While b <> 0: DoEvents
b = getwindow(b, GW_HWNDNEXT)
Caption = GetCaption(b)
If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
FindWindowByTitle = b
Exit Do
Exit Function
End If
Loop
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.