We all know that in Microsoft. Net, C # is the first language used to develop network solutions. But in the fat client, C # also has outstanding performance. For programmers familiar with Win32 APIs, the key is how to integrate the new features of C # With Win32 APIs.
The following example shows how to call Win32 API-EnumWindows in C # to enumerate all windows:
1. First, you must declare a delegate function for the callback function of Win32 API-EnumWindows:
Public delegate bool CallBack (int hwnd, int lParam );
2. Then, use the platform CALL statement in C # To call API-EnumWindows from the USER32.DLL library. For detailed parameters, see MSDN-Win32 API.
[DllImport ("user32")]
Public static extern int EnumWindows (CallBack x, int y );
3. Finally, instantiate the delegate and call EnumWindows.
CallBack myCallBack = new CallBack (EnumWindowsApp. Report );
4. complete code is as follows:
Using System;
Using System. Runtime. InteropServices;
Public delegate bool CallBack (int hwnd, int lParam );
Public class EnumWindowsApp
{
[DllImport ("user32")]
Public static extern int EnumWindows (CallBack x, int y );
Public static void Main ()
{
CallBack myCallBack = new CallBack (EnumWindowsApp. Report );
EnumWindows (myCallBack, 0 );
}
Public static bool Report (int hwnd, int lParam)
{
Console. Write ("Window handle is :");
Console. WriteLine (hwnd );
Return true;
}
}
Note: For more information about "delegation", "platform call", and "instantiation", see Visual. Net Help.