tags:

views:

1157

answers:

2

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.

+10  A: 

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)

David Brown
Right. And this thread (http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/afa69b03-5425-4124-b9b4-f8c6cb9bcc9c) shows how to use it to get "next"
hometoast
Brilliant, thank you loads!
Timwi
A: 

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

Richard Szalay