Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / Win32
Alternative
Tip/Trick

Use Lambda Expressions as Unmanaged Callbacks

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
5 Nov 2010CPOL 5.8K   1  
// Even more simplifiedclass Program{ [DllImport("user32", ExactSpelling = true)] static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam); [UnmanagedFunctionPointer(CallingConvention.Winapi)] delegate bool WNDENUMPROC(IntPtr hwnd, IntPtr lParam); ...
C#
// Even more simplified
class Program
{
    [DllImport("user32", ExactSpelling = true)]
    static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam);

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool WNDENUMPROC(IntPtr hwnd, IntPtr lParam);

    static void Main(string[] args)
    {
        List<IntPtr> windows = new List<IntPtr>();
        EnumWindows((hwnd, lParam) =>
        {
            windows.Add(hwnd);
            return true;
        }, IntPtr.Zero);
    }
}

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --