none
Trying to enumerate Process.Handle RRS feed

  • Question

  • Hello,

    I am trying to enumerate handles for all running processes.
        (1) I call System.Diagnostics.Process.GetProcesses(".")
        (2) On each of these, I call Process.Handle

    When I call Process.Handle, it gives a Win32Exception.

    I'm running Vista.

    Any help is appreciated.

    Aaron

    ---

    Dim m_arrSysProcesses() As Process
    m_arrSysProcesses = Process.GetProcesses(
    ".")
    For Each SysProcess As Process In m_arrSysProcesses
        Try
            Dim handle As System.IntPtr = SysProcess.Handle
        Catch e As System.ComponentModel.Win32Exception
            ' See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
            Select Case e.NativeErrorCode
            Case ERROR_ACCESS_DENIED
                Console.WriteLine(
    "Permissions denied trying to read process handle")
                MsgBox(
    "Permissions denied trying to read process handle")
            End
            Case Else
                Console.WriteLine("Win32Exception encountered trying to read process handle")
            End
            End Select
        End Try
    Next
    Wednesday, November 5, 2008 11:53 PM

Answers

  • P/Invoke GetPerformanceInfo() from psapi.dll.  PERFORMANCE_INFORMATION.HandleCount tells you how many open handles are in use.  Not to put judgment on what you are trying to do, but running TaskMgr.exe and finding and uninstalling the trouble-maker would make more sense.  In TaskMgr, use View + Select Columsn, check Handle Count.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Wednesday, November 12, 2008 3:36 PM
    Thursday, November 6, 2008 5:03 PM
    Moderator

All replies

  • Some processes belong to other users (such as SYSTEM), and you will not be able to obtain a handle to them (even if you are administrator).

    (You can start Task Manager and see this in the User Name column in the Processes tab.)

    On my Vista machine, all of the errors encountered were ERROR_ACCESS_DENIED.  If your question is regarding your "Case Else", I could not reproduce.  In that case, what was the e.NativeErrorCode?
    Thursday, November 6, 2008 12:03 AM
  • You don't have permission to open process handles on system processes.  Nor would you have a legitimate reason to do so.  All of this is of course by design.
    Hans Passant.
    Thursday, November 6, 2008 12:04 AM
    Moderator
  • Thank you for verifying the issue!  On my machine it is always ERROR_ACCESS_DENIED.

    The goal is to predict when the system is about to run out of handle resources.

    I wonder how else to approach the issue...

    • Is there a way to read total handle counts for the system, instead of adding up handles from each process?
    • Could a device driver read system handles?
    • Is there a system counter that tells how much memory is available for Windows objects?

    From what I understand, there is a certain region of memory from which Windows objects are allocated, and when this memory is full the system fails to create new handles.

    I'll read more about this.  In the mean time, any additional help is appreciated.

    Aaron

    Thursday, November 6, 2008 4:48 PM
  • P/Invoke GetPerformanceInfo() from psapi.dll.  PERFORMANCE_INFORMATION.HandleCount tells you how many open handles are in use.  Not to put judgment on what you are trying to do, but running TaskMgr.exe and finding and uninstalling the trouble-maker would make more sense.  In TaskMgr, use View + Select Columsn, check Handle Count.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Wednesday, November 12, 2008 3:36 PM
    Thursday, November 6, 2008 5:03 PM
    Moderator