当前位置:网站首页>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 .
边栏推荐
- 如何迁移virtualbox 的虚拟机到hype-v
- Miscellaneous things
- haas506 2.0开发教程-高级组件库-modem.net(仅支持2.2以上版本)
- 杂七杂八的东东
- mysql 索引
- mysql 基础查询
- Cloud box is deeply convinced to create a smart dual-mode teaching resource sharing platform for Nanjing No. 1 middle school
- haas506 2.0開發教程-高級組件庫-modem.sms(僅支持2.2以上版本)
- system 权限程序不能访问sd卡问题
- centos7 mysql 记录
猜你喜欢

Easy EDA #学习笔记09# | ESP32-WROOM-32E模组ESP32-DevKitC-V4开发板 一键下载电路

cmder

mysql 基础查询

如何迁移virtualbox 的虚拟机到hype-v

WPF Command指令和INotifyPropertyChanged

994. rotten oranges - non recursive method

20220621 Dual Quaternion

haas506 2.0开发教程-hota(仅支持2.2以上版本)

Haas506 2.0 development tutorial -hota (only supports versions above 2.2)

Miscellaneous things
随机推荐
Concepts and differences of DQL, DML, DDL and DCL
2121. sum of intervals of the same elements - hash table method
疫情下的传媒产业,小程序生态驱动数字化转型探索
Cetos7 record
cetos7 记录
20220621 Three Conjugates of Dual Quaternions
idea自动生成serialVersionUID
haas506 2.0开发教程-高级组件库-modem.sim(仅支持2.2以上版本)
嵌入式实时系统线程的副作用
mars3d点线面的绘制和重置
20220621 Dual Quaternion
C# DPI适配问题
Copy and paste of idea without escape
mongodb 记录
haas506 2.0开发教程-高级组件库-modem.voiceCall(仅支持2.2以上版本)
swagger3整合oauth2 认证token
剑指 Offer 42. 连续子数组的最大和
2022年养老理财产品有哪些?风险小的
图解三次握手四次挥手,小白都能看懂
Functions and basic structure of CPU