Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Everyone!

I have a problem with parameters in EnumChildWindows, GCHandle, EnumWindow, EnumWindowProc. I wanna get the handles from an application and the findWindowEx works only on the first child window and I wanna get the second handle not the first.

I don't know what their parameters are except the handle (hwnd). Please help me to correctly declare / fill them.

Here is a code from pinvoke.net:


C#
using System.Runtime.InteropServices;

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

/// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</param>
/// <returns>List of child windows</returns>

public static List<IntPtr> GetChildWindows(IntPtr parent)
{
    List<IntPtr> result = new List<IntPtr>();
    GCHandle listHandle = GCHandle.Alloc(result);
    try
    {
        EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
        EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
    }
    finally
    {
        if (listHandle.IsAllocated)
            listHandle.Free();
    }
    return result;
}

/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</returns>
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
    GCHandle gch = GCHandle.FromIntPtr(pointer);
    List<IntPtr> list = gch.Target as List<IntPtr>;
    if (list == null)
    {
        throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
    }
    list.Add(handle);
    //  You can modify this to check to see if you want to cancel the operation, then return a null here
    return true;
}

/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
Posted
Updated 4-Mar-15 23:04pm
v3

1 solution

The information you want is all in the documentation. Start at https://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx[^].
 
Share this answer
 
Comments
Roland-HE-C# 5-Mar-15 4:21am    
I've read this earlier and I don't understand that because I can't add parameters to them.
You schould write me an example how to declare their parameters.
Please help.
Richard MacCutchan 5-Mar-15 5:07am    
What do you mean by that? The parameters are clearly explained in the documentation. You need to use the information in http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx and/or http://www.pinvoke.net/default.aspx/user32.EnumWindows to convert them for use by P/Invoke.
Roland-HE-C# 5-Mar-15 6:10am    
I've read them as I told you but I don't know how to give values to their parameters like EnumWindowsproc callback the visual basic indicates mistakes in the code.
Please present me these codes above in notepad or calculator. :))
You can't lose with it because you practise it and will remember later too. :))
Richard MacCutchan 5-Mar-15 6:20am    
The P/Invoke website provides examples, I am not going to duplicate what is already available.
Roland-HE-C# 5-Mar-15 11:52am    
Thanks, I now know how to work with the callback. :))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900