From 69cd33e7df57bc2bac0aff6ae1408a470a528cc4 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Thu, 13 Feb 2025 18:31:22 -0800 Subject: [PATCH 1/2] Actually allocate the test type. --- .../COM/NativeClients/MiscTypes/MiscTypes.cpp | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp b/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp index ce76fba7f12da4..b4e7a8469113df 100644 --- a/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp +++ b/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp @@ -334,8 +334,12 @@ void ValidationTests() ::printf("-- Interfaces...\n"); { - struct InterfaceImpl : IInterface2 + class InterfaceImpl : IInterface2 { + std::atomic _refCount; + public: + InterfaceImpl() : _refCount{ 1 } {} + STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject) override { if (riid == __uuidof(IInterface1) || riid == __uuidof(IInterface2)) @@ -351,13 +355,28 @@ void ValidationTests() *ppvObject = nullptr; return E_NOINTERFACE; } + AddRef(); return S_OK; } - STDMETHOD_(ULONG, AddRef)() override { return 1; } - STDMETHOD_(ULONG, Release)() override { return 1; } - } iface{}; + + STDMETHOD_(ULONG, AddRef)() override + { + return ++_refCount; + } + + STDMETHOD_(ULONG, Release)() override + { + ULONG count = --_refCount; + if (count == 0) + delete this; + return count; + } + }; + ComSmartPtr iface; + iface.Attach(new InterfaceImpl()); + ComSmartPtr result; - HRESULT hr = miscTypesTesting->Marshal_Interface(&iface, &result); + HRESULT hr = miscTypesTesting->Marshal_Interface(iface, &result); THROW_IF_FAILED(hr); } } From 8a7d879f44f1041e7ded4a0e1f4de16668f5ccc4 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Fri, 14 Feb 2025 07:14:21 -0800 Subject: [PATCH 2/2] Use existing utilities to implement IUnknown. --- .../COM/NativeClients/MiscTypes/MiscTypes.cpp | 55 ++++++------------- 1 file changed, 17 insertions(+), 38 deletions(-) diff --git a/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp b/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp index b4e7a8469113df..c85df4871c43df 100644 --- a/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp +++ b/src/tests/Interop/COM/NativeClients/MiscTypes/MiscTypes.cpp @@ -89,6 +89,23 @@ struct VariantMarshalTest } }; +class InterfaceImpl : + public UnknownImpl, + public IInterface2 +{ +public: // IInterface1 +public: // IInterface2 +public: // IUnknown + STDMETHOD(QueryInterface)( + /* [in] */ REFIID riid, + /* [iid_is][out] */ _COM_Outptr_ void __RPC_FAR *__RPC_FAR *ppvObject) + { + return DoQueryInterface(riid, ppvObject, static_cast(this), static_cast(this)); + } + + DEFINE_REF_COUNTING(); +}; + void ValidationTests() { ::printf(__FUNCTION__ "() through CoCreateInstance...\n"); @@ -334,44 +351,6 @@ void ValidationTests() ::printf("-- Interfaces...\n"); { - class InterfaceImpl : IInterface2 - { - std::atomic _refCount; - public: - InterfaceImpl() : _refCount{ 1 } {} - - STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject) override - { - if (riid == __uuidof(IInterface1) || riid == __uuidof(IInterface2)) - { - *ppvObject = static_cast(this); - } - else if (riid == __uuidof(IUnknown)) - { - *ppvObject = static_cast(this); - } - else - { - *ppvObject = nullptr; - return E_NOINTERFACE; - } - AddRef(); - return S_OK; - } - - STDMETHOD_(ULONG, AddRef)() override - { - return ++_refCount; - } - - STDMETHOD_(ULONG, Release)() override - { - ULONG count = --_refCount; - if (count == 0) - delete this; - return count; - } - }; ComSmartPtr iface; iface.Attach(new InterfaceImpl());