Visual C++ Callback Function Example

Imaging that your application need to load a DLL, which it could be your self-developed DLL or 3rd party DLL, and your application is dependent on some input or data from the DLL. Most DLL will provides APIs for calling the right function in order to execute certain processes to get the return value from the API.

Calling the DLL’s API periodically is expensive. You will need to add extra efforts to run the scheduler. Moreover, it doesn’t guarantee that the API will return the right value due to the unavailability of return data.

To reduce the overhead, we could create a callback function and register it to the DLL. In this case, we don’t have to create a scheduler to call the DLL’s API periodically. In fact, the DLL will call the callback function and trigger our application whenever the data is available.

This tutorial will guide you on how to create a callback function and register it to a DLL.

The Solution

Create a Win32 Console application project. Name the project “SampleCallback”. Make sure that the application type is console application.

Add a new Win32 Console application project to the same solution. Name the project “callbackproc”. Make sure that the application type is DLL.

callbackproc project

Open the callbackproc.h file and enter the following.

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#include <string>

#ifdef CALLBACKPROC_EXPORTS

#define CALLBACKPROC_API __declspec(dllexport)

#else

#define CALLBACKPROC_API __declspec(dllimport)

#endif

// our sample callback will only take 1 string parameter

typedefvoid(CALLBACK*fnCallBackFunc)(std::stringvalue);

// marked as extern "C" to avoid name mangling issue

extern"C"

{

//this is the export function for subscriber to register the callback function

CALLBACKPROC_API voidRegister_Callback(fnCallBackFunc func);

}

Open the callbackproc.cpp file and enter the following.

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#include "stdafx.h"

#include "callbackproc.h"

#include <sstream>

// This is an example of an exported function.

CALLBACKPROC_API voidRegister_Callback(fnCallBackFunc func)

{

intcount=0;

// let's send 10 messages to the subscriber

while(count<10)

{

// format the message

std::stringstream msg;

msg<<"Message #"<<count;

// call the callback function

func(msg.str());

count++;

// Sleep for 2 seconds

Sleep(2000);

}

}

CallbackSample project

Open the project properties page.

Make sure that the directory name “..\callbackproc” is in the C/C++->General->Additional Include Directories.

Make sure that the directory name “..\callbackproc\debug” is in the Linker->General->Additional Libraries Directories. I make debug build in this tutorial. You might need to change according to your selection.

Make sure that “callbackproc.lib” file is in the Linker->Input->Additional Dependencies

Open the CallbackSample.cpp file and enter the following.

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#include "stdafx.h"

#include <windows.h>

#include <string>

#include <windows.h>

#include <string>

#include "callbackproc.h"

// Callback function to print message receive from DLL

voidCALLBACK MyCallbackFunc(std::stringvalue)

{

printf("callback: %s\n",value.c_str());

}

int_tmain(intargc,_TCHAR*argv[])

{

// Register the callback to the DLL

Register_Callback(MyCallbackFunc);

return0;

}

Output

Be careful when your try to compile the projects. Make sure that callbackproc project is always get compile first. This is because CallbackSample project is dependent on it.

Compile CallbackSample project now and run it. You should observe the following output where the DLL will call the callback function for 10 times before it end the operation.