Proper way to create a winrt object in C

Leslie 1 Reputation point
2021-06-05T15:54:22.163+00:00

May I know what is the proper way to create a winrt object in C?
Right now I'm trying to convert my C++ code which use winrt API to a plain C code.
And right now I'm able to get a few winrt static function working.
However, for the objects required by the static function, like the __FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation for the get_Completed function in __FIAsyncOperation_1_Windows__CDevices__CHumanInterfaceDevice__CHidDeviceVtbl, I can't find a proper way to create the object.
First, I can't find the iid of this object in the idl file.
Second, I'm not sure about the namespace of the object.

I did find how this class being declare in C++ macro,

#ifndef DEF___FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation_USE
#define DEF___FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation_USE
#if !defined(RO_NO_TEMPLATE_NAME)
namespace ABI { namespace Windows { namespace Foundation {
template <>
struct __declspec(uuid("07faa053-eb2f-5cba-b25b-d9d57be6715f"))
IAsyncOperation<ABI::Windows::Devices::Enumeration::DeviceInformation*> : IAsyncOperation_impl<ABI::Windows::Foundation::Internal::AggregateType<ABI::Windows::Devices::Enumeration::DeviceInformation*, ABI::Windows::Devices::Enumeration::IDeviceInformation*>>
{
    static const wchar_t* z_get_rc_name_impl()
    {
        return L"Windows.Foundation.IAsyncOperation`1<Windows.Devices.Enumeration.DeviceInformation>";
    }
};
// Define a typedef for the parameterized interface specialization's mangled name.
// This allows code which uses the mangled name for the parameterized interface to access the
// correct parameterized interface specialization.
typedef IAsyncOperation<ABI::Windows::Devices::Enumeration::DeviceInformation*> __FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation_t;
#define __FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation ABI::Windows::Foundation::__FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation_t
/* Foundation */ } /* Windows */ } /* ABI */ }

#endif // !defined(RO_NO_TEMPLATE_NAME)
#endif /* DEF___FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation_USE */

So, I tried to use this uuid and name_impl here to create the object, like this

            namespace = L"Windows.Foundation.IAsyncOperation`1<Windows.Devices.Enumeration.DeviceInformation>";
            hr = WindowsCreateStringReferenceFunc(namespace, (UINT32)wcslen(namespace), &namespace_string_header, &namespace_string);
            static const IID async_iid = { 0x07faa053, 0xeb2f, 0x5cba, { 0xb2, 0x5b, 0xd9, 0xd5, 0x7b, 0xe6, 0x71, 0x5f } };
            if (SUCCEEDED(hr)) {
                __FIAsyncOperation_1_Windows__CDevices__CEnumeration__CDeviceInformation* test;
                hr = RoGetActivationFactoryFunc(namespace_string, &async_iid, &test);
                if (!SUCCEEDED(hr)) {
                    printf("Couldn't find Windows.Foundation.IAsyncOperation`1<Windows.Devices.Enumeration.DeviceInformation>: %d\n", hr);
                }
            }

And after the build, the program return
Couldn't find Windows.Foundation.IAsyncOperation`1<Windows.Devices.Enumeration.DeviceInformation>: -2147221164
As I don't have the error code mapping, I don't know which part goes wrong.
So can anyone tell me the correct way to create a object of winrt in c?

Community Center Not monitored
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.