Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: gera en 15 Agosto 2005, 04:17 am



Título: SUPRIMIR PROCESOS
Publicado por: gera en 15 Agosto 2005, 04:17 am
SUPRIMIR PROCESOS

Hola, Estoy necesitando un ejemplo vb que suprima un proceso indicado (Por ejemplo el word), Yo tengo un ejemplo pero lo unico que suprime so las aplicaciones.. Si me pueden ayudar con esto estoy muy agradecido. chau gracias


Título: Re: SUPRIMIR PROCESOS
Publicado por: Xpeed en 15 Agosto 2005, 05:23 am
puedes hacer un codigo simple, utilizando la instruccion shell y desde el cmd matar los procesos por el pid, para visualizarlos los podrias meter en un .txt  con el comando tasklist, y matarlos con el taskkill.. para que la aplicacion quede mas comoda, puedes meter el .txt a una lista y desde alli metes el pid que euieres acabar y listo...


un saludo.


Título: Re: SUPRIMIR PROCESOS
Publicado por: Slasher-K en 15 Agosto 2005, 23:30 pm
Se hace con TerminateProcess. Busca en el foro que hay ejemplos de código sobre eso.

Saludos.


Título: Re: SUPRIMIR PROCESOS
Publicado por: yeikos en 17 Agosto 2005, 13:03 pm
taskkill /f /pid ID


Título: Re: SUPRIMIR PROCESOS
Publicado por: NYlOn en 21 Agosto 2005, 00:24 am
aca t dejo el codigo:

1 form y 1 modulo

Controles:
-2 Command Buttons> cmdProcesos
                                 cmdKill
-1 List View> Lvw
-1 ChekBox> chkPreguntar

Codigo en el form:
Código:
Private Sub cmdKill_Click()
If chkPreguntar.Value = 1 Then
   If MsgBox("Esta seguro que desea terminar el proceso '" + Lvw.SelectedItem + "' ?", vbQuestion + vbYesNo) = vbYes Then
      KillProcess (Lvw.SelectedItem)
      cmdProcesos_Click
   End If
Else
   KillProcess (Lvw.SelectedItem)
   cmdProcesos_Click
End If
End Sub

Private Sub cmdProcesos_Click()
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Lvw.ListItems.Clear
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0)
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc)
i = 0
While theloop <> 0
exename = proc.szExeFile
ret = Lvw.ListItems.Add(, "first" & CStr(i), exename)
Lvw.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID
i = i + 1
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap
End Sub

Public Sub KillProcess(ByVal processName As String)
On Error GoTo ErrHandler
Dim oWMI
Dim ret
Dim sService
Dim oWMIServices
Dim oWMIService
Dim oServices
Dim oService
Dim servicename
Set oWMI = GetObject("winmgmts:")
Set oServices = oWMI.InstancesOf("win32_process")
For Each oService In oServices

servicename = LCase(Trim(CStr(oService.Name) & ""))

If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
ret = oService.Terminate
End If

Next

Set oServices = Nothing
Set oWMI = Nothing


ErrHandler:
Err.Clear
End Sub

Private Sub Form_Load()
Dim header As ColumnHeader
Lvw.View = lvwReport
Lvw.ColumnHeaders.Clear
Set header = Lvw.ColumnHeaders.Add(, "first", "Process", 2000)
Set header = Lvw.ColumnHeaders.Add(, "second", "ID", 950)
Lvw.Refresh
End Sub

Private Sub Form_Resize()
Lvw.Height = Me.Height - 500
Lvw.Width = Me.Width - 1575
cmdKill.Left = Lvw.Width + 100
cmdProcesos.Left = Lvw.Width + 100
End Sub

Codigo en el Modulo:
Código:
Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPheaplist = &H1
Public Const TH32CS_SNAPthread = &H4
Public Const TH32CS_SNAPmodule = &H8
Public Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Public Const MAX_PATH As Integer = 260


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

espero q te sirva

ak te dejo el codigo mas resumido, x si keres que termine un proceso concreto:
'--------------------------------------------------------------------
Private Sub Command1_Click()
KillProcess ("winword.exe") 'termina el proceso del Word
End Sub

Public Sub KillProcess(ByVal processName As String)
On Error GoTo ErrHandler
Dim oWMI
Dim ret
Dim sService
Dim oWMIServices
Dim oWMIService
Dim oServices
Dim oService
Dim servicename
Set oWMI = GetObject("winmgmts:")
Set oServices = oWMI.InstancesOf("win32_process")
For Each oService In oServices

servicename = LCase(Trim(CStr(oService.Name) & ""))

If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
ret = oService.Terminate
End If

Next

Set oServices = Nothing
Set oWMI = Nothing


ErrHandler:
Err.Clear
End Sub
'----------------------------------------------------------------------

PD: todavia no consigo que tire un msgbox cuando no lo puede cerrar


un abrazo ;)


Título: Re: SUPRIMIR PROCESOS
Publicado por: Slasher-K en 21 Agosto 2005, 01:32 am
Para verificar si sigue activo usa la función GetExitCodeProcess (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getexitcodeprocess.asp) pasandole el handle al proceso que querés verificar, si el código de salida es STILL_ACTIVE (259) significa que no lo pudo cerrar.

Saludos.


Título: Re: SUPRIMIR PROCESOS
Publicado por: NYlOn en 21 Agosto 2005, 04:11 am
muchas gracias ;)

ahora pruebo a ver q onda  ;D

un abraz0 ;)