I’m working on an app that will monitor the number of GDI Objects of another process (in this case, the spooler)
To do this in VB.NET requires an API call to GetGuiResources. While testing, I was using Process.GetProcesses to get a list of all the available processes, passed the handle of each process to the API function, and writing the result to a textbox.
Problem was, all process that I did not own throw security errors, even when I set the app to run with elevated privs. The internet once again failed me, and I stumbled across the solution while browsing the system.diagnostics.process documentation on MSDN.
The solution is to call Process.EnterDebugMode() before getting your process list and calling the API, then calling Process.LeaveDebugMode() when you are done.
Here is an example. remember to run with elevate privs, and create a multiline textbox called txtOut.
Public Class Form1 Declare Function GetGuiResources Lib "user32" (ByVal hProcess As Long, ByVal uiFlags As Long) As Long ' uiFlags: 0 - Count of GDI objects ' uiFlags: 1 - Count of USER objects Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click Dim procList As Process() Dim TYPE_GDI As UInteger = 0 Process.EnterDebugMode() procList = Process.GetProcesses For Each proc As Process In procList Dim objCnt As Long Try objCnt = GetGuiResources(proc.Handle, TYPE_GDI) Catch ex As Exception objCnt = -1 End Try txOut.AppendText(proc.ProcessName & vbTab & "GDI: " & objCnt.ToString & vbNewLine) Next proc Process.LeaveDebugMode() End Sub End Class