daetips
Messages postés
142
Date d'inscription
jeudi 10 juillet 2003
Statut
Membre
Dernière intervention
10 novembre 2007
22 juin 2005 à 15:09
Bah tu peux regarder ds la liste des processus s'il est actif via son nom.exe, ou une fonction vb tiens:
Public Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Public Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
Public Declare Function CreateToolhelpSnapshot Lib "KERNEL32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Public Declare Function GetCurrentProcess Lib "KERNEL32" () As Long
Public Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Public Declare Function OpenProcess Lib "KERNEL32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Public Declare Function ProcessFirst Lib "KERNEL32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib "KERNEL32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function TerminateProcess Lib "KERNEL32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwflags As Long
szexeFile As String * 260
End Type
Public Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Public Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Dim lhwndProcess As Long
Dim lExitCode As Long
Dim lRetVal As Long
Dim lhThisProc As Long
Dim lhTokenHandle As Long
Dim tLuid As LUID
Dim tTokenPriv As TOKEN_PRIVILEGES, tTokenPrivNew As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
Const PROCESS_ALL_ACCESS &H1F0FFF, PROCESS_TERMINAT &H1Const ANYSIZE_ARRAY 1, TOKEN_ADJUST_PRIVILEGES &H20Const TOKEN_QUERY &H8, SE_DEBUG_NAME As String "SeDebugPrivilege"
Const SE_PRIVILEGE_ENABLED = &H2
Public Function Processus(nom_process, Optional killer As Boolean = False) As Boolean
Dim i As Integer
Dim hSnapshot As Long
Dim uProcess As PROCESSENTRY32
Dim r As Long
Dim Nom(1 To 100)
Dim Num(1 To 100)
Dim nr As Integer
nr = 0
hSnapshot = CreateToolhelpSnapshot(2&, 0&)
If hSnapshot = 0 Then Exit Function
uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapshot, uProcess)
Do While r
nr = nr + 1
Nom(nr) = uProcess.szexeFile
Num(nr) = uProcess.th32ProcessID
r = ProcessNext(hSnapshot, uProcess)
Loop
If killer = False Then
For i = 1 To nr
If Split(UCase(Nom(i)), Chr(0))(0) = UCase(nom_process) Then
'If InStr(UCase(Nom(I)), UCase(nom_process)) <> 0 Then
Processus = True
Exit For
End If
Call CloseHandle(hSnapshot)
Next i
Else
For i = 1 To nr
If Split(UCase(Nom(i)), Chr(0))(0) = UCase(nom_process) Then
'If InStr(UCase(Nom(I)), UCase(nom_process)) <> 0 Then
ProcessTerminate (Num(i))
Call CloseHandle(hSnapshot)
Processus = True
Exit For
End If
Call CloseHandle(hSnapshot)
Next i
End If
End Function
Public Function ProcessTerminate(Optional lProcessID As Long) As Boolean
On Error Resume Next
If lProcessID Then
'Give Kill permissions to this process
lhThisProc = GetCurrentProcess
OpenProcessToken lhThisProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, lhTokenHandle
LookupPrivilegeValue "", SE_DEBUG_NAME, tLuid
'Set the number of privileges to be change
tTokenPriv.PrivilegeCount = 1
tTokenPriv.TheLuid = tLuid
tTokenPriv.Attributes = SE_PRIVILEGE_ENABLED
'Enable the kill privilege in the access token of this process
AdjustTokenPrivileges lhTokenHandle, False, tTokenPriv, Len(tTokenPrivNew), tTokenPrivNew, lBufferNeeded
'Open the process to kill
lhwndProcess = OpenProcess(PROCESS_TERMINAT, 0, lProcessID)
If lhwndProcess Then
'Obtained process handle, kill the process
ProcessTerminate = CBool(TerminateProcess(lhwndProcess, lExitCode))
Call CloseHandle(lhwndProcess)
End If
End If
On Error GoTo 0
End Function
Pour l'utiliser c'est:
MsgBox Processus("msnmsgr.exe") 'renvoi true si ouvert
Lret = Processus("msnmsgr.exe", True) 'renvoi true si ouvert+le ferme
if Lret = true then
msgbox "Le processus à été fermé"
end if
Daetips