The function pointer concept of c sharp delegate is explained in detail

  • 2020-04-02 01:41:54
  • OfStack

Principle:

1. Function pointer is actually the first address of the instruction encoded by the function in memory. In C++/C, this address can be used directly by the function name

When one function calls another, the called function is passed in as a function pointer

2, The technique used in the callback function callback is the function pointer:

The callback function is like an interrupt handler that the system calls automatically when the condition you set is met. To do this, you need to do three things:

1). Declaration;

2). Definition;

3). Set the trigger condition, which is to convert the name of your callback function into an address as an argument in your function, so that the DLL can be called easily.

Callbacks are functions that an application provides to Windows system DLLS or other DLLS and are typically used to intercept messages, retrieve system information, or handle asynchronous events. The application tells the DLL the address pointer to the callback function, and the DLL calls the function when appropriate. The callback function must conform to a predefined parameter format and method of passing, otherwise it will crash the program or system as soon as the DLL calls it. Typically, callbacks are called with standard WindowsAPI, i.e., s/s call. Of course, the DLL compiler can define the call itself, but the client must follow the same rules. In the successive stdcall mode, the parameters of the function are pushed into the stack from right to left. The parameters are passed by value except for explicitly indicating pointer or reference. The function is responsible for ejecting the parameters from the stack before returning.

3, C# implements the function pointer technique with the concept of delegate, and ent provides additional security and, of course, a loss of flexibility


Related articles: