Re: function pointer help!



"Ron H." <rnh@xxxxxxx> wrote in message news:473b2e5f$0$7573$8d2e0cab@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I can't get this function pointer to work! Here's the scoop: I am using a
third party I/O board. The manufacturer supplied a lib and header file with
the following typedef and cbEnableEvent function prototype:

typedef void (__stdcall *EVENTCALLBACK)(int, unsigned, unsigned, void*);

int EXTCCONV cbEnableEvent(int BoardNum, unsigned EventType, unsigned Count,
EVENTCALLBACK CallbackFunc, void *UserData);

I created a function to deal with the callback:

//the return void and input prameters are defined in the manual...
void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*)
{
}

So my challenge is to call cbEnableEvent with a pointer to CallHandler().

How about this?

EVENTCALLBACK pCallHandler;
pCallHandler = CallHandler;

The compiler complains with:

error C2440: '=' : cannot convert from 'void (__stdcall
CMyProjectView::* )(int,unsigned int,unsigned int,void *)' to
'EVENTCALLBACK'

There is no context in which this conversion is possible



Any help will be greatly appreciated!!!


You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. You can declare your C++ function as static, or simply make it a nonmember function. The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions.

Your static/global function will not be able to call other C++ member functions, but the usual solution to that is to pass your 'this' pointer in the void* UserData parameter. Then in your callback function you can cast it back to an object pointer and call object functions with it.

--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Re: [RFC] timers, pointers to functions and type safety
    ... * they have callback of type void ... callback is called by the code that even in theory has no ... cast to unsigned long and cast back in the callback. ... number - not a pointer cast to unsigned long, not an index in array, etc. ...
    (Linux-Kernel)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • RE: Callback not being called
    ... The callback in the unmanaged code appears to fire, ... ppStringArray, int cArray); ... void CALLBACK TimerProc( ... theCallBack,LPTSTR* ppStringArray, int cArray) ...
    (microsoft.public.dotnet.framework.interop)