Newsgroup: it.comp.lang.visual-basic
Titolo: Uccidere un processo

Salve,
Ho un programma principale che lancia
degli altri n programmi (client) al suo avvio.
Avrei la necessità che alla chiusura del
processo principale di terminare anche
tutti gli altri n programmi lanciati.
Qualcuno mi sa dare una mano?
            Grazie

 ha scritto:

In un modulo inserisci
'----------------------------------
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As
Any) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal
wCmd As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As
Long, lpdwProcessId As Long) As Long
' costanti sendmessage
Public Const WM_CLOSE& = &H10
Public Const WM_QUIT& = &H12
Public Const WM_SETFOCUS = &H7
Public Const WM_ACTIVATE = &H6
Public Const WM_SHOWWINDOW = &H18
Public Const WM_PAINT = &HF
Private Const WM_PRINT = &H317
' costanti handle/applicazioni
Public Const SYNCHRONIZE = &H100000
Public Const INFINITE = -1&
Public Const MAX_PATH = 260
Public Const SW_SHOWNORMAL = 1
Public Const LB_FINDSTRING = &H18F
Public Const GW_HWNDNEXT = 2
Function GetWinHandle(hInstance As Long) As Long
' partendo da hinstance di finestra restituisce winhandle
  Dim tempHwnd As Long
  ' Grab the first window handle that Windows finds:
  tempHwnd = FindWindow(vbNullString, vbNullString)
  ' Loop until you find a match or there are no more window handles:
  Do Until tempHwnd = 0
     ' Check if no parent for this window
     If GetParent(tempHwnd) = 0 Then
        ' Check for PID match
        If hInstance = ProcIDFromWnd(tempHwnd) Then
           ' Return found handle
           GetWinHandle = tempHwnd
           ' Exit search loop
           Exit Do
        End If
     End If
     ' Get the next window handle
     tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
  Loop
End Function
Function ProcIDFromWnd(ByVal hWnd As Long) As Long
' partendo da hwnd di applizione restituisce il proc id
  Dim idProc As Long
  ' Get PID for this HWnd
  GetWindowThreadProcessId hWnd, idProc
  ' Return PID
  ProcIDFromWnd = idProc
End Function
Function ApplicazioneAncoraRunning(LngRET As Long) As Byte
' determina se l'applicazione sta ancora girando
Dim lPID As Long
lPID = OpenProcess(SYNCHRONIZE, 0, LngRET)
If lPID <> 0 Then
    ApplicazioneAncoraRunning = 1
Else
    ApplicazioneAncoraRunning = 0
End If
End Function
'-----------------------------------------
Quando lanci le applicazioni scrivi
LngRET1 = Shell("app1.exe", vbNormalFocus)
.
.
.
LngRETN = Shell("appN.exe", vbNormalFocus)
per verificare se un'applicazione lanciata è ancora attiva usa:
if ApplicazioneAncoraRunning(lngRetX)=1 then ...
per terminare un'applicazione X lanciata usa:
lhwindow = GetWinHandle(LngRETX)
If lhwindow <> 0 Then
     lRetValue = SendMessage(lhwindow, WM_CLOSE, 0, 0&)
End If
Occhio he il tutto è asincrono, devi dare il tempo a winodws di
chiudere/aprire l'applicazione prima di testarla/chiuderla.
x:termoPilucco