How to await on an IAsyncOperation using C++/COM

450 Views Asked by At

I am accessing a WinRT API using C++ and COM.

The following code gives me results == unspecified

ComPtr<IGeolocatorStatics> statics;
RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Geolocation_Geolocator).Get(), IID_PPV_ARGS(&statics));

ComPtr<IAsyncOperation<GeolocationAccessStatus>> op;
statics->RequestAccessAsync(&op);

GeolocationAccessStatus results;
op->GetResults(&results);

Whereas I get results == allowed from adding a Sleep call.

ComPtr<IGeolocatorStatics> statics;
RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Devices_Geolocation_Geolocator).Get(), IID_PPV_ARGS(&statics));

ComPtr<IAsyncOperation<GeolocationAccessStatus>> op;
statics->RequestAccessAsync(&op);

Sleep(100);

GeolocationAccessStatus results;
op->GetResults(&results);

Obviously, I need to either await on AsyncOperation to receive a callback when it is finished. I would prefer to do the former. How do I await on a WinRT IAsyncOperation from C++ using COM?

0

There are 0 best solutions below