Как закрыть внешнюю программу
Это можно сделать несколькими способами. Можно просто послать ей сообщение закрыться:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub CloseProg(hwnd As Long)
SendMessage hwnd, WM_CLOSE, 0, 0
End Sub
Можно принудительно закрыть программу:
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private Const WM_QUERYENDSESSION = &H11
Private Const WM_ENDSESSION = &H16
Private Sub CloseProg(hwnd As Long)
Dim pID As Long
Dim hProc As Long
GetWindowThreadProcessId hwnd, pID
hProc = OpenProcess(PROCESS_TERMINATE, False, pID)
Call SendMessage(hwnd, WM_QUERYENDSESSION, 0, 1)
Call SendMessage(hwnd, WM_ENDSESSION, -1, 1)
TerminateProcess hProc, 0
CloseHandle hProc
End Sub
|