VC Delphi callback function description and examples and analysis-go

Source: Internet
Author: User

A callback function is a very useful and important concept.

When an event occurs, the system or other functions automatically call a function. Callback functions are widely used in Windows programming, such as hook callback functions: mouseproc, getmsgproc, enumwindows, and drawstate callback functions. There are also many system-level callback processes. This article will not introduce these functions and procedures, but will talk about some experiences in implementing your own callback functions.

The idea of using a callback function is generated because a DLL program written by VC is used for some long asynchronous work, applications that need to be notified to use dll: Some events have been completed. process the subsequent parts of the event. I first thought about how to use the synchronization object, file ing, messages, and other DLL functions to send notifications to the application. Then I suddenly thought that I could write a function at the application end, when you need to deal with subsequent issues, you can directly call this function in the DLL.

So I wrote the original form of a callback function. Both VC and Delphi have been tested.
I. Declare the callback function type.
VC version
Typedef int (winapi * pfcallback) (INT param1, int param2 );

Delph

Pfcallback = function (param1: integer; param2: integer): integer; stdcall;

In fact, it declares that the return value is int, and the input parameter is the pointer to the function of two int values.

Since the C ++ and Pascal compilers may have different processing methods for parameter input stack and function return, the function types are modified in a unified manner using winapi (winapi macro expansion is _ stdcall) or stdcall.

Ii. Declare the original form of the callback function

Declared function prototype

VC version

Int winapi cbfunc (INT param1, int param2 );

Delphi Edition

Function cbfunc (param1, param2: integer): integer; stdcall;
The above functions are global functions. If you want to use a function in a class as the original form of the callback function, declare the function as a static function.

Iii. Callback Function caller
I put the callback function into the DLL. This is a very simple Win32 DLL generated by VC and outputs its function name using the def file.
Testcallback. The implementation is as follows:

Pfcallback gcallback = 0;

Void winapi testcallback (pfcallback func)

{

If (func = NULL) return;

Gcallback = func;

DWORD threadid = 0;

Andle hthread = createthread (

Null,

Null,

Thread1,

Lpvoid (0 ),

& Threadid

);

Return;

}

This function saves the input pfcallback func parameter for use and starts a thread. Declares a function pointer pfcallback.
Gcallback saves the input function address.

4. How to Use the callback function:

After the testcallback function is called, a thread is started. As a demonstration, the thread manually delays processing and prints the thread running process on the screen.

The code of this thread is also implemented in the DLL project.

 

Ulong winapi thread1 (lpvoid PARAM)

{

Tchar buffer [256];

HDC = getdc (hwnd_desktop );

Int step = 1;

MSG;

DWORD starttick;

// A delay Loop

For (; Step <200; Step ++)

{

Starttick = gettickcount ();

/* This section refers to the part of the thread's running time for the system to process other transactions */

For (; gettickcount ()-starttick <10 ;)

{

If (peekmessage (& MSG, null, 0, 0, pm_noremove ))

{

Translatemessage (& MSG );

Dispatchmessage (& MSG );

}

}

/* Print the running state to the desktop. This is the favorite task of vcbear debugging */

Sprintf (buffer, "Running % 04d", step );

If (HDC! = NULL)

Textout (HDC, 30, 50, buffer, strlen (buffer ));

}

/* Call the callback function after a delay period */

(* Gcallback) (step, 1 );

/* End */

: Releasedc (hwnd_desktop, HDC );

Return 0;

}

5. Everything is ready

A project is created using VC and Delphi, and the Implementation part of the callback function is written.

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); // demonstrate that the callback function is called

Return res;

}

Delphi Edition

Function cbfunc (param1, param2: integer): integer;

Begin

Result: = param1 + param2;

Tform1.edit1. Text: = inttostr (result); // demo callback function called

End;

Use the static connection method to connect to the exit function testcallback In the DLL, and add the button (
For Delphi projects, you also need to put an edit control on form1. The default name is edit1 ).

Call testcallback in response to the buttonclick event

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

The function call will return immediately after the thread is created, and the application can do other things at the same time. Now we can see that the screen keeps displaying strings, indicating that the threads created in the DLL are running normally. After a while, the thread delay ends. The MessageBox pops up in the VC application, indicating that the callback function is called and the result of param1 and param2 operations is displayed, the text in the edit control of the Delphi Program is rewritten to param1, param2
.

It can be seen that different callback function addresses can be transferred according to different requirements using the callback function programming mode, you can also define the prototype of various callback functions (you also need to change the parameters and return value conventions of the callback function) to handle multiple callback events, so that program control is flexible and variable, it is also an efficient and clear coupling method between program modules. Especially useful in some asynchronous or complex program systems
--
You can concentrate on implementing the core business processes and technical functions of the module in a module (such as DLL). The extended functions of the peripheral only provide one callback function interface, by calling the callback function address passed by other modules, the subsequent processing is seamlessly handed over to another module and processed as needed.

The example in this article uses the method of calling the callback function after the multi-thread delay in the DLL, just to highlight the effect of the callback function, as long as it is within the process, you can use the function address as the callback function.

The principle of such a programming mode is very simple and simple: the function is also called as a pointer and an address. There is nothing complicated, just a small skill in programming. As for how much benefit the callback function mode can bring to you, it depends on whether you use it and how to use this programming mode.

Http://www.cnblogs.com/shilf/archive/2010/06/03/1750439.html

Related Article

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.