There is a good rule: NEVER typecast function pointers. Will save you from
many subtle errors.
Warren Tang wrote:
The question is, why "CALLBACK" is a necessity. Why must I use it in a
callback function?
You don't. The important thing is that both caller and callee agree on a
calling convention.
#include <string>
#include <iostream>
#include <ostream>
// declare function type with cdecl
typedef std::string __cdecl function(std::string str);
// define function with stdcall
std::string __stdcall times2( std::string str) {
return str+str;
}
int main() {
// Note: the cast is necessary to compile this, otherwise
// the compiler rightfully complains.
function* pfn = (function*)×2;
std::cout << "res=" << (*pfn)("Fou!") << std::endl;
}
If you run the above program in a debugger, you should get a debug
assertion
because the function was called with the incorrect calling convention.
Maybe you have to switch the two calling conventions around, I haven't
actually tested this.
As far as the win32 API is concerned, most function declarations in the
header files include the calling conventions so that changing the default
calling convention doesn't change their meaning.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschaftsfuhrer: Thorsten Focking, Amtsgericht Hamburg HR B62 932