Writing a callback [call back] function in C++/VC++ using windows API

One of the instances, your customers or yourself would be using call backs when you need to call a method asynchronously and receive the feedback.
When I tell asynchronously, it means like you want to read a huge file[> 100MB] and parse them. I know this consumes a huge amount of memory. Use file handles to go through the file little by little.
First of all lets define the callback. Its a typedef.

typedef void (*typeReadFileCallback) (string, string);

Over here, my method parameters are filename, and return output after reading from the file.
Next we need to declare a callback variable. All this can be done in the header file if needed.

typeReadFileCallback varCallback;

Go over to the cpp file. Next we need to create or spawn a new thread to set this callback.

//main method
//method to create the thread
//definition to create thread
(LPTHREAD_START_ROUTINE)Callback_help, //callbac_help is the name of the method to execute
//define the callback_help method
void callback_help()
{ //define the logic for reading the file

finally we need the user to use the fallback, which is done as below:
the user defines a method to use the callback.
for eg:

void testcallback()
print("Callback is called");
//user's main program

whenever the program finishes executing the method, the call back is called.
leave your comments, if your confused.

Leave a Reply

Your email address will not be published. Required fields are marked *