.NET callback function

zhaozj2021-02-16  76

[Source: .NET SDK Document]

The callback function is the code that helps the non-host DLL function to complete the task in the hosted application. The call to the callback function will be passed from the hosted application, indirectly passed to the hosting implementation indirectly through a DLL function. In a variety of DLL functions called in the platform call, some functions require the correct operation of the callback function in the hosted code. This topic will introduce the elements of the hosted function and explain how to implement the callback function and call the callback function from the managed code.

Callback function foundation

To call most DLL functions from the hosted code, create the managed definition of the function and then call the function. This process is relatively straightforward. To use a DLL function that requires a callback function, there will be some additional steps. First, you must check this function in the document to determine if the function needs a callback. Next, you must create a backup function in the hosted application. Finally, the DLL function is called and the pointer to the callback function is passed as a parameter. The following figure summarizes these steps.

Tune function and implementation

The callback function is ideal for use in the case of repeated execution tasks. Another common use is used with enumeration functions such as EnumfontFamilies, Enumprinters and EnumWindows in Win32 API. As shown in the following sections, the EnumWindows function will enumerate all existing windows on your computer, and call the callback function to perform a task for each window. Implement the following procedure below will indicate how managed applications use platform calls to output handle values ​​for each window on the local computer. In particular, the example will use the EnumWindows function to gradually browse the list list and use a managed callback function (named callback) to output the value of the window handle. Before you begin the callback function, check the signature of the EnumWindows function before you start implementation. Enumwindows has the following signature: BOOL EnumWindows (WndenumProc Lpenc, LParam LPARAM) indicates that one of the clues that require callbacks are the LPENUMFUNC parameter. If the parameter uses a pointer to the callback function, there is usually a combination of LP (long pointers) prefix with the FUNC suffix. See Microsoft Platform SDK for documents for Win32 functions. Create a managed callback function. This example declares a delegation type called Callback, which uses two parameters: hWnd and LPARAM. The first parameter is the handle of the window; the second parameter is defined by the application. In this release, these two parameters must be an integer. The callback function usually returns a non-zero value to represent success, returning zero means failure. This example explicitly sets the return value to True to continue enumeration. Create a delegate and pass it as a parameter to the EnumWindows function. Platform calls will automatically convert the commission to a common callback format. Make sure the garbage collector will not recycle the commission before the callback function completes its work. If the delegate is passed as a parameter, or the entrustted delegate is transmitted as a field in the structure, the delegate will not be recycled during the call. Therefore, as shown in the following enumeration example, the callback function completes its work before calling, without having to manage additional operations in the managed caller. However, if the callback function can be called after the call is returned, the managed caller must take corresponding measures to ensure that the commission will not be recycled until the callback function is completed. For more information on preventing garbage collection, see the interoP package processing with platform calls. Example [Visual Basic] Imports SystemImports System.Runtime.InteropServices

Public Delegate Function Callback (_HWND AS INTEGER, LPARAM AS INTEGER) AS Booleanpublic Class EnumReportApp

Declare Function Enumwindows LIB "User32" (_ x as callback, y as integer) AS integer

Public Shared Sub Main () ENUMWINDOWS (Addressof EnormReportApp.report, 0) End Sub 'Main

Public Shared Function Report (hwnd As Integer, lParam As Integer) _ As Boolean Console.Write ( "Window handle is") Console.WriteLine (hwnd) Return True End Function 'ReportEnd Class' EnumReportApp [C #] using System; using System. Runtime.InteropServices;

Public Delegate Bool Callback (int hwnd, int lparam);

Public class enumreportapp {

[DLLIMPORT ("User32")] Public Static Extern Int EnumWindows (Callback X, INT Y);

Public static void main () {callback mycallback = new callback (enumreportapp.report); Enumwindows (MyCallback, 0);}

Public Static Bool Report (int hwnd, int lparam) {console.write ("Window Handle IS"); console.writeLine (hwnd); return true;}}

转载请注明原文地址:https://www.9cbs.com/read-15002.html

New Post(0)