Using Com Events

Post Reply
psykoboy
Posts: 3
Joined: Tue Oct 21, 2008 4:17 pm

Using Com Events

Post by psykoboy »

Hi everyone,

I'm having trouble trouble using the Com dialog to get the frame only when the onFrameAvailable event is trigered. I actually don't really know how to use Event in c++. I'm trying to read the MSDN but it's a little bit confusing.
I tried the "minimal c++ code" available in this forum, but doesn't seem to work. I search in the samples, didn't find nothing.
Maybe someone here can help ?
Are the ATL macro easier to use ?

Here is the code I have now :

Code: Select all

		CoInitialize(NULL);


		CComPtr<INPCameraCollection> * collection = new CComPtr<INPCameraCollection>();
		CComPtr<INPCamera>           cam;

		collection->CoCreateInstance(CLSID_NPCameraCollection);

		CameraEvents ce;
		IUnknown* pIUnknown = NULL;
		IConnectionPointContainer* pIConnectionPointContainer = NULL;

		(*collection)->Enum();
		(*collection)->Item(0, &cam);

		ce.DispEventAdvise(cam);
		
		// Get the IUnknown pointer
		cam->QueryInterface(IID_IUnknown, (void**)&pIUnknown);
		// Get the connectin Point
		cam->QueryInterface(IID_IConnectionPointContainer, (void **)&pIConnectionPointContainer);
		// Find the connection point
		// Don't really know what to use here pIConnectionPointContainer->FindConnectionPoint(....
		
		cam->Open();
		cam->Start();

		int i = 0;
		while(i < 300){
			i++;
			printf(".");
		}
		cam->Stop();
		cam->Close();
		collection->Release();		
		delete collection;

Thanks a lot for your help

Edit:

I also tried with this code, but I don't get any print from handle functions

Code: Select all

class CCameraEvents :    public IDispEventImpl<0, CCameraEvents, &DIID__INPCameraEvents, &LIBID_OptiTrack, 1, 0>
{
public:
    CCameraEvents() { ; }
    ~CCameraEvents() { ; }

public:
    // connection point notifications
    STDMETHOD_(void, OnFrameAvailableIdCamera)(INPCamera * pCamera, LONG Group, LONG Id) { HandleFrameAvailableIdCamera(pCamera, Group, Id); }
    STDMETHOD_(void, OnFrameAvailable)(INPCamera * pCamera) { HandleFrameAvailable(pCamera); }
    STDMETHOD_(void, OnSwitchChange)(INPCamera * pCamera, LONG lNewSwitchState) { HandleSwitchChange(pCamera, lNewSwitchState); }

    virtual void HandleFrameAvailable(INPCamera * pCamera);
    virtual void HandleSwitchChange(INPCamera * pCamera, LONG lNewSwitchState);
    virtual void HandleFrameAvailableIdCamera(INPCamera * pCamera, LONG Group, LONG Id);
    
public:
    BEGIN_SINK_MAP(CCameraEvents)
        SINK_ENTRY_EX(0, DIID__INPCameraEvents, 1, OnFrameAvailable)
        SINK_ENTRY_EX(0, DIID__INPCameraEvents, 2, OnSwitchChange)
        SINK_ENTRY_EX(0, DIID__INPCameraEvents, 3, OnFrameAvailableIdCamera)
    END_SINK_MAP()

};

void CCameraEvents::HandleFrameAvailable(INPCamera * pCamera)
{
	printf("\n HandleFrameAvailableEvent \n");
}


void CCameraEvents::HandleSwitchChange(INPCamera * pCamera, LONG lNewSwitchState)
{
	printf("\n HandleSwitchChangeEvent\n");
}

void CCameraEvents::HandleFrameAvailableIdCamera(INPCamera * pCamera, LONG Group, LONG Id) 
{
	printf("\n HandleFrameAvailableIdCamera Event \n");
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		CoInitialize(NULL);


		CComPtr<INPCameraCollection> * collection = new CComPtr<INPCameraCollection>();
		CComPtr<INPCamera>           cam;

		collection->CoCreateInstance(CLSID_NPCameraCollection);

		CCameraEvents * ce = new CCameraEvents();

		ce->DispEventAdvise(cam);
		
		
		cam->Open();
		cam->Start();
				
		int i = 0;
		while(i < 300){
			i++;
			printf(".");		
		}
		cam->Stop();
		cam->Close();
		collection->Release();		
		delete collection;
		
		
	}
	return nRetCode;
}
Post Reply