In un modulo:
codice:
Option Explicit
'Chiude un programma.
Private Type THREADENTRY32
dwSize As Long
cntUsage As Long
th32ThreadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type
Private Const TH32CS_SNAPTHREAD As Long = &H4&
Private Const WM_QUIT As Long = &H12
Private Declare Function FindWindow Lib "User32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "User32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Thread32First Lib "Kernel32.dll" (ByVal HSnapShot As Long, ByRef EntryModule As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "Kernel32.dll" (ByVal HSnapShot As Long, ByRef EntryModule As THREADENTRY32) As Long
Private Declare Function PostThreadMessage Lib "User32.dll" Alias "PostThreadMessageA" (ByVal idThread As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal hObject As Long) As Long
Dove ti serve:
codice:
Function CloseProcess(pID As Long) As Boolean
'Chiude tutti i thread del processo.
Dim EntryThread As THREADENTRY32
Dim HSnapShot As Long, RetVal As Long
'Recupera tutti i thread del processo.
HSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pID)
If HSnapShot <> -1 Then
EntryThread.dwSize = Len(EntryThread)
RetVal = Thread32First(HSnapShot, EntryThread)
Do While RetVal
If EntryThread.th32OwnerProcessID = pID Then
'Invia il messaggio di chiusura.
Call PostThreadMessage(EntryThread.th32ThreadID, WM_QUIT, 0, 0)
End If
RetVal = Thread32Next(HSnapShot, EntryThread)
Loop
Call CloseHandle(HSnapShot)
End If
End Function
Per cercare il processo:
codice:
Private Sub CloseWindProc(sText As String)
'Chiude la finestra specificata.
Dim H As Long, ID As Long
'Recupera l'handle della finestra.
H = FindWindow(vbNullString, sText)
'Recupera l'identificatore del thread associato alla finestra.
Call GetWindowThreadProcessId(H, ID)
If MsgBox("Chiudere '" & sText & "'?", vbInformation Or vbYesNo, "Chiudi Processo") = vbYes Then CloseProcess (ID)
End Sub
Spero fosse quello che cercavi