c++winapiatl

Win32 C++: Error in CComPtr Release(): ATL::_NoAddRefReleaseOnCComPtr<T> in "atlcomcli.h" is inaccessible


I'm writing a Win32 C++ function to iterate Portable Devices. After successfully opening and connecting to the device, I need to release it. (There's also a Close() but Microsoft's documentation recommends Release() in its examples.)

For some reason I'm getting the error on pIPortableDevice->Release() as below related to atlcomcli.h even though it's available:

function ATL::_NoAddRefReleaseOnCComPtr<T>::Release [with T=IPortableDevice]" 
(declared in ..\atlmfc\include\atlcomcli.h) is inaccessible

You can zoom in to see the full image: enter image description here

The other methods, such as ->Open() and ->Content(), work. Full code:

DWORD countPnPDeviceIDs = 0;
PWSTR* pPnpDeviceIDs = NULL;
CComPtr<IPortableDeviceManager> pPortableDeviceManager;
CComPtr<IPortableDevice> pIPortableDevice;
CComPtr<IPortableDeviceValues> pClientInformation;
CComPtr<IPortableDeviceContent> pContent;
//...
pIPortableDevice->Content(&pContent); // OK
pContent->Properties(&pContentProperties); // OK
//...
pIPortableDevice->Release(); // SYNTAX ERROR

VS Code Project Properties:
Windows SDK 10.0
Visual Studio 2022 (v143)
C++ 14 Standard

Includes:

#include <PortableDeviceApi.h>
#include <PortableDevice.h>
#include <atlbase.h>

Linked: PortableDeviceGuids.lib


Solution

  • CComPtr<T> is a smart pointer. It means, you should not release resources that a smart pointer holds. Hence, you should not call pIPortableDevice->Release(). This is a private member function that will be called in the destructor CComPtr::~CComPtr() when pPortableDeviceManager life ends.