GetProcessesByName() fails intermittantly with Access Denied

From: rox.scott (roxscott_at_discussions.microsoft.com)
Date: 12/23/04

  • Next message: Raghu: "COM (VB6) interop with .NET assemblies"
    Date: Thu, 23 Dec 2004 11:49:03 -0800
    
    

    I am trying to get the ProcessID of a process that I have just started.
    It is within ASP.NET, but with impersonation of non-Admin user.
    If I call 10 times in the same request, it succeeds about 80%.
    2 of the 10 will give System.ComponentModel.Win32Exception: "Access is denied"
    I saw something posted about needing to have perf counters enabled on any
    enumerated process, is this true ??
    My code goes something like this:
            sAp as String = "EXCEL" ' for example ...
            sHandle = FindWindow(sAp, sGuid).ToString
            Try
                Try
                    For Each proc In
    System.Diagnostics.Process.GetProcessesByName(sAp)
                        If proc.Handle.ToString = sHandle Or
    proc.MainWindowHandle.ToString = sHandle Then
                            iRes = proc.Id
                            Exit For
                        End If
                    Next
                Catch e As Exception ' if at first you don't succeed ...
                    System.Threading.Thread.Sleep(2000)
                    For Each proc In
    System.Diagnostics.Process.GetProcessesByName(sAp)
                        If proc.Handle.ToString = sHandle Or
    proc.MainWindowHandle.ToString = sHandle Then
                            iRes = proc.Id
                            Exit For
                        End If
                    Next
                End Try
            Catch e As Win32Exception
                Throw New Exception("AHA!!")
            Finally
                ExcelProcessID = iRes
            End Try


  • Next message: Raghu: "COM (VB6) interop with .NET assemblies"