I'm trying to write a simple C++ app that registers to Windows sensor events. I followed the MSDN documentaion and managed succesfully to get notifications when sensor events occur, my problem is that my main function ends, and so does the application. How to i cuase it to wait forever for events to occur? Currently it registers and dies...
I have the following code:
My main looks like this:
int _tmain(int argc, _TCHAR* argv[])
{
RegisterToSensorEvents();
return 0;
}
void RegisterToSensorEvents()
{
ISensorManager* pSensorManager = NULL;
CoInitialize(NULL);
HRESULT hr = ::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSensorManager));
// Get a collection of all sensors on the computer.
ISensorCollection* pSensorCollection = NULL;
hr = pSensorManager->GetSensorsByCategory(SENSOR_CATEGORY_ALL, &pSensorCollection);
EventsManager *pEventClass = NULL;
ISensorEvents* pMyEvents = NULL;
pEventClass = new(std::nothrow) EventsManager();
hr = pEventClass->QueryInterface(IID_PPV_ARGS(&pMyEvents));
ULONG numOfSensors;
pSensorCollection->GetCount(&numOfSensors);
for(int i=0; i< numOfSensors; i++)
{
ISensor *sensor = NULL;
pSensorCollection->GetAt(i,&sensor);
hr = sensor->SetEventSink(pMyEvents);
}
}
EventsManager is a class that derives from ISensorEvents and implements its callbacks, for example:
STDMETHODIMP EventsManager::OnDataUpdated(ISensor *pSensor,ISensorDataReport *pNewData)
{
cout <<"got here: Data Update" << endl;
}
I tried:
int _tmain(int argc, _TCHAR* argv[])
{
RegisterToSensorEvents();
while(true){}
return 0;
}
but seems like this infinte loop did not leave time for the program to process the incomming events, I tried adding Sleep in the loop body, but it didn't work either.
anyone?
UPDATE:
after investigation i see that the issue is different - seems like somehow my registartion of SetEventSink gets canceled and that is why i don't get any event notification.
if i copy this line:
hr = sensor->SetEventSink(pMyEvents);
into my loop:
while(true)
{
hr = sensor->SetEventSink(pMyEvents);
}
the events are fired as expected. But it sounds to me very wrong to do such a thing.
Need to understand why this is hapenning.
Can anyone help?