Um ein Fremdprogramm unter VB zu beenden, benötigst du einige API

-Funktionen. Füge untenstehende Zeilen in ein VB-Modul (nicht in die Form!) ein. Vorbedingung ist allerdings, daß du die genaue Fenster-Überschrift des zu beendennen Programms kennst.
Das Beenden mittels AppActivate und SendKeys funktioniert zwar auch, ist aber nicht 100%ig zuverlässig. AppActivate aktiviert (falls vorhanden) ein Fenster, daß mit dem beim Aufruf angegebenen Fenstertitel (AppID) beginnt. SendKeys schickt die angegebenen Tastatur-Events an das zur Zeit aktive Fenster. Ob das Fenster noch das selbe ist wie das, was zuvor durch AppActivate aktiviert wurde, ist nicht sicher. Und ob dort Alt-F4 richtig umgesetzt wird, auch nicht. Auf der anderen Seite braucht AppActivate nicht den kompletten Fensternamen...
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As Long, _
ByVal lpWindowName As String) As Long
Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE As Long = &H10
Public Sub BeendeExternesProgramm (sFenstertitel as String)
Dim hwnd As Long
hwnd = FindWindow(0&, sFenstertitel)
If hwnd <> 0 Then
lReturn = SendMessage(hwnd, WM_CLOSE, 0&, 0&)
Debug.Print sFenstertitel & " wurde beendet"
Else
Debug.Print sFenstertitel & " wurde nicht gefunden"
End If
End Sub
Ach ja, noch was:
(Quelle: Dan Appleman, "Visual Basic Programmer's Guide to the Win32 API
)
SendNotifyMessage
VB Declaration
Declare Function SendNotifyMessage& Lib "user32" Alias "SendNotifyMessageA" _
(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As _
Long)
Description
Sends a message to a window. If the target window belongs to the same thread as the caller, this function behaves like the SendMessage function and does not return until the message is processed. If the target window belongs to a different thread, the function returns immediately.
Use with VB
No problem.
Parameter Type/Description
hwnd Long—Handle of a window to receive the message.
msg Long—The identifier of the message.
wParam Long—Depends on the message.
lParam Long—Depends on the message.
Return Value
Long—True on success, False on error. Sets GetLastError.
Platform
Windows 95, Windows NT
...ansgar
Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP