C # implement the API (DLL) callback function

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;

 

Namespace mydelegate
{
# Region delegate callback function

Public Delegate bool callback (INT hwnd, int lparam );

Public class enumreportapp
{
[Dllimport ("USER32")]
// The prototype of the Win32 function in user32.dll is:
// Bool enumwindows (wndenumproc lpenumfunc, lparam)
// One of the clues for callback of this function is that the lpenumfunc parameter exists. If the parameter uses a pointer to the callback function, the name usually contains a combination of LP (long pointer) prefix and func suffix.
// This function is introduced after user32.dll is imported here, but the first parameter has been passed into the delegate function callback defined here
Public static extern int enumwindows (callback X, int y );

Public static bool report (INT hwnd, int lparam)
{< br> console. write ("window handle is");
console. writeline (hwnd);
return true;
}

Public static void main ()
{
// Instantiate a delegate to implement the report defined above. That is, when mycallback is called, the report is called.
Callback mycallback = new callback (enumreportapp. Report );
// From this. netProgramTo call the Win32 function in user32.dll, but the function "calls back (callback)" in the. NET program just defined in mycallback
Enumwindows (mycallback, 0); // enumwindows is a winapi function, and mycallback points to the report method here.
Console. Readline ();
}
}

# Endregion
}

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.