How can I use GetNextWindow() in C#?
Solution 1
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)
Solution 2
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
ssss
Expert in: C#, .NET/IL, JavaScript, HTML/CSS, Unicode; extensive experience with: SQL, Perl, PHP, Delphi, protocols such as HTTP, SMTP, etc.; less experience with: Java, C/C++
Updated on June 13, 2022Comments
-
ssss almost 2 years
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, forGetNextWindow()
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 callGetTopWindow()
andGetWindowText()
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.
-
hometoast about 15 yearsRight. And this thread (social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/…) shows how to use it to get "next"
-
ASA about 10 yearsWhat's the point of GetNextWindow() besides confusing poor pinvokers if it takes the same arguments as GetWindow() then?
-
David Brown about 10 yearsIt's included for backwards compatibility with old code. As for why they chose to implement it using a macro, I would guess that it's to eliminate an intermediate function call (back when compilers weren't as good at optimization), but I don't know for sure. That's probably a good question for Raymond Chen.