Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GDI handles

315 views
Skip to first unread message

Dirk Abhau

unread,
Jan 13, 2010, 2:47:01 AM1/13/10
to
In taskmanager I see the amount of used GDI and user objects per process. How
can I achieve this using a powershell script on Windows XP using PowerShell
1.0?

The result should be a list with all process names, PIDs and amount of used
GDI objects.

I played with the following:
PS H:\> (Get-WmiObject -Class Win32_OperatingSystem -ComputerName
.).InvokeMethod("GetGuiResources",0)
Method invocation failed because
[System.Management.ManagementObject#root\cimv2\Win32_OperatingSystem] doesn't
contain a method named 'InvokeMethod'.
At line:1 char:74
+ (Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).InvokeMethod(
<<<< "GetGuiResources",0)

But with no success as you can see.

If this is not possible with Powershell, every way to get this information
using a Windows native way is fine. It needs to be automated without any user
interaction and I would like to avoid to write and compile some code for
this, that's all.

thanks

Thomas Lee

unread,
Jan 13, 2010, 9:14:59 AM1/13/10
to
In message <E971A397-4A4E-4A05...@microsoft.com>, Dirk
Abhau <Dirk...@discussions.microsoft.com> writes

>In taskmanager I see the amount of used GDI and user objects per process. How
>can I achieve this using a powershell script on Windows XP using PowerShell
>1.0?
>
>The result should be a list with all process names, PIDs and amount of used
>GDI objects.
>
>I played with the following:
>PS H:\> (Get-WmiObject -Class Win32_OperatingSystem -ComputerName
>.).InvokeMethod("GetGuiResources",0)
>Method invocation failed because
>[System.Management.ManagementObject#root\cimv2\Win32_OperatingSystem] doesn't
>contain a method named 'InvokeMethod'.
>At line:1 char:74
>+ (Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).InvokeMethod(
><<<< "GetGuiResources",0)
>
>But with no success as you can see.

Yes - there is no such method (GetGuiResources) on that class!

GetGuiResources is an unmanaged API and I'm not skilled enough (yet) to
know how to call this API natively - you need to use p/invoke.

>If this is not possible with Powershell, every way to get this information
>using a Windows native way is fine. It needs to be automated without any user
>interaction and I would like to avoid to write and compile some code for
>this, that's all.

You can use P/Invoke - but i can;t give you a good sample.

Thomas

--
Thomas Lee
doct...@gmail.com

Dirk Abhau

unread,
Jan 14, 2010, 5:33:01 AM1/14/10
to
I found a way to execute any C# code here:
http://blogs.msdn.com/powershell/archive/2006/04/25/583236.aspx

I think I can live with this, but if anybody knows a more simple way to get
the GDI handles from PowerShell, please post it.

"Thomas Lee" wrote:

> .....


>
> You can use P/Invoke - but i can;t give you a good sample.
>
> Thomas
>
> --
> Thomas Lee
> doct...@gmail.com

> .
>

Thomas Lee

unread,
Jan 15, 2010, 7:06:15 AM1/15/10
to
In message <9EC579F5-288F-41A8...@microsoft.com>, Dirk
Abhau <Dirk...@discussions.microsoft.com> writes

>I found a way to execute any C# code here:
>http://blogs.msdn.com/powershell/archive/2006/04/25/583236.aspx

Strictly speaking that's running C/C++ and unmanaged code.

PowerShell is built on top of .NET and calling into the .NET framework
is trivial in most cases. In effect, your PowerShell script is a bit of
managed code.

What you are asking is how to get into the unmanaged Win32 APIs. From
any bit of managed code (whether it's an actual C# program or a
PowerShell script, you have to use P/Invoke to get to that API. Its ugly
and is to be avoided if/where possible. This may be a case where you are
forced to use it.

>I think I can live with this, but if anybody knows a more simple way to get
>the GDI handles from PowerShell, please post it.

I will ask around, but I am not aware of any managed API you can call to
get this info.

Sorry I don't have a better answer.

Keith Hill

unread,
Jan 25, 2010, 12:07:00 AM1/25/10
to
If you are on Powershell 2.0 this works:

$sig = @'
[DllImport("User32.dll")]
public static extern int GetGuiResources(IntPtr hProcess, int uiFlags);
'@

Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
$handle = @(Get-Process -id $pid)[0].Handle
$numGuiHandles = [Win32.NativeMethods]::GetGuiResources($handle, 0)

"Number of GUI handles $numGuiHandles"

--
Keith

"Dirk Abhau" <Dirk...@discussions.microsoft.com> wrote in message
news:9EC579F5-288F-41A8...@microsoft.com...

0 new messages