creating a callback method in C++

symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

creating a callback method in C++

Post by symfonysid »

Hi,

I had originally posted this under an old thread. But I'm reposting it to a new thread, because it's actually a new topic.

I've been reading through the C++ VC8/2005 sample trying to figure out how to set-up a callback. But I don't understand how this sample works. So, I was hoping that someone could give me a little help with it.

What I've seen so far is that:

The class CCameraEvents in CameraDlg.h registers the callback method HandleFrameAvailable().

The class CCameraDlg which actually contains the callback subclasses CCameraEvents().

The method OnInitDialog() in the class CCameraDlg contains the line of code:
// hook up connection points
this->DispEventAdvise(m_spCamera);
I think this is what sets up the callback, but whenever I try to make this same call myself, I get exception, because somewhere, MFC is trying to make a lock, but it can't do so. I'm not sure what state the pointer m_spCamera is actually in when this method is called. Has it been initialized? Has the camera been opened? I don't know...

In any case I've adapted this code as best I could for my own application, but the callback isn't actually used. And my guess at the moment is that it's because my call to DispEventAdvise() is failing. However, maybe I misunderstood the significance of this method, and there is something else that is happening in the sample application which I'm not doing.

Thank you for your help.

Greg
beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: creating a callback method in C++

Post by beckdo »

Events in MFC are a challenge. The state of m_spCamera is probably what's holding you up.

You'll want to make sure you've used the CameraCollection to retrieve a valid CComPtr to a camera. At that point you can make the call to DispEventAdvise(). The camera doesn't have to be opened or started for the event callback to register properly.
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

Hi Doug,

When I make the call to DispEventAdvise() right after retrieving the CComPtr, I get the exception I already mentioned. Visual Studio shows me the atlcore.h file, and it fails in the CComCriticalSection class's Lock() method:

class CComCriticalSection
{
public:
CComCriticalSection() throw()
{
memset(&m_sec, 0, sizeof(CRITICAL_SECTION));
}
~CComCriticalSection()
{
}
HRESULT Lock() throw()
{
EnterCriticalSection(&m_sec); *** FAILS HERE ***
return S_OK;
}
HRESULT Unlock() throw()
{
LeaveCriticalSection(&m_sec);
return S_OK;
}
HRESULT Init() throw()
{
HRESULT hRes = E_FAIL;
__try
{
InitializeCriticalSection(&m_sec);
hRes = S_OK;
}
// structured exception may be raised in low memory situations
__except(STATUS_NO_MEMORY == GetExceptionCode())
{
hRes = E_OUTOFMEMORY;
}
return hRes;
}

HRESULT Term() throw()
{
DeleteCriticalSection(&m_sec);
return S_OK;
}
CRITICAL_SECTION m_sec;
};

I don't know why this is. But in any case I've adapted the OptiTrack example as best I could, and after looking through it again, I still have no idea what I'm missing. It's rather difficult to even understand how this example works though, because it's full of all this MFC xxxx with Window's message maps and so on... Is there a simpler example available showing just how to set-up a callback, but which doesn't do any image displaying or anything like that? Something like the C++ command line application for polling frames?

Thanks,
Greg
beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: creating a callback method in C++

Post by beckdo »

We do have other less convoluted samples. Are you particularly invested in getting an event callback or can you rely on polling for frames?
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

Hi Doug,

Yes. I am particularly invested in getting an event callback. I was polling for frames, but I realized that the timer I was using to poll was only triggering every 15-20 msecs even though I set it to trigger every 5 msecs. Although there might be another way around that problem, for the particular application which we've created it's important that the latency be as small as possible, and so, a callback is any case the best solution.

Greg
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

Hi Doug,

I haven't heard from you in about a week and a half now. Does this mean that you're working on putting together a less convoluted sample for me?

Thanks,
Greg
yoshi
Posts: 174
Joined: Sun Jul 24, 2005 5:00 am
Location: silicon valley

Re: creating a callback method in C++

Post by yoshi »

Hi,

Do you have following lines of code as in the sample project?

CComModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()

_Module.Init(ObjectMap, m_hInstance);
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

Hi Yoshi,

No. I didn't. I just tried to put them in, but I had some problems, because m_hInstance wasn't declared. In your app m_hInstance is declared in the super class CWinApp, but I'm not using MFC to create the user interface, and I don't have any class that is derived from CWinApp. I read somewhere online that you can get a reference to m_hInstance by calling AfxGetInstanceHandle(), but when I did this, I got an assertion error, because m_hInstance apparently was NULL inside the AfxGetInstanceHandle() method. Do you have any advice how I could get around this problem?

Thanks,
Greg
yoshi
Posts: 174
Joined: Sun Jul 24, 2005 5:00 am
Location: silicon valley

Re: creating a callback method in C++

Post by yoshi »

Is your program console or window app? Can you try using HINSTANCE from WinMain()?
symfonysid
Posts: 20
Joined: Fri Nov 09, 2007 7:39 am

Re: creating a callback method in C++

Post by symfonysid »

Hi Yoshi,

Yes, I had to dig around to find WinMain in GUI toolkit I was using. (It was being called in a macro.) But once I found it, I passed the HINSTANCE variable it was being given to _Module.Init, and now, the callback is working fine! And I'm finally getting 100 frames/sec again!!!

Thank you very much for your help,

Greg
Post Reply