#1
  1. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792

    Finding all processes running on a machine


    Ok, I'm having a major brainfart because I should know this but for the life of me cannot remember it. What I need to do is to get a list of handles of all processes running on the machine. I know it will take an API call, but for the life of me I don't know what it is and am having problems finding it. Anyone know?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    No es importante
    Posts
    2,065
    Rep Power
    14
    Onslaught I am sure you have found it by now but just in case here you go.
    http://www.mentalis.org/apilist/EnumProcesses.shtml
  4. #3
  5. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Thanks for the link victorpendleton.
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Ok, this leads me to another question which I find myself pondering on. How do I get the process handle of the application that I am currently in? I know one easy solution would be to cycle through the processlist by name and get it by the application's name, but I cannot do that in this instance because there can be the possibility of multiple instances of this one application running at once.

    Let me break it down and give a little history of what is going on here. I am writing a suite of applications, one of which is an activex applications that will communicate with another computer on a wan via dcom. There will be multiple computers that run this component. It will start out at around 15 computers, but will greatly increase to 200 or so after a couple of months. When an instance of this application starts up, it creates a log file with some vital information of what it is doing just in case the application dies or the server dies or anything bad happens and causes the process not to be completed. One of the other applications in the suite will watch for the activex application. It will also look at the log files that have been written. If there is a log file out there but not an corresponding application then I will launch a third application that starts up a local instance of the activex application and finishes the process.

    So, in the activex application I must write out that instance's process handle so that it can be compared with the list of current processes running. I thought that maybe App.hInstance might be what I need, but this is not the case.

    Any ideas on how I could solve this delima?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    2
    Rep Power
    0

    Talking


    Try this one


    Public Function GetProcessHandle(strComputer As String) As Long
    Dim ObjProcessSet As Object
    Dim ObjThreadSet As Object
    Dim objThread As Object
    Dim objProcess As Object

    'Query the minmgmts to get the process id from the threadID Should only be one value
    Set ObjThreadSet = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2").ExecQuery("Select * FROM Win32_PerfRawData_PerfProc_Thread WHERE IDThread=" & CStr(App.ThreadID))

    For Each objThread In ObjThreadSet
    'Now Get the Handle for the process corresponding to this thread
    Set ObjProcessSet = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2").ExecQuery("Select * FROM Win32_Process WHERE ProcessID=" & objThread.Properties_("IDProcess"))
    For Each objProcess In ObjProcessSet
    'Return the Handle for this process
    GetProcessHandle = objProcess.Handle
    Next
    Next

    GoSub Cleanup
    Exit Function

    Cleanup:
    Set objProcess = Nothing
    Set ObjProcessSet = Nothing
    Set objThread = Nothing
    Set ObjThreadSet = Nothing
    Return

    End Function
  10. #6
  11. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Thanks for the post gthomso1, I did however already solve this problem with the help of the link that victor posted and the help of some of our members through the C/C++ board.

    To get the process id of the application itself you can use:
    Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    2
    Rep Power
    0
    No problem I though there would have to be a more elegant way of doing it

IMN logo majestic logo threadwatch logo seochat tools logo