Page 2 of 3

Re: creating a callback method in C++

Posted: Tue Apr 06, 2010 9:33 am
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.

Re: creating a callback method in C++

Posted: Tue Apr 06, 2010 10:14 am
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.

Re: creating a callback method in C++

Posted: Wed Apr 07, 2010 9:35 am
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.

Re: creating a callback method in C++

Posted: Fri Apr 09, 2010 7:38 am
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.

Re: creating a callback method in C++

Posted: Fri Apr 09, 2010 7:50 am
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;

Re: creating a callback method in C++

Posted: Fri Apr 09, 2010 7:59 am
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.

Re: creating a callback method in C++

Posted: Fri Apr 09, 2010 8:31 am
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()

Re: creating a callback method in C++

Posted: Fri Apr 09, 2010 9:00 am
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...

Re: creating a callback method in C++

Posted: Fri Apr 09, 2010 11:28 am
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.

Re: creating a callback method in C++

Posted: Mon Apr 19, 2010 7:51 am
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 ;)