Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CALLBACK Function 1

Status
Not open for further replies.

francisco

Programmer
Jun 20, 2000
8
DE
First of all: What is a callback function?<br><br>My problem is, i´m trying to write my class to manage a Joystick, in this class I define two member functions:<br><br>BOOL CALLBACK EnumJoy(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);<br><br>BOOL DI_Init();<br><br>DI_Init() calls EnumDevices wich is defined as follows:<br><br>HRESULT EnumDevices(<br>&nbsp;&nbsp;DWORD dwDevType,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;LPDIENUMCALLBACK lpCallback,&nbsp;&nbsp;<br>&nbsp;&nbsp;LPVOID pvRef,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;DWORD dwFlags&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>);<br><br>The second parameter (lpCallback) is the <br>Address of a callback function to be called with a description of each DirectInput device.<br><br><br>My Call is implemented as follows:<br><br>if( !m_lpDIJoystick )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hr = m_lpDI-&gt;EnumDevices( DIDEVTYPE_JOYSTICK, EnumJoy, (LPVOID)FALSE,DIEDFL_ATTACHEDONLY) ;<br><br><br>When I try to compile it I get the following error message:<br><br>&nbsp;error C2664: 'EnumDevices' : cannot convert parameter 2 'int (const struct DIDEVICEINSTANCEA *,void *)' in 'int (__stdcall *)(const struct DIDEVICEINSTANCEA *,void *)' <br><br>If I define my member function EnumJoy out of my class as a global function than I compile my program but I would like to have it as a member function, how should I declare it?. <br>Thanks in Advance.<br><br>javi<br>
 
Dear javi,<br><br>To use a member function for a callback you need to satisfy two conditions.<br><br>1) The function must be declared 'static'. This means that when the function is called it will not contain a 'this' pointer and so will not have access to an instance of the class. So to solve that problem you need to implement condition (2)<br><br>2) The entry point API that generates the calls to the callback must take a 32bit application defined parameter that it passes to the callback function. When this occurs you can pass your own 'this' pointer as the parameter to gain access to your object instance in the callback function.<br><br>Now in your case condition (2) is satisfied by the 'pvRef' parameter of the IDirectInput7::EnumDevices() function. <br><br>class myclass<br>{<br>....<br>void init();<br>static BOOL CALLBACK EnumJoy(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);<br>};<br><br>void myclass::init(){<br>if( !m_lpDIJoystick )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hr = m_lpDI-&gt;EnumDevices( DIDEVTYPE_JOYSTICK, EnumJoy, (LPVOID)this,DIEDFL_ATTACHEDONLY) ;<br>}<br><br>static BOOL CALLBACK MyClass::EnumJoy(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef){<br><br>&nbsp;&nbsp;myclass* pThis = reinterpret_cast&lt;myclass*&gt;(pvRef);<br>&nbsp;&nbsp;// perform operations using pThis<br>}<br><br>Hope this helps<br>-pete<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top