How to REALLY consume a WinRT object with the WRL

Microsoft published an example that supposedly demonstrates how to consume a WinRT object with the WRL at How to Create and Consume an Object.

However, if you’ve tried that example you probably noticed that it simply does not work as the following snippet returns E_INVALIDARG.

ComPtr<IActivationFactory> uriActivationFactory;
HRESULT hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriActivationFactory);

The problem is, that HString::MakeReference(RuntimeClass_Windows_Foundation_Uri) does not return a valid HStringReference-instance.

Instead, you should create a HStringReference-object using its constructor and pass that instance to the GetActivationFactory call:

ComPtr<IActivationFactory> uriActivationFactory;
HStringReference runtimeClassUri(RuntimeClass_Windows_Foundation_Uri);
HRESULT hr = GetActivationFactory(runtimeClassUri.Get(), &uriActivationFactory);

Now, the call should return S_OK and you’ll have a valid uriActivationFactory that you can work with.

Leave a Reply

Your email address will not be published. Required fields are marked *