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
}