OBTENER ID DEL PROCESO

Iniciado por ciruit, 10 Diciembre 2005, 14:24 PM

0 Miembros y 1 Visitante están viendo este tema.

ciruit

Buenas
he intentado de varias formas obtener el id de un proceso (conociendo el nombre claro) pero no he dado con el resultado.
me podeis ayudar ?
gracias

germixxx


Chief

hola. mmmmmmmmm bueno lo que io hago (gracias a Slasher-k :P) es utilizar las APIs CreateToolhelp32Snapshot para crear un objeto SnapShot y despues con Process32First y Process32Next vas enumerando los procesos y podes obtener el PID y otras cosas mas.

Fijate en las chinchetas, hay en la de las faqs y preguntas mas frecuentes esas, hay un code que hizo Slasher-k sobre procesos, threads y demases :P hay usa todo eso.


Salu2!
sr. oscuro


ciruit


..::[ thekingkid ]::..

#4
la verdad esto es lo me sirve ami es un buen ejemplo descargado de la pagina de jose rubi (jejejeje)

------------------------------------------------------------------------
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID 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 Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
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 * 260
       End Type

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Const PROCESS_TERMINATE = &H1
Const PROCESS_CREATE_THREAD = &H2
Const PROCESS_VM_OPERATION = &H8
Const PROCESS_VM_READ = &H10
Const PROCESS_VM_WRITE = &H20
Const PROCESS_DUP_HANDLE = &H40
Const PROCESS_CREATE_PROCESS = &H80
Const PROCESS_SET_QUOTA = &H100
Const PROCESS_SET_INFORMATION = &H200
Const PROCESS_QUERY_INFORMATION = &H400
Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const SYNCHRONIZE = &H100000
Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'Para llenar el listbox con los procesos
Sub RellenaLista()
Dim hSnapShot As Long
Dim uProceso As PROCESSENTRY32
Dim res As Long

List1.Clear
hSnapShot = CreateToolhelpSnapshot(2&, 0&)
If hSnapShot <> 0 Then
   uProceso.dwSize = Len(uProceso)
   res = ProcessFirst(hSnapShot, uProceso)
   Do While res
      List1.AddItem Left$(uProceso.szExeFile, InStr(uProceso.szExeFile, Chr$(0)) - 1)
      List1.ItemData(List1.NewIndex) = uProceso.th32ProcessID
      res = ProcessNext(hSnapShot, uProceso)
      MsgBox (uProceso.szExeFile)
   Loop
   Call CloseHandle(hSnapShot) ' por cada proceso wey
End If

End Sub

'Para cerrar el proceso seleccionado
Private Sub Command1_Click()
Dim hProcess As Long, iResult As Long

If List1.ListIndex = -1 Then
    Beep
    MsgBox "Debe seleccionar una aplicación"
Else
    hProcess = OpenProcess(PROCESS_TERMINATE, True, List1.ItemData(List1.ListIndex))
    MsgBox hProcess
    iResult = TerminateProcess(hProcess, 99)
    MsgBox iResult
    CloseHandle hProcess
    Sleep (100)
    RellenaLista
End If
End Sub

Private Sub Form_Load()
Call RellenaLista
End Sub



si te sirve no hay tos
just wanna try

NYlOn


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
    MsgBox FindWindow(vbNullString, "Opera")
End Sub


Eso buscaria la ventana Opera
Si devuelve 0 es pq no encuentra la ventana.

Espero q sea lo q buscabas

Saluds.-

ZEALOT

hola a todos. por aqui ottra vez

solo una opinion: no confundan un handle de ventana con el ID de un proceso(PID), son 2 cosas muy distintas, el unico que mas se acerca a la respuesta es ..::[ thekingkid ]::..

saludos bye  ;)

ciruit

Estudiare ese código y les cuento que tal me fue por que ahora mis ojos no dan mas :(.
Saludos y gracias.

PD: Cualquier otro code se agradece.


-- Lo simple es bueno... --

kakinets

bueno espero que te sirva....


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
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
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 Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)

Private Sub Form_Load()
    Dim hSnapShot As Long, uProcess As PROCESSENTRY32
    hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
    uProcess.dwSize = Len(uProcess)
    R = Process32First(hSnapShot, uProcess)
    Me.AutoRedraw = True
    Do While R
        List1.AddItem Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
        R = Process32Next(hSnapShot, uProcess)
    Loop
    CloseHandle hSnapShot
End Sub


               Saludos KakiNets