EnumWindows

T

Trecius

Hello, Newsgroupians:

I've a question regarding the Win32 function EnumWindows. First, the
prototype for EnumWindows is as follows...

BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)

As such, I have declared the following...

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int EnumWindows(EnumWindowsProcDelegate lpEnumFunc,
IntPtr lParam];
private delegate int EnumWindowsProcDelegate(IntPtr hwnd, IntPtr lParam);

Now, I'd like to call the EnumWindows. When it finds a specific window I'm
looking for, I want the lParam to be the return value of that specific
window. Here's an example...

IntPtr hwnd = new IntPtr();
EnumWindows(new EnumWindowsProcDelegate(EnumWndProc), hwnd);
....

private static int EnumWndProc(IntPtr hwnd, IntPtr lParam)
{
// For simplicity, the first window I find will be set to the lParam
lParam = hwnd;
}

However, after calling EnumWindows(...), hwnd is always 0x00000000 after the
EnumWndProc. As such, I tried the GCHandle, but I still get no where. Does
anyone know what I can do to return a client handle in the lParam?

Thank you, all.


Trecius
 
N

Nicholas Paldino [.NET/C# MVP]

Trecius,

I would use an anonymous method to do this, like so:

IntPtr value = IntPtr.Zero;
IntPtr hwnd = IntPtr.Zero;

EnumWindows(delegate(IntPtr hwnd, IntPtr lParam)
{
// Set the value variable based on your condition.
value = < whatever >;
}, hwnd);

Then, when you run EnumWindows, it will set the value variable, and you
can use it from there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top