elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Entrar al Canal Oficial Telegram de elhacker.net


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Matar un proceso concreto sabiendo su ruta
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Matar un proceso concreto sabiendo su ruta  (Leído 2,150 veces)
rantam

Desconectado Desconectado

Mensajes: 5


Ver Perfil
Matar un proceso concreto sabiendo su ruta
« en: 22 Marzo 2008, 18:06 pm »

hola!
Necesito matar un proceso concreto sabiendo su path. todos los codes q leo son para matar un proceso con su nombre pero esto no discrimina si hay 2 procesos con el mismo nombre ejecutandose... :-(
Me explico yo tengo 2 procesos ejecutandose en 2 carpetas distintas pero se llaman igual:
c:\prueba1\hola.exe
c:\pruebas2\hola.exe
y kisiera matar uno en concreto (c:\pruebas2\hola.exe por ejemplo) y q el otro se mantenga activo.
Alguien sabe de algun code o la forma de conseguir esto?
Muxas gracias! ;)


En línea

~~
Ex-Staff
*
Desconectado Desconectado

Mensajes: 2.981


Ver Perfil WWW
Re: Matar un proceso concreto sabiendo su ruta
« Respuesta #1 en: 22 Marzo 2008, 19:57 pm »

Mira, con este code puedes obtener la ruta de todos los procesos activos, luego ya te las puedes apañar  para matar el q kieras:

Código
  1. 'en un modulo
  2.  
  3. Public Declare Function Process32First Lib "kernel32" ( _
  4.         ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
  5.  
  6.      Public Declare Function Process32Next Lib "kernel32" ( _
  7.         ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
  8.  
  9.      Public Declare Function CloseHandle Lib "Kernel32.dll" _
  10.         (ByVal Handle As Long) As Long
  11.  
  12.      Public Declare Function OpenProcess Lib "Kernel32.dll" _
  13.        (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
  14.            ByVal dwProcId As Long) As Long
  15.  
  16.      Public Declare Function EnumProcesses Lib "psapi.dll" _
  17.         (ByRef lpidProcess As Long, ByVal cb As Long, _
  18.            ByRef cbNeeded As Long) As Long
  19.  
  20.      Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
  21.         (ByVal hProcess As Long, ByVal hModule As Long, _
  22.            ByVal ModuleName As String, ByVal nSize As Long) As Long
  23.  
  24.      Public Declare Function EnumProcessModules Lib "psapi.dll" _
  25.         (ByVal hProcess As Long, ByRef lphModule As Long, _
  26.            ByVal cb As Long, ByRef cbNeeded As Long) As Long
  27.  
  28.      Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
  29.         ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
  30.  
  31.      Public Declare Function GetVersionExA Lib "kernel32" _
  32.         (lpVersionInformation As OSVERSIONINFO) As Integer
  33.  
  34.      Public Type PROCESSENTRY32
  35.         dwSize As Long
  36.         cntUsage As Long
  37.         th32ProcessID As Long
  38.         th32DefaultHeapID As Long
  39.         th32ModuleID As Long
  40.         cntThreads As Long
  41.         th32ParentProcessID As Long
  42.         pcPriClassBase As Long
  43.         dwFlags As Long
  44.         szExeFile As String * 260
  45.      End Type
  46.  
  47.      Public Type OSVERSIONINFO
  48.         dwOSVersionInfoSize As Long
  49.         dwMajorVersion As Long
  50.         dwMinorVersion As Long
  51.         dwBuildNumber As Long
  52.         dwPlatformId As Long
  53.  
  54.  
  55.         szCSDVersion As String * 128
  56.      End Type
  57.  
  58.      Public Const PROCESS_QUERY_INFORMATION = 1024
  59.      Public Const PROCESS_VM_READ = 16
  60.      Public Const MAX_PATH = 260
  61.      Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
  62.      Public Const SYNCHRONIZE = &H100000
  63.  
  64.      Public Const PROCESS_ALL_ACCESS = &H1F0FFF
  65.      Public Const TH32CS_SNAPPROCESS = &H2&
  66.      Public Const hNull = 0
  67.  
  68.      Function StrZToStr(s As String) As String
  69.         StrZToStr = Left$(s, Len(s) - 1)
  70.      End Function
  71.  
  72.      Public Function getVersion() As Long
  73.         Dim osinfo As OSVERSIONINFO
  74.         Dim retvalue As Integer
  75.         osinfo.dwOSVersionInfoSize = 148
  76.         osinfo.szCSDVersion = Space$(128)
  77.         retvalue = GetVersionExA(osinfo)
  78.         getVersion = osinfo.dwPlatformId
  79.      End Function
  80.  
  81. 'en el form (lleva un listbox y un  buttom)
  82.  
  83. Option Explicit
  84.  
  85.  
  86.  
  87.      Private Sub cmdEnum_Click()
  88.      List1.Clear
  89.  
  90.      Select Case getVersion()
  91.  
  92.  
  93.  
  94.      Case 1
  95.  
  96.         Dim f As Long
  97.         Dim sname As String
  98.         Dim hSnap As Long
  99.         Dim proc As PROCESSENTRY32
  100.         hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  101.         If hSnap = hNull Then Exit Sub
  102.         proc.dwSize = Len(proc)
  103.  
  104.         f = Process32First(hSnap, proc)
  105.         Do While f
  106.           sname = StrZToStr(proc.szExeFile)
  107.           List1.AddItem sname
  108.           f = Process32Next(hSnap, proc)
  109.         Loop
  110.  
  111.      Case 2
  112.         Dim cb As Long
  113.         Dim cbNeeded As Long
  114.         Dim NumElements As Long
  115.         Dim ProcessIDs() As Long
  116.         Dim cbNeeded2 As Long
  117.         Dim NumElements2 As Long
  118.         Dim Modules(1 To 200) As Long
  119.         Dim lRet As Long
  120.         Dim ModuleName As String
  121.         Dim nSize As Long
  122.         Dim hProcess As Long
  123.         Dim i As Long
  124.  
  125.         cb = 8
  126.         cbNeeded = 96
  127.         Do While cb <= cbNeeded
  128.            cb = cb * 2
  129.            ReDim ProcessIDs(cb / 4) As Long
  130.            lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
  131.         Loop
  132.         NumElements = cbNeeded / 4
  133.  
  134.         For i = 1 To NumElements
  135.  
  136.            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
  137.               Or PROCESS_VM_READ, 0, ProcessIDs(i))
  138.  
  139.            If hProcess <> 0 Then
  140.  
  141.                lRet = EnumProcessModules(hProcess, Modules(1), 200, _
  142.                                             cbNeeded2)
  143.  
  144.                If lRet <> 0 Then
  145.                   ModuleName = Space(MAX_PATH)
  146.                   nSize = 500
  147.                   lRet = GetModuleFileNameExA(hProcess, Modules(1), _
  148.                                   ModuleName, nSize)
  149.                   List1.AddItem Left(ModuleName, lRet)
  150.                End If
  151.            End If
  152.  
  153.         lRet = CloseHandle(hProcess)
  154.         Next
  155.  
  156.      End Select
  157. End Sub


En línea

LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: Matar un proceso concreto sabiendo su ruta
« Respuesta #2 en: 22 Marzo 2008, 19:58 pm »

hola fijate en este ejemplo de Cobein podes saber cual es el path de cada proseso

http://www.uploadsourcecode.com.ar/d/wsfQSMOs0JesYOWh2MD9BxdhzHKbBYi3

Saludos
En línea

rantam

Desconectado Desconectado

Mensajes: 5


Ver Perfil
Re: Matar un proceso concreto sabiendo su ruta
« Respuesta #3 en: 22 Marzo 2008, 20:46 pm »

Muxas gracias EON y Leandro! pero yo la ruta del proceso ya la se, lo q necesito es matarla de alguna manera...
osea q yo llame a una funcion diciendole:
mata ("c:\prueba2\hola.exe")


Private sub mata (ruta as string)
...
....codigo chungo q mata el proceso....
.....
end sub
En línea

LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: Matar un proceso concreto sabiendo su ruta
« Respuesta #4 en: 23 Marzo 2008, 00:33 am »

hola poreso mismo si sabes la ruta de los procesos podes hacer una comparacion si es igual al que queres matar.

te paso un ejemplo para un modulo con un rejunte de codigos (puede que aya cosas con distitnas apis y el mismo fin.)

tiene tres funciones, para tu caso la tercera

call KillProcessByName ("Calc.exe")
call KillProcessByID (idproceso)
call KillProcessByPath (C:\WINDOWS\system32\notepad.exe)

Código:
Option Explicit
Private Declare Function EnumProcesses Lib "PSAPI.DLL" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (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 ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Any, ReturnLength As Any) As Long


Private Const PROCESS_VM_READ As Long = (&H10)
Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)

Const MAX_PATH& = 260


Private Type LUID
   lowpart As Long
   highpart As Long
End Type

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

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const PROCESS_ALL_ACCESS = &H1F0FFF

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 KillProcessByName(ProcessName As String) As Boolean
   Const TH32CS_SNAPPROCESS As Long = 2&
   Const PROCESS_ALL_ACCESS = 0
   Dim uProcess As PROCESSENTRY32
   Dim rProcessFound As Long
   Dim hSnapshot As Long
   Dim szExename As String
   Dim exitCode As Long
   Dim myProcess As Long
   Dim AppKill As Boolean
   Dim appCount As Integer
   Dim i As Integer
   On Local Error GoTo Finish
   appCount = 0
   
   uProcess.dwSize = Len(uProcess)
   hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
   rProcessFound = ProcessFirst(hSnapshot, uProcess)
   Do While rProcessFound
       i = InStr(1, uProcess.szexeFile, Chr(0))
       szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
       If Right$(szExename, Len(ProcessName)) = LCase$(ProcessName) Then
           
           appCount = appCount + 1
           myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
            If KillProcessByID(uProcess.th32ProcessID, 0) Then
                KillProcessByName = True
            End If

       End If
       rProcessFound = ProcessNext(hSnapshot, uProcess)
   Loop
   Call CloseHandle(hSnapshot)
   Exit Function
Finish:
    MsgBox "Error!"
End Function

Function KillProcessByID(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
    Dim hToken As Long
    Dim hProcess As Long
    Dim tp As TOKEN_PRIVILEGES
   

    If GetVersion() >= 0 Then

        If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
            GoTo CleanUp
        End If

        If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
            GoTo CleanUp
        End If

        tp.PrivilegeCount = 1
        tp.Attributes = SE_PRIVILEGE_ENABLED

        If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
            GoTo CleanUp
        End If
    End If

    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    If hProcess Then
        KillProcessByID = (TerminateProcess(hProcess, exitCode) <> 0)
        CloseHandle hProcess
    End If
   
    If GetVersion() >= 0 Then
        tp.Attributes = 0
        AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
       
CleanUp:
        If hToken Then CloseHandle hToken
    End If
   
End Function



Function KillProcessByPath(ByVal FileName As String) As Boolean
    Dim Array_Process() As Long
    Dim Buffer As String
    Dim i_Process As Long
    Dim ret As Long
    Dim Path As String
    Dim t_cbNeeded As Long
    Dim Handle_Process As Long
    Dim i As Long

    ReDim Array_Process(250) As Long
   
    ret = EnumProcesses(Array_Process(1), 1000, t_cbNeeded)

    i_Process = t_cbNeeded / 4

    For i = 1 To i_Process

            Handle_Process = OpenProcess(PROCESS_QUERY_INFORMATION + PROCESS_VM_READ, 0, Array_Process(i))
           
            If Handle_Process <> 0 Then
               
                Buffer = Space(255)
                ret = GetModuleFileNameExA(Handle_Process, 0, Buffer, 255)
                Path = Left(Buffer, ret)
           
            End If

            ret = CloseHandle(Handle_Process)

            If UCase(Path) = UCase(FileName) Then
                KillProcessByPath = KillProcessByID(Array_Process(i), 0)
            End If
    Next

End Function



« Última modificación: 23 Marzo 2008, 00:37 am por LeandroA » En línea

rantam

Desconectado Desconectado

Mensajes: 5


Ver Perfil
Re: Matar un proceso concreto sabiendo su ruta
« Respuesta #5 en: 23 Marzo 2008, 01:02 am »

OK!! :D muxas gracias!!!!! ya entiendo lo q me keriais decir...Primero q mire todos los procesos activos y cuando coincida con la ruta q haga un terminateprocess al handle del proceso.
la idea q tenia yo esq se podia matar con algun simple comando en plan:
taskill c:\......
pos eso q muxas gracias! ta solucionao :laugh:
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines