×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

CALLBACK Function

CALLBACK Function

CALLBACK Function

(OP)
First of all: What is a callback function?

My problem is, i´m trying to write my class to manage a Joystick, in this class I define two member functions:

BOOL CALLBACK EnumJoy(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);

BOOL DI_Init();

DI_Init() calls EnumDevices wich is defined as follows:

HRESULT EnumDevices(
  DWORD dwDevType,              
  LPDIENUMCALLBACK lpCallback,  
  LPVOID pvRef,                 
  DWORD dwFlags                 
);

The second parameter (lpCallback) is the
Address of a callback function to be called with a description of each DirectInput device.


My Call is implemented as follows:

if( !m_lpDIJoystick )
      hr = m_lpDI->EnumDevices( DIDEVTYPE_JOYSTICK, EnumJoy, (LPVOID)FALSE,DIEDFL_ATTACHEDONLY) ;


When I try to compile it I get the following error message:

 error C2664: 'EnumDevices' : cannot convert parameter 2 'int (const struct DIDEVICEINSTANCEA *,void *)' in 'int (__stdcall *)(const struct DIDEVICEINSTANCEA *,void *)'

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?.
Thanks in Advance.

javi

RE: CALLBACK Function

Dear javi,

To use a member function for a callback you need to satisfy two conditions.

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)

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.

Now in your case condition (2) is satisfied by the 'pvRef' parameter of the IDirectInput7::EnumDevices() function.

class myclass
{
....
void init();
static BOOL CALLBACK EnumJoy(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef);
};

void myclass::init(){
if( !m_lpDIJoystick )
      hr = m_lpDI->EnumDevices( DIDEVTYPE_JOYSTICK, EnumJoy, (LPVOID)this,DIEDFL_ATTACHEDONLY) ;
}

static BOOL CALLBACK MyClass::EnumJoy(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef){

  myclass* pThis = reinterpret_cast<myclass*>(pvRef);
  // perform operations using pThis
}

Hope this helps
-pete

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close