当前位置:网站首页>Detailed explanation of callback function

Detailed explanation of callback function

2022-06-23 06:54:00 zetor_ major

explain 1


callback The original meaning of this word is that the caller is not in , In some way, let that person call you back when he is here .

 

void f() { ... } Call this function ... It has nothing to do with callbacks . Equivalent to A:"B, You do it f."

 

void f(int (*g)()) { ... g(); } This function has the characteristics of callback . It seems that A:"B, You do it f. Let me do it when I'm done g."

 

That is, the caller leaves a clue for the callee to notify him at a specific time , Carry on to him “ Callback ”. 

 

----

 

Then it said ,f This job requires you to do one thing after you finish it g. But this g Not at all f Self prescribed , Is in the call f I'll tell you when f Of . For example, you can let B When it's done , Do nothing ; Or call you ; Or let you do another thing . That is to say g The job itself is a variable .

 

----

 

If not provided as a parameter ,g became f A fixed part .A Direct said “ You do it f” No, that's it . The meaning of being a parameter lies in , This g You can change it at will . 

 

----

 

explain 2

 

First, make a figurative metaphor :

   You have a mission , But there are some things you can't do , Or unwilling to do , So let me help you with this part , You do your other tasks or wait for my news , But when I'm done, I'll let you know that I'm done , You can use it , How can I tell you ? You give me a cell phone , Let me call you when I'm done , I called you , You get my results and add them to your work , Continue to finish other work . Call me back , Mobile phone is my way to inform you , It is the callback function , It's also called a callback function

   The callback function is provided by the application to Windows System DLL Or other DLL Called function , Generally used to intercept messages 、 Get system information or handle asynchronous events . The application program tells the address pointer of the callback function DLL, and DLL This function will be called when appropriate . The callback function must comply with the pre-defined parameter format and transfer method , otherwise DLL Calling it will cause the program or system to crash . Usually , The callback function adopts the standard WindowsAPI How to invoke , namely __stdcall, Of course ,DLL The preparer can define the calling method by himself , But the client program must follow the same rules . stay __stdcall Under way , The parameters of the function are pushed onto the stack from right to left , Except that it is explicitly indicated as a pointer or reference , Parameters are passed by value , The function is responsible for popping parameters from the stack before returning .

   Understand callback functions !

   The program is calling a function (function) when ( Usually refers to api). It's like a program (program) call (Call) I have a function (function) The relationship is expressed as follows :

  call( call )

  program --------------------→ dll 

   When a program calls a function , When you pass the address of your own function as a parameter to the function called by the program ( This function is called a callback function ). A callback function is required DLL Functions are often functions that must repeat certain operations . The relationship is expressed as follows :

  call( call )

  program --------------------→ dll

  ↑ ¦

  ¦_______________________________¦

  callback( Callback )

   When the function you call passes the return value to the callback function , You can use the callback function to handle or complete certain operations . As for how to define your own callback function , With the specific use of API Function related , Many different types of callback functions have various parameters , For descriptions of these parameters, the parameters and return values of the callback function are generally described in the help . In fact, simply speaking, the callback function is after the function you write meets certain conditions , By DLL call !

   There is also such a saying ( Easier to understand ):

   The callback function is like an interrupt handler , The system automatically calls when the conditions you set are met . So , There are three things you need to do :

  1. Statement ;

  2. Definition ;

  3. Set trigger conditions , In your function, the name of your callback function is converted to the address as a parameter , For convenience DLL call . 

 

example

 

     The callback function is not called by you , It is the system that provides you with the ability to implement 、 To use . Where the callback function is called , When is it called , You don't know , You can only implement this function , As long as you implement this function , Your program will use this function , Ordinary functions will not work if they are not called in your program . For example, window procedure function , Your program does not call , But you need to implement this function , So after your program is executed , The window procedure function will work ; If you define a normal function fun, If your program does not call , So in your program fun This function will not work . 

 

The following example :

 

For example, you need to enumerate all the windows . You can call EnumWindows function . however ,EnumWindows Just enumerate the windows , As for how to deal with it, you need to define .

 

   That is to say , When you call EnumWindow, To specify a callback function , To tell EnumWindow, When she enumerates the windows , What should I do with this window .

 

   CallBack( HWND wnd ... ) //EnumWindow Callback function for

   {

        // Here is a custom definition

        // Suppose the purpose of the design is to find out that the title is “xxx” The window of ;

        CString str = GetWindowText( wnd );

        if( ! str.Compare( "xxx" ))

        {

            g_hWnd = wnd; // Save the handle to this window ;

            return TRUE;  // tell EnumWindow There is no need to continue enumerating .

        }

        else

            return FALSE; // tell EnumWindow, This time, we have enumerated

                          // Not what I'm looking for , You need to continue enumerating the next window .

   }

 

   ............ A code .............

   g_hWnd = NULL;    // Initialize the variable that holds the handle to the window you are looking for .

   EnumWindow(  CallBack ...... ); Call the enumeration function and specify the callback function .

   if( g_hWnd )

      // It means that we have found

   else

      // Still not found . 

原网站

版权声明
本文为[zetor_ major]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230537386469.html