Autor
|
Tema: Matar un proceso concreto sabiendo su ruta (Leído 2,169 veces)
|
rantam
Desconectado
Mensajes: 5
|
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
|
|
|
|
~~
|
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: 'en un modulo Public Declare Function Process32First Lib "kernel32" ( _ ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Public Declare Function Process32Next Lib "kernel32" ( _ ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Public Declare Function CloseHandle Lib "Kernel32.dll" _ (ByVal Handle As Long) As Long Public Declare Function OpenProcess Lib "Kernel32.dll" _ (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _ ByVal dwProcId As Long) As Long Public Declare Function EnumProcesses Lib "psapi.dll" _ (ByRef lpidProcess As Long, ByVal cb As Long, _ ByRef cbNeeded As Long) As Long Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _ (ByVal hProcess As Long, ByVal hModule As Long, _ ByVal ModuleName As String, ByVal nSize As Long) As Long Public Declare Function EnumProcessModules Lib "psapi.dll" _ (ByVal hProcess As Long, ByRef lphModule As Long, _ ByVal cb As Long, ByRef cbNeeded As Long) As Long Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _ ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Public Declare Function GetVersionExA Lib "kernel32" _ (lpVersionInformation As OSVERSIONINFO) As Integer 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 OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type Public Const PROCESS_QUERY_INFORMATION = 1024 Public Const PROCESS_VM_READ = 16 Public Const MAX_PATH = 260 Public Const STANDARD_RIGHTS_REQUIRED = &HF0000 Public Const SYNCHRONIZE = &H100000 Public Const PROCESS_ALL_ACCESS = &H1F0FFF Public Const TH32CS_SNAPPROCESS = &H2& Public Const hNull = 0 Function StrZToStr(s As String) As String StrZToStr = Left$(s, Len(s) - 1) End Function Public Function getVersion() As Long Dim osinfo As OSVERSIONINFO Dim retvalue As Integer osinfo.dwOSVersionInfoSize = 148 osinfo.szCSDVersion = Space$(128) retvalue = GetVersionExA(osinfo) getVersion = osinfo.dwPlatformId End Function 'en el form (lleva un listbox y un buttom) Option Explicit Private Sub cmdEnum_Click() List1.Clear Select Case getVersion() Case 1 Dim f As Long Dim sname As String Dim hSnap As Long Dim proc As PROCESSENTRY32 hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) If hSnap = hNull Then Exit Sub proc.dwSize = Len(proc) f = Process32First(hSnap, proc) Do While f sname = StrZToStr(proc.szExeFile) List1.AddItem sname f = Process32Next(hSnap, proc) Loop Case 2 Dim cb As Long Dim cbNeeded As Long Dim NumElements As Long Dim ProcessIDs() As Long Dim cbNeeded2 As Long Dim NumElements2 As Long Dim Modules(1 To 200) As Long Dim lRet As Long Dim ModuleName As String Dim nSize As Long Dim hProcess As Long Dim i As Long cb = 8 cbNeeded = 96 Do While cb <= cbNeeded cb = cb * 2 ReDim ProcessIDs(cb / 4) As Long lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded) Loop NumElements = cbNeeded / 4 For i = 1 To NumElements hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _ Or PROCESS_VM_READ, 0, ProcessIDs(i)) If hProcess <> 0 Then lRet = EnumProcessModules(hProcess, Modules(1), 200, _ cbNeeded2) If lRet <> 0 Then ModuleName = Space(MAX_PATH) nSize = 500 lRet = GetModuleFileNameExA(hProcess, Modules(1), _ ModuleName, nSize) List1.AddItem Left(ModuleName, lRet) End If End If lRet = CloseHandle(hProcess) Next End Select End Sub
|
|
|
En línea
|
|
|
|
|
rantam
Desconectado
Mensajes: 5
|
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
|
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) 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
Mensajes: 5
|
OK!! 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
|
|
|
En línea
|
|
|
|
|
|