creating a callback method in C++

asuteau
Posts: 20
Joined: Tue Jan 05, 2010 8:01 am
Location: Angers, FRANCE

Re: creating a callback method in C++

Post by asuteau »

Hi,

I've just found this post and I'm currently highly interested in creating a callback in C++. Unfortunately, I cannot figure out how callback system works with OptiTrack devices...

The problem is that I'm not using a MFC project but a DLL project. So I have many difficulties to understand what is done in the MFC sample application provided with the SDK, to implement a callback.

Do you have any sample application which is less convoluted ?


Thanks for your answer.

Best regards,
Aymeric.
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

I used a C++ callback in a non MFC application. The source code is available at: http://surfacetracker.sourceforge.net. Look at the class OptitrackCamera.
asuteau
Posts: 20
Joined: Tue Jan 05, 2010 8:01 am
Location: Angers, FRANCE

Re: creating a callback method in C++

Post by asuteau »

Hi Greg,

Thanks a lot for your quick answer. I'll test it tomorrow and let you know if it works for me.

Best regards,
Aymeric.
asuteau
Posts: 20
Joined: Tue Jan 05, 2010 8:01 am
Location: Angers, FRANCE

Re: creating a callback method in C++

Post by asuteau »

Hi Greg,

I've a question for you. In your class "OptiTrackCamera.cpp", inside the method called "instantiateCOMObjects()", I don't understand what is the parameter g_hInstance which is the second parameter of the method _Module.Init.
Because when I use this function, during the execution, Visual Studio tells me that I'm using this parameter without having initialized it.
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

That's a global variable (the only one being used in the application) for the window handle. Look at the bottom of SurfaceTrackerApplication.cpp. It's defined like this:

HINSTANCE g_hInstance;

int __stdcall WinMain ( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
g_hInstance = hInstance;
asuteau
Posts: 20
Joined: Tue Jan 05, 2010 8:01 am
Location: Angers, FRANCE

Re: creating a callback method in C++

Post by asuteau »

Ok, I've defined it and now it's compiling without any problem.
But my callback function doesn't work. I've implemented handleFrameAvailable, added some display into it but I don't see anything in the Win32 console...
Just to be sure, the callback handleFrameAvailable will be called every time a new frame is available ?

Thanks for your response.
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

Yes, it should be. You must be missing something. I can remember having the same problem. If I remember correctly, in my case I left out these lines before OptiTrackCamera::instantiateCOMObjects():

CComModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
asuteau
Posts: 20
Joined: Tue Jan 05, 2010 8:01 am
Location: Angers, FRANCE

Re: creating a callback method in C++

Post by asuteau »

Yes, I think I missed something in instantiateCOMObjects method. I've checked and the following lines are just before this method :

Code: Select all

CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
However, I've seen that your method ends with this instruction :

Code: Select all

HRESULT hr = CoCreateInstance(__uuidof(INPCameraCollection), NULL, CLSCTX_ALL, __uuidof(INPCameraCollection), (void **) &pINPCameraCollection);

I haven't used this instruction, what does it do exactly ? Because I didn't declared any INPCameraCollection object, just an INPCamera one...
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

I can't remember. My own code was adapted from an OptiTrack example, and I carried that over from one of their examples.
asuteau
Posts: 20
Joined: Tue Jan 05, 2010 8:01 am
Location: Angers, FRANCE

Re: creating a callback method in C++

Post by asuteau »

Ok, I finally figured it out.
When hooking up connection points, I replaced DispEventAdvise(handle) by this->DispEventAdvise(handle). I've also added this->DispEventUnadvise(handle) in the destructor of my class and now everything works fine.

Thank you Greg, for all your answers ;)
Post Reply