{"id":1304,"date":"2014-06-10T15:35:26","date_gmt":"2014-06-10T13:35:26","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=1304"},"modified":"2014-06-10T15:35:26","modified_gmt":"2014-06-10T13:35:26","slug":"getting-gdi-object-count-per-process-in-powershell","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2014\/06\/getting-gdi-object-count-per-process-in-powershell\/","title":{"rendered":"Getting GDI object count per process in Powershell"},"content":{"rendered":"<p>Ever wanted to query the GDI object count in PowerShell per process?<\/p>\n<p>Here&#8217;s how:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&quot;Number of GUI handles per process&quot;\r\n$sig = @'\r\n&#x5B;DllImport(&quot;User32.dll&quot;)]\r\npublic static extern int GetGuiResources(IntPtr hProcess, int uiFlags);\r\n'@\r\n\r\nAdd-Type -MemberDefinition $sig -name NativeMethods -namespace Win32\r\n\r\n$processes = &#x5B;System.Diagnostics.Process]::GetProcesses()\r\n&#x5B;int]$gdiHandleCount = 0\r\nForEach ($p in $processes)\r\n{\r\n    try{\r\n        $gdiHandles = &#x5B;Win32.NativeMethods]::GetGuiResources($p.Handle, 0)\r\n        $gdiHandleCount += $gdiHandles\r\n        $p.Name + &quot; : &quot; + $gdiHandles.ToString()   \r\n    }\r\n    catch {\r\n        #&quot;Error accessing &quot; + $p.Name\r\n    }\r\n}\r\n&quot;Total number of GDI handles &quot; + $gdiHandleCount.ToString()\r\n<\/pre>\n<p>Alternatively you can use Process Explorer from Microsoft (originally sys-internals) if it is installed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wanted to query the GDI object count in PowerShell per process? Here&#8217;s how: &quot;Number of GUI handles per process&quot; $sig = @&#8217; &#x5B;DllImport(&quot;User32.dll&quot;)] public static extern int GetGuiResources(IntPtr hProcess, int uiFlags); &#8216;@ Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32 &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2014\/06\/getting-gdi-object-count-per-process-in-powershell\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[284,240,120],"class_list":["post-1304","post","type-post","status-publish","format-standard","hentry","category-reference","tag-gdi","tag-powershell","tag-query"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=1304"}],"version-history":[{"count":1,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1304\/revisions"}],"predecessor-version":[{"id":1305,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1304\/revisions\/1305"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=1304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=1304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=1304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}