Click to See Complete Forum and Search --> : CALLBACK Phenomenom


May 17th, 1999, 01:16 PM
Hi All!

I wish to know what exactly are those callback functions. I've seen so much of it, esp. with Tree/ListViews. Please explain it in lamens terms.

Will be very much appreciated.
Thank you.

Yours,
Anonymous.

Wayne Fuller
May 17th, 1999, 06:23 PM
If you are familiar with function pointers, that is all CALLBACK functions are. If you are not, then you need to be if you are going to use them.

A CALLBACK function is a function with particular parmeters that you implement and pass the address to the function that needs the CALLBACK function.

For example

The following code uses a CALLBACK function of type FONTENUMPROC, and it calls this function until it returns a 0 or there are no more fonts that fit the particular catagory. This is a typical way of enumerating the fonts on a computer.

int CSomeClass::EnumerateFonts()
{
LOGFONT lf;
FONTENUMPROC pCallback;

HDC hDC = ::GetDC(NULL);
memset(&lf, 0x0, sizeof(LOGFONT));
lf.lfCharSet = DEFAULT_CHARSET;
pCallback = ::EnumFontsCallBack;

if ( !::EnumFontFamiliesEx(hDC, &lf, pCallback, (LPARAM) this, (DWORD) 0) )
{
return 0;
}

m_IsEnumerated = true;
// Set default font
CString str;

str.LoadString(IDS_DEFAULT_FONT);
GetFontIndex(str);

return 1;
}

static int AFXAPI EnumFontsCallBack(const LOGFONT *pLF, const TEXTMETRIC *pTM, DWORD fontType, LPARAM lParam)
{
// Do not support UNICODE
if ( pLF->lfCharSet > SYMBOL_CHARSET )
{
return 1;
}

...
Do Something with the font
...

return 1;
}




Wayne

May 18th, 1999, 02:48 PM
I would like to understand ueses for this callback functions. Why would someone use a callback function? Is there a substitute method
to use other than a callback function. May be the answer to this question help me understand callbacks.
Thank you

Wayne Fuller
May 18th, 1999, 03:48 PM
I am not very good at explaining but here goes.

Don't let the word CALLBACK confuse you. If you see the word just think function pointer. It is a windows defined term, that's all. As a matter of fact it is just defined as __stdcall in the windows header files.
If you are going to use a windows function that requires you to have a CALLBACK function, there is no workaround. The previous example is a good one to break apart. I call a function called ::EnumFontFamiles where in the third parameter it expects a CALLBACK function of type FONTENUMPROC. This means you would have to write a function that is of type FONTENUMPROC and pass the address to this function. The reason why you do this is because the ::EnumFontFamiles function will keep calling your function pointer until you send back a zero or it runs out of fonts. So in your function you can do whatever you want to with the fonts that come in. Put them in a list box or whatever.
It sounds to me you do not know why somebody would use function pointers. If this is not the case then disregard the last section.
I think I explain better with examples so bear with me.

The way I learned function pointers is in the following snipet of code:


#include <iostream.h>

// This function multiplies two integers
int Multiply(int x, int y)
{
return x * y;
}

// This function adds two integers
int Add(int x, int y)
{
return x + y;
}

// This function subtracts two integers
int Subtract(int x, int y)
{
return x - y;
}

// This is a function pointer that takes two integers
// and returns an int
int (*DoOperation) (int x, int y);

// I could also do this
//#define CALLBACK
//CALLBACK int (*DoOperation) (int x, int y);

void main(int argc, char *argv[])
{
int x = 5, y = 2;

// pass the address of the function Multiply
DoOperation = Multiply;
cout << DoOperation(x, y) << endl;

// pass the address of the function Add
DoOperation = Add;
cout << DoOperation(x, y) << endl;

// pass the address of the function Subtract
DoOperation = Subtract;
cout << DoOperation(x, y) << endl;
}




As you can see function pointers are extremely powerful. You can see that this is limitless. I hope this helps a little bit. The best way and I think the only way is to type away. It is one thing when somebody tells you how to do something, but another when you figure it out yourself.

Wayne