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
For Each proc In System.Diagnostics.Process.GetProcessesByName(sAp) ' this line excepts
If proc.Handle.ToString = sHandle Or proc.MainWindowHandle.ToString = sHandle Then
iRes = proc.Id
Exit For
End If
Next
Catch e As Win32Exception
Throw New Exception("AHA!!")
Finally
ExcelProcessID = iRes
End Try