Any recommendation in using smart pointers for COM-lite objects in C++/WinRT component? [closed]
Asked Answered
A

1

9

C++/WinRT provides 3 flavors of smart pointers to use for COM objects - Microsoft::WRL::ComPtr, com_ptr, and ATL-based CComPtr.

In my case, it is a COM-lite object, meaning it is NOT an in-proc or out-of-proc COM object, it is created as a C++ object.

In that case, which smart pointer should I use in a C++/WinRT component?

Aerophobia answered 2/9, 2020 at 6:0 Comment(7)
Out of the 3 smart pointer types you listed, only winrt::com_ptr is part of C++/WinRT. With just a single option, the question as asked is trivially answered. Though I'm not sure I understand what a "COM-lite object" is.Saudra
Why not Microsoft::WRL::ComPtr?, it is a Windows Runtime C++ template libraryAerophobia
Because you precluded the Windows Runtime Library from your options by requiring it to be part of C++/WinRT. Of course, you can use WRL types, though it's pretty unclear, what problem you are trying to solve with an answer to this question. Plus, I still don't understand what a "COM-lite object" is supposed to be, and why it needs to be owned by a COM smart pointer type when (apparently) it isn't a COM object at all.Saudra
C++/WinRT does not offer the mentioned flavors. WRL and ATL might be mixed side by side in C++ code, but this would not necessarily happen. Similar you might also have wil::com_ptr as well. The templates are doing the same thing, so your rule of thumb is not not mix libraries for no reason. If you are developing a project based on C++/WinRT then prefer its templates.Kudu
@iinspectable , it is not created by CoCreateInstanceAerophobia
@iinspectable , COM lite objects are NOT created by CoCreateInstance. Purpose of the question is to understand what is the reason for having multiple smart pointer definitions, answer would help to choose right option in future implementation.Aerophobia
"what is the reason for having multiple smart pointer definitions" - That's easy: Different libraries needed a way to manage COM object lifetimes. Neither library could re-use an existing implementation without pulling in a dependency, so each came up with its own implementation. Today we have ATL/MFC's CComPtr, Visual Studio's _com_ptr_t, WRL's ComPtr, C++/WinRT's com_ptr, and WIL's com_ptr. All of those implementations do in fact have dependencies, with the exception of C++/WinRT's that only requires a C++17 compiler (in theory, anyway).Saudra
C
11

Whether using "true COM" or "COM lite" (a.k.a. "nano-COM"), you track the lifetime the same way using the IUnknown methods AddRef and Release. The various COM smart-pointers all rely on the IUnknown methods, so you can you whichever one you want.

For C++/WinRT applications, the recommendation is to use winrt::com_ptr. For more information, see Consume COM components with C++/WinRT on Microsoft Docs.

I personally prefer to use Microsoft::WRL::ComPtr in all my code because my projects generally support UWP using C++/CX, UWP using C++/WinRT, Xbox using the XDK via C++/CX, Xbox using the XDK via C++/WinRT, Xbox using the GDK, and Win32 desktop platforms.

  • The WRL ComPtr works for Windows Runtime applications using C++/CX or C++/WinRT.

  • WRL ComPtr works for Win32 classic desktop applications on older versions of Windows as well. You can use #include <wrl/client.h> to get just this class, and if you make no use at all of Windows Runtime APIs you can also define both __WRL_NO_DEFAULT_LIB__ and __WRL_CLASSIC_COM_STRICT__ preprocessor defines.

  • WRL ships in the Windows 8.0 SDK, Windows 8.1 SDK, Windows 10 SDK, and Xbox One XDK. This means it's available all the way back to Visual Studio 2012.

  • It's a better option than the older ATL CComPtr. WRL is essentially "ATL 2.0" so a number of subtle usage issue have been fixed. See this MSDN Magazine Article.

  • There are also some quirks about where ATL was made available. In Visual Studio "Express" editions, ATL/MFC were not included as they were considered "Professional" features. The VS 2012 and VS 2013 versions of the Express SKUs all had the Windows 8.x SDK, so they included WRL but not ATL. FWIW, the Community editions don't have this issue.

See Microsoft Docs

Cabman answered 2/9, 2020 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.