begin process at 2012 02 11 11:54:22
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Système

 > LISTER ET TERMINER UN PROCESSUS

LISTER ET TERMINER UN PROCESSUS


 Information sur la source

Note :
8,43 / 10 - par 7 personnes
8,43 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Système Niveau :Débutant Date de création :04/09/2003 Date de mise à jour :04/09/2003 23:13:09 Vu / téléchargé :7 456 / 1 089

Auteur : pcpunch

Ecrire un message privé
Site perso
Commentaire sur cette source (9)
Ajouter un commentaire et/ou une note

 Description

Si joint un code qui liste les processus dans un listbox, et qui permet de terminer le processus de son choix!!!

PS: j'ai deja poster une source similaire, mais j'ai ajouter le listage des process a l'attention de TURAKAM!!!!

Il y a aussi la source !!!!! a vous de choisir

Source

  • 'dans un module
  • Option Explicit
  • Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  • Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  • Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  • Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
  • Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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
  • Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  • Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  • Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  • Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
  • Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
  • Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
  • Private Type LUID
  • LowPart As Long
  • HighPart As Long
  • End Type
  • Private Type LUID_AND_ATTRIBUTES
  • pLuid As LUID
  • Attributes As Long
  • End Type
  • Private Type TOKEN_PRIVILEGES
  • PrivilegeCount As Long
  • TheLuid As LUID
  • Attributes As Long
  • End Type
  • Public Const MAX_PATH As Integer = 260
  • Public Const TH32CS_SNAPPROCESS As Long = 2&
  • 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 * MAX_PATH
  • End Type
  • Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  • Function ProcessTerminate(Optional lProcessID As Long, Optional lHwndWindow As Long) As Boolean
  • 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 = &H1
  • Const ANYSIZE_ARRAY = 1, TOKEN_ADJUST_PRIVILEGES = &H20
  • Const TOKEN_QUERY = &H8, SE_DEBUG_NAME As String = "SeDebugPrivilege"
  • Const SE_PRIVILEGE_ENABLED = &H2
  • On Error Resume Next
  • If lHwndWindow Then
  • 'Get the process ID from the window handle
  • lRetVal = GetWindowThreadProcessId(lHwndWindow, lProcessID)
  • End If
  • 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
  • Public Function TerminerProcessus(nom_process) As String
  • 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(TH32CS_SNAPPROCESS, 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
  • For i = 1 To nr
  • If InStr(UCase(nom(i)), UCase(nom_process)) <> 0 Then
  • ProcessTerminate (num(i))
  • Exit For
  • End If
  • Next i
  • End Function
  • Public Function ListerProcess(Destination As ListBox)
  • Dim i As Integer
  • Dim hSnapshot As Long
  • Dim uProcess As PROCESSENTRY32
  • Dim r As Long
  • hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
  • If hSnapshot = 0 Then Exit Function
  • uProcess.dwSize = Len(uProcess)
  • r = ProcessFirst(hSnapshot, uProcess)
  • Do While r
  • Destination.AddItem uProcess.szexeFile
  • r = ProcessNext(hSnapshot, uProcess)
  • Loop
  • End Function
  • 'Dans la form
  • 'Il faut ajouter :
  • ' Un listbox : list1
  • ' Un Command button : Command1
  • Private Sub Form_Load()
  • ListerProcess List1
  • End Sub
  • Private Sub Command1_Click()
  • If List1.Text = "" Then Exit Sub
  • If MsgBox("Vous allez terminer le processus de " & List1.Text & vbCrLf _
  • & "Confirmation ??", vbYesNo, "Attention") = vbNo Then Exit Sub
  • TerminerProcessus List1.Text
  • List1.Clear
  • ListerProcess List1
  • End Sub
'dans un module
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (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
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long

Private Type LUID
LowPart As Long
HighPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type


Public Const MAX_PATH As Integer = 260
Public Const TH32CS_SNAPPROCESS As Long = 2&

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 * MAX_PATH
    End Type


Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Function ProcessTerminate(Optional lProcessID As Long, Optional lHwndWindow As Long) As Boolean
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 = &H1
Const ANYSIZE_ARRAY = 1, TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8, SE_DEBUG_NAME As String = "SeDebugPrivilege"
Const SE_PRIVILEGE_ENABLED = &H2

On Error Resume Next
If lHwndWindow Then
'Get the process ID from the window handle
lRetVal = GetWindowThreadProcessId(lHwndWindow, lProcessID)
End If

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

Public Function TerminerProcessus(nom_process) As String
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(TH32CS_SNAPPROCESS, 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
For i = 1 To nr
If InStr(UCase(nom(i)), UCase(nom_process)) <> 0 Then
ProcessTerminate (num(i))
Exit For
End If
Next i
End Function

Public Function ListerProcess(Destination As ListBox)
Dim i As Integer
Dim hSnapshot As Long
Dim uProcess As PROCESSENTRY32
Dim r As Long
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapshot = 0 Then Exit Function
uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapshot, uProcess)
Do While r
Destination.AddItem uProcess.szexeFile
r = ProcessNext(hSnapshot, uProcess)
Loop
End Function


'Dans la form
'Il faut ajouter :
' Un listbox : list1
' Un Command button : Command1

Private Sub Form_Load()
ListerProcess List1
End Sub

Private Sub Command1_Click()
If List1.Text = "" Then Exit Sub
If MsgBox("Vous allez terminer le processus de " & List1.Text & vbCrLf _
& "Confirmation ??", vbYesNo, "Attention") = vbNo Then Exit Sub
TerminerProcessus List1.Text
List1.Clear
ListerProcess List1
End Sub


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip Source avec une capture SIMON EN VB
Source avec Zip Source avec une capture GRAVURE CD AVEC NEROCMD DEPUIS APPLI VB
Source avec Zip Source avec une capture RADIO SUR INTERNET
Source avec Zip Source avec une capture AFFICHE DES TITULAIRES FOOTBALL (MCFOOTMANAGER)
Source avec Zip Source avec une capture IMPRESSION LABEL DVD

 Sources de la même categorie

Source avec Zip Source avec une capture AUTORISER/REFUSER L'EXECUTION DE PROCESSUS par pierreh51
Source avec Zip Source .NET (Dotnet) CLONE/FORK DES FLUX DE LA CONSOLE : PERMETTRE LA REDIRECTION... par ShareVB
Source avec Zip Source .NET (Dotnet) DÉFRAGMENTER UN FICHIER par ShareVB
Source avec Zip Source .NET (Dotnet) ECRAN DE VEILLE : DÉTECTER LE LANCEMENT/DÉCLENCHER/EMPÊCHER par ShareVB
Source avec Zip Source avec une capture DESACTIVER / ACTIVER LES MISES EN VEILLES PC par Arsena

Commentaires et avis

Commentaire de alex62fr le 23/09/2003 15:17:23

salut !!
le code m'interresse bcp, mais quand je le lance ca me donne l'erreur suivante :
Le point d'entrée Create Toolhelp32Snapshot d'une DLL introuvable dans kernel 32

?? ca viens de koi ca ??
@+ alex

Commentaire de pcpunch le 23/09/2003 23:01:42

Quelle os? c'est une dll qui est en cause!!
sous xp ef pas de probleme!!

Commentaire de Syborg le 14/10/2003 11:54:44

Toolhelp32Snapshot Cette api n'est implémentée que sur 95 98 et ME

sous NT et W2K il faut utiliser les api's suivante de la dll PSAPI.DLL
EnumProcesses()
EnumProcessModules()
GetModuleFileNameExA()

Commentaire de jlesausse le 11/05/2004 18:49:37

Merci Cyborg, mais puisque tu as l'air de maitriser les API, serait-il possible que tu nous codes les modifs en conséquence pour NT / W2K ?
Personnellement je travaille sur NT4 au boulot, et j'ai besoin de savoir à tout moment si un process particulier est bien en train de tourner.

Merci d'avance

Jacques

Commentaire de rabixpvb le 25/02/2005 11:26:01

Mci pr l sourc

Commentaire de Kruggs le 26/02/2005 22:20:31

merci pour le code, yé pas mal, par contre, moi ej voudrai faire une recherche de pross mai pas via la listbox via un tab genre (1,100) e je capte pas la variable "uProcess.szexeFile"
prend comme valeur "process.exe apres ya plein despace ou de caractere pas compri" je sai pas dou sa sort lol si kelekin peu maider a regler sa :) thx davance
:)

Commentaire de fkuchta le 26/05/2005 17:55:45

Franchement, est-ce que tu pourrais écrire en français ?
Sa serévacheman +coul passke sé superdura conprendrsinon.

Commentaire de FLYeRNeT le 19/08/2005 15:21:44

Nickel !!!

Marche avec Windows XP, Windows 2000 Server et Windows 2003 server !!!

Commentaire de Kruggs le 19/08/2005 20:16:12

plop,
dsl fkuchta pour lecriture, les prochaine foi je tacherai de mieu ecrir:)
ba sinon pou le code jai bidouiller autrement pour faire une recherche , vu ke jai pas reussi a ignorer les caractere bizzare derriere le nom du process
merci encore une foi pour le code:)

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,608 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales