Call the Win32 function EnumWindows in C # to enumerate all windows

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.