Using WRL to access WinRT, I cannot get ActivateInstance to work

431 views Asked by At

I am trying to figure out how to access the WinRT API via WRL in C++, using visual studio 2022 with the latest update 17.1.2.

(Background: I've done a fair amount of old-fashioned Windows desktop programming with the Win32 API. However, I now wish to access parts of the Windows API that are supported only in WinRT, such as access to Bluetooth LE devices. Note that I am trying to avoid UWP applications, as I fear it may cause my applications to be bloated, and am not sure what needed functionality it might remove from my programs. Further, I have tried Microsoft's C++/WinRT, but was having lots of trouble at points where the Microsoft compiler was telling me there was a problem up in the generated C++ headers--not my own code. So I am now trying to work closer to the ground with WRL.)

So, starting with an example provided in MS documentation (uri winRT class, which I can get to work as they wrote it), I have tried to try use of the ActivateInstance WRL class (which is not used in their example).

It seems not to be working for me. But I am probably doing something wrong. Can anyone help?

I have pared down the code to show the precise issue:

#include <roapi.h>
#include <combaseapi.h>
#include <iostream>

#include <windows.foundation.h>
#include <windows.foundation.collections.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>
#include <stdio.h>

using namespace ABI::Windows::Foundation;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

int dummy;

ComPtr<IUriRuntimeClassFactory> uriFactory;
ComPtr<IUriRuntimeClass> uri;

HRESULT hr, hr2;
    
int main()
{
   
    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    
    // Get the activation factory for the IUriRuntimeClass interface.
    
    
    hr  = GetActivationFactory(HStringReference(L"Windows.Foundation.Uri").Get(), &uriFactory);
    
    hr2 = ActivateInstance     (HStringReference(L"Windows.Foundation.Uri").Get(), &uri);

    dummy = 1;    
}

What happens is hr returns as S_OK. (Creating the factory. I don't use it here, but it is apparently a valid factory as it works when I leave in the rest of the Microsoft WRL example.)

But, what is confusing me is that hr2 is returning "E_NOTIMPl (not implemented)", and I thought it would get me a smart pointer to the uri class.

(Referencing this MS documentation for ActivateInstance:

and observing that the argument types are the same as

for GetActivationFactory

Thanks

0

There are 0 answers