Accueil > > > FUNCTION TERMINER(KILL) UN PROCESSUS PAR SON NOM
FUNCTION TERMINER(KILL) UN PROCESSUS PAR SON NOM
Information sur la source
Description
Ben ce module n'est pas de moi. A la base il liste les processus dans un list box et termine celui de son choix. Mais j'avais besoin de terminé un processus connu au demarage de mon soft, alors j'ai ajouter la fonction qui permet de terminé le processus par son nom d'executable. si ca peu faire gagner du temps a qq?????
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 KillProcessus(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
-
-
- 'voila pour l'utiliser de votre form:
- KillProcessus "iexplore.exe"
'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 KillProcessus(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
'voila pour l'utiliser de votre form:
KillProcessus "iexplore.exe"
Conclusion
Oubliez pas de laisser un petit commentaire, sympa si possible lol....
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|