Function name as parameter passing and callback function

Source: Internet
Author: User

The member function name cannot be passed as a parameter, otherwise an error will occur because a this pointer is hidden inside

The callback function is written by you. You need to call another function, and one of the parameters of this function is your callback function name. In this way, the system will invoke the callback function you wrote when necessary, so that you can do what you want to do in the callback function.

Module A has a function foo, which passes the address of Foo to module B, and then, when an event occurs in B, calls Foo from the address of Foo passed from a, notifies a what happened, and makes a react accordingly. Then we call Foo the callback function.

My understanding: (Waiting to see)

Eg:s.dll has a function s_create, passing the address of the Servercallback function to the counter server, and then, after something has happened in the counter server, the address of the S_create function passed over the servercallback of S.dll. , call the Servercallback function, notify S.dll What's going on, let S.dll react

For example, the client makes a request to S.dll, and then s.dll a series of communication work, when S.dll call S_create, S.dll back to stop for a while, first to do another event, is to execute the Servercallback function (the function's pointer is a s_ Create Parameter form exists), Servercallback performs a variety of communication after the message processing is returned to S_create. Continue down

Example:

The callback function is a very useful and important concept. When an event occurs, the system or other function automatically calls a function that you define. Callback functions are used in many situations, such as hook callback functions: Mouseproc,getmsgproc and Enumwindows,drawstate callback functions, etc., there are many system-level callback procedures. This article is not intended to introduce these functions and procedures, but rather to talk about some of the experience of implementing your own callback functions.

The idea of using a callback function is to use VC and Delphi hybrid programming, a DLL program written by VC for some time long asynchronous work, after the completion of the work, you need to notify the application of the DLL: some events have been completed, please handle the next part of the event. began to think of using synchronization objects, file mapping, messages and other implementation of DLL functions to the application of notification, and then suddenly thought can be in the application side to write a function, and so on need to handle the subsequent matters, in the DLL directly call this function.

So he wrote a prototype of the callback function. Both in VC and Delphi.

One: Declares the callback function type.

VC version

typedef int (WINAPI *pfcallback) (int param1,int Param2);

Second: Declaring the callback function as a prototype

declaring function prototypes

VC version

int WINAPI cbfunc (int param1,int Param2);

Three: callback function call caller

Function calling callback function I put it in the DLL, which is a very simple VC-generated WIN32 DLL. and use DEF file to output its function name Testcallback. The implementation is as follows:

Pfcallback gcallback=0;

void WINAPI Testcallback (pfcallback Func)

{

if (func==null) return;

Gcallback=func;

DWORD threadid=0;

HANDLE hthread = CreateThread (null, NULL, THREAD1, LPVOID (0), &threadid);

Return

}

The work of this function saves the incoming pfcallback func parameter for use, and starts a thread. Declares a function pointer pfcallback gcallback to save the incoming function address.

Four: How the callback function is used:

After the Testcallback function is called, a thread is started, and as a demonstration, the thread is artificially deferred and the process of running the thread is printed on the screen.

Five: everything has

Using VC and Delphi to establish a project, write the implementation of the callback function part

VC version

int WINAPI cbfunc (int param1,int Param2)

{

int res= param1+param2;

TCHAR buffer[256]= "";

sprintf (Buffer, "callback result =%d", res);

MessageBox (Null,buffer, "testing", MB_OK); The demo callback function is called

return res;

}

Use the static connection method to connect the Exit function in the DLL Testcallback, add the Button in the project (for the Delphi project, you also need to put an edit control on the Form1, the default name is Edit1).

Response ButtonClick Event Call Testcallback

Testcallback (CBFUNC)//function parameter cbfunc is the address of the callback function

The function call returns immediately after the thread is created, and the application can do something else at the same time. Now you can see the display string on the screen, indicating that the thread created in the DLL is working properly. After a while, the thread delay part ends, the VC application pops up the MessageBox, indicating that the callback function is called and displays the result according to the param1,param2 operation, and the text in the Delphi program edit control is rewritten to PARAM1,PARAM2 Results of the operation.

It can be seen that the programming mode of using callback function may pass different callback function address according to different requirements, or define the prototype of various callback functions (also need to change the parameters and return value convention using callback function), implement various callback event processing, can make the control of the program flexibly changeable, is also a kind of high efficiency, A clear method of coupling between the program modules. Especially useful in some asynchronous or complex program systems-you can focus on the business processes and functions of the module core in a module (such as a DLL), and the extended function of the periphery gives only the interface of a callback function, by invoking the address of the callback function passed by other modules. Seamlessly hand-follow processing to another module as it is handled in a customized manner.

The example of this article uses a multithreaded delay in the DLL to call the callback function, just to highlight the effect of the callback function, in fact, as long as it is within the process, you can be happy with the function address passed to pass, as a callback function to use.

The principle of such a programming pattern is very simple: to call a function as a pointer to an address, there is nothing else complicated, just a little trick in programming. As to how the callback function pattern can benefit you, it depends on whether you use it or not, and how to use the programming pattern.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.