Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use GetNextWindow() in C#?

Tags:

c#

windows

winapi

The Microsoft WinAPI documentation appears to suggest that user32.dll contains a function called GetNextWindow() which supposedly allows one to enumerate open windows in their Z order by calling this function repeatedly.

Pinvoke usually gives me the necessary DllImport statement to use WinAPI functions from C#. However, for GetNextWindow() it doesn't have an entry. So I tried to construct my own:

[DllImport("user32.dll")]
static extern IntPtr GetNextWindow(IntPtr hWnd, uint wCmd);

Unfortunately, when trying to call this, I get an EntryPointNotFoundException saying:

Unable to find an entry point named 'GetNextWindow' in DLL 'user32.dll'.

This seems to apply only to GetNextWindow(); other functions that are listed on Pinvoke are fine. I can call GetTopWindow() and GetWindowText() without throwing an exception.

Of course, if you can suggest a completely different way to enumerate windows in their current Z order, I'm happy to hear that too.

like image 852
Timwi Avatar asked Apr 28 '09 14:04

Timwi


2 Answers

GetNextWindow() is actually a macro for GetWindow(), rather than an actual API method. It's for backward compatibility with the Win16 API.

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

enum GetWindow_Cmd : uint {
    GW_HWNDFIRST = 0,
    GW_HWNDLAST = 1,
    GW_HWNDNEXT = 2,
    GW_HWNDPREV = 3,
    GW_OWNER = 4,
    GW_CHILD = 5,
    GW_ENABLEDPOPUP = 6
}

(From Pinvoke.net)

like image 169
David Brown Avatar answered Oct 24 '22 04:10

David Brown


GetNextWindow is a c++ macro that calls GetWindow, so you cannot call it from .NET. Call GetWindow instead.

From MSDN:

Using this function is the same as calling the GetWindow function with the GW_HWNDNEXT or GW_HWNDPREV flag set

like image 2
Richard Szalay Avatar answered Oct 24 '22 06:10

Richard Szalay