Excerpted from: Programming Lovers Forum [http://www.programfan.com/]
The callback function is a very useful, and it is also very important concept. When a certain event occurs, the system or other function will automatically call a function of your defined. The callback function is used in Windows programming, such as the Hook callback function: MouseProc, getMsgProc, and EnumWindows, DrawState callback functions, there are many system-level callback processes. This article is not prepared to introduce these functions and processes, but talk about some experiences in implementing their own callback functions.
The reason for producing this callback function is because now use VC and Delphi hybrid programming, with a DLL program written by VC to make some time for more time, after work is completed, you need to notify the use of DLL applications: Some events Already completed, processes the subsequent part of the event. Start thinking about using synchronous objects, files, messages, etc. to implement DLL functions to the application, and then suddenly thinks that you can write a function in the DLL when you need to handle follow-up, etc. I.e.
So I started and wrote the original shape of a callback function. Testing in VC and Delphi
1: Declare the callback function type.
VC version
Typedef Int (WinAPI * PfCallback) (Int Param1, Int Param2);
Delph version
PfCallback = Function (param1: integer; param2: integer): integer; stdcall;
In fact, a pointer to the pointing function of the return value is INT and the incoming parameter is two int.
Since the C and Pascal compilers may be inconsistent with the processing returned by the parameters and the function, the function type uses WinAPI (WinAPI Macro Exhibition is __stdcall) or STDCALL unified modification.
Two: Statement of the callback function
Declaration function
VC version
Int WinAPI CBFUNC (Int Param1, Int Param2);
Delphi version
Function CBFUNC (Param1, Param2: Integer): Integer; stdcall;
The above function is a global function. If you want to use a function in a class as a callback function, declare the class function as a static function.
Three: Tune function call caller
The function of calling the callback function I put it in the DLL, which is a very simple VC generated Win32 DLL. And use the 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 waiting and start a thread. Declare a function pointer PFCallback GCallback saves the incoming function address.
Four: How to use the callback function:
After the TestCallback function is called, a thread is launched, as a demonstration, the thread is delayed, and the process running the thread is on the screen.
The code for this segment thread is also implemented in the DLL project.
Ulong WinApi Thread1 (LPVOID param)
{
TCHAR BUFFER [256]; HDC HDC = Getdc (HWND_DESKTOP);
INT Step = 1;
MSG msg;
DWORD StartTick;
// a delay cycle
For (; Step <200; step )
{
Starttick = gettickcount ();
/ * This section is handed over part of the run time for the thread to allow the system to handle other transactions * /
FOR (; gettickcount () - StartTick <10;)
{
IF (PEEKMESSAGE (& MSG, NULL, 0, 0, PM_NOREMOVE))
{
TranslateMessage (& MSG);
DispatchMessage (& MSG);
}
}
/ * Print the running situation to the desktop, this is the most like-to-dry thing when the vcbear debugger * /
Sprintf (Buffer, "Running% 04D", STEP);
IF (hdc! = null)
Textout (HDC, 30, 50, Buffer, Strlen (buffer);
}
/ * Tune the callback function after delayed time * /
(* gcallback) (Step, 1);
/*end*/
:: ReleaseDC (hwnd_desktop, hdc);
Return 0;
}
Five: Everything has
Establish a project with VC and Delphi, write a callback function
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; // Demo the callback function is called
Return res;
}
Delphi version
Function CBFUNC (Param1, Param2: Integer): Integer;
Begin
Result: = Param1 param2;
TFORM1.EDIT1.TEXT: = INTSTOSTR (Result); / / Demo the callback function is called
END;
Use a static connection method to connect the exit function TestCallback in the DLL, add Button in the project (for Delphi project, you also need to put an Edit control on Form1, the default name Edit1).
Response ButtonClick event call TestCallback
TestCallback (CBFUNC) / / Function parameter cbfunc is the address of the callback function
The function calls to create a thread, return, and the application can do doing things 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 MessageBox, indicating that the callback function is called and displays the result of the PARAM1, PARAM2 operation, and the text in Delphi's program edit control is rewritten into param1, param2 operation. result.
It can be seen that the programming mode using the callback function can be transmitted according to different requirements, or the original shape of various callback functions can be defined (but also need to change the parameters of the callback function and the return value) to implement a variety of callback event processing. It can make the program's control flexible and more equivalent, a highly efficient, clear program module. Especially useful in some asynchronous or complex program systems - you can concentrate on the business process and technical features of the module core in a module (such as DLL), the external extension function gives only a callback function interface, by calling In the way, other modules pass the callback function address, seamlessly handle the subsequent processing to another module, handle it in a customized manner. This example uses the way the callback function is called after the multi-thread delay in the DLL, but in order to highlight the effect of the callback function, it can be delivered to the function address as long as it is in this process. Go, as a callback function is used.
This kind of programming mode is very simple. One: That is to show the function as a pointer an address to call, there is no other complicated thing, just a small skill in the programming. As for the callback function mode, how much benefits can you bring to you, just see if you are using, how to use this programming mode.