C# Delegates and Events—Win32 callbacks


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

C# Delegates and Events

edit

Win32 callbacks

The last use for delegates that we will cover is to provide a way for unmanaged Windows API functions to call back into your managed code. Some Windows API functions accept the address of an application-defined function as a parameter and then call that function to perform some application-specific processing. An example is the EnumChildWindows API function which enumerates all children of a parent window and invokes the application-defined callback function on each. To pass a function address from a C# program, we use a delegate.

The next example uses the EnumChildWindows API function to iterate through each of the top level windows on a system. Each window handle is passed to our application-defined callback method, WindowEnumProc, which prints the windows text to the console.

using System;
using System.Text;
using System.Runtime.InteropServices;
 
namespace enumchildren {
 
   class WindowEnumerator {
      // declare the delegate
      public delegate bool WindowEnumDelegate (IntPtr hwnd, 
                                               int lParam);
 
      // declare the API function to enumerate child windows
      [DllImport("user32.dll")]
      public static extern int EnumChildWindows(IntPtr hwnd,
                                                WindowEnumDelegate del, 
                                                int lParam);
 
      // declare the GetWindowText API function
      [DllImport("user32.dll")]
      public static extern int GetWindowText(IntPtr hwnd,
                                             StringBuilder bld, int size);
 
      static void Main(string[] args) {
         // instantiate the delegate
	 WindowEnumDelegate del 
            = new WindowEnumDelegate(WindowEnumProc);
			
	 // call the win32 function
	 EnumChildWindows(IntPtr.Zero, del, 0);
			
	 Console.WriteLine("Press enter to exit");
	 Console.ReadLine();
         }
 
      public static bool WindowEnumProc(IntPtr hwnd,int lParam) {
	 // get the text from the window
	 StringBuilder bld = new StringBuilder(256);
	 GetWindowText(hwnd, bld, 256);
	 string text = bld.ToString();
		
	 if(text.Length > 0) {
	    Console.WriteLine(text);
            }
	 return true;
	 }
      }
   }

This example defines the WindowEnumerator class which begins be defining a delegate named WindowEnumDelegate. I have declared this delegate to have a compatible signature with the type of callback function that the EnumChildWindows function is expecting. Next I import two Windows API functions from the user32.dll.

The Main method only does three things. It first instantiates the WindowEnumDelegate to call the WindowEnumProc method defined in the listing. Then, it calls the EnumChildWindows API function, passing the delegate as argument. For each window whose parent is the value NULL from C++ (which is IntPtr.Zero from C#), the system will invoke my delegate. Each time my WindowEnumProc is called, it receives the window handle of the current window as its first argument. It then calls GetWindowText to read the window’s title bar text into a buffer and display it to the console.


Previous_Page_.gif Next_Page_.gif


Personal tools