Código: Seleccionar todo

Option Explicit
'----------------------------------------------------------------------------------------
' Module     : TerminateProcessByName v2.0
' Purpose    : Finalize process by Name
' Author     : The Swash
' References : API-Guide and MSDN
' DateTime   : 21/06/2010
' Website    : www.Indetectables.Net
'----------------------------------------------------------------------------------------

'Kernel32
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32" (ByVal hObject As Long) As Long

'NTDLL
Private Declare Function NtTerminateProcess Lib "ntdll" (ByVal hProcess As Long, lpExitStatus As Long) As Long

'Constants
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Const PROCESS_ALL_ACCESS = &H1F0FFF
Const STILL_ACTIVE = &H103

'Type PROCESSENTRY32
Private 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

Public Function TerminateProcessByName(ByVal lpzProcess As String) As Boolean
Dim hCTHS       As Long
Dim hProc       As PROCESSENTRY32
Dim hBase       As Long
Dim sBuff       As String
Dim sPID        As String
Dim hOpen       As Long
Dim hTerminate  As Long
Dim sParts()    As String
Dim i           As Long

hCTHS = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
hProc.dwSize = Len(hProc)
hBase = Process32First(hCTHS, hProc)

Do While hBase
   sBuff = Left(hProc.szExeFile, GetLongString(hProc.szExeFile))
   If LCase$(Left(hProc.szExeFile, GetLongString(hProc.szExeFile))) = LCase$(lpzProcess) Then sPID = sPID & hProc.th32ProcessID & ","
   hBase = Process32Next(hCTHS, hProc)
Loop

Call CloseHandle(hCTHS)

If Len(sPID) > 0 Then
   sPID = Left$(sPID, Len(sPID) - 1)
   sParts() = Split(sPID, ",")
   
   For i = LBound(sParts) To UBound(sParts)
       hOpen = OpenProcess(PROCESS_ALL_ACCESS, 0, sParts(i))
       hTerminate = NtTerminateProcess(hOpen, 0)
       CloseHandle (hOpen)
   Next i
   
   If hTerminate <> 0 Then ' 0 = SUCESS
      TerminateProcessByName = False
      Else
      TerminateProcessByName = True
   End If
   
End If

End Function

'Get Long of string
Public Function GetLongString(ByVal sData As String) As Long
If InStr(1, sData, Chr(0)) > 0 Then
   GetLongString = InStr(1, sData, Chr(0)) - 1
   Else
   GetLongString = Len(sData)
End If
End Function
Les presento este codigo, que es por decirlo una nueva version o actualización de este modulo :

Código: Seleccionar todo

http://indetectables.net/foro/viewtopic.php?f=14&t=20380
En este caso decidi reemplazar unas apis (TerminateProcess y GetExitCodeProcess) por NtTerminateProcess y ademas elimina todos los procesos que tengan el mismo nombre, es decir si hay 1000 notepad los termina a todos!

Espero les guste..
Salu2!
En tu ventana
Y en tu ventana, gritas al cielo pero lo dices callada..
Hola The Swash! ya he probado tu código y funciona.. le he echado un pequeño vistazo y he visto una cosilla que tampoco tiene importancia, porque supongo que se te habrá pasado.
The Swash escribió:sBuff = Left(hProc.szExeFile, GetLongString(hProc.szExeFile))
If LCase$(Left(hProc.szExeFile, GetLongString(hProc.szExeFile))) = LCase$(lpzProcess) Then sPID = sPID & hProc.th32ProcessID & ","
Aquí haces lo mismo 2 veces. No sería mejor...

Código: Seleccionar todo

sBuff = Left(hProc.szExeFile, GetLongString(hProc.szExeFile))
   If LCase$(sBuff) = LCase$(lpzProcess) Then sPID = sPID & hProc.th32ProcessID & ","
Además te propongo usar la función Trim para quitar espacios iniciales y finales en blanco (útil cuando no sabes cómo es el string de entrada).
Quedaría una cosa así:

Código: Seleccionar todo

sBuff = Left(hProc.szExeFile, GetLongString(hProc.szExeFile))
   If Trim(LCase(sBuff)) = Trim(LCase(lpzProcess)) Then sPID = sPID & hProc.th32ProcessID & ","
(he testeado también la corrección que te he propuesto y sigue funcional)
Si puedo proponerte alguna mejora más al code te lo digo ok?

Un saludo! R-007
Muchas gracias, Trim no funciona tan bien como deberia, pero te agradezco por la recomendaciones!
En tu ventana
Y en tu ventana, gritas al cielo pero lo dices callada..
Un placer poder colaborar en lo que se puede! jejeje
Bueno mañana lo miro con más detenimiento (aunque tenga examen pasado mañana xd) que hoy ya estoy que me duermo zZzz
jajja
bueno un saludo! xaoo
Responder

Volver a “Otros lenguajes”