How to use _com_ptr_t?

Let's say I have a class that owns D3DDevice:

class Thing
{
public:
    Thing()
    {
        D3D11CreateDevice(..., &device, ...);
    }
    ~Thing()
    {
        device->Release();
    }
private:
    ID3D11Device* device;
};

From what I understand, I can use _com_ptr_tto ensure that the object will be deleted without my explicit call Release()in the destructor. The problem is that I cannot understand the correct syntax for the template.

I could find almost no information about _com_ptr_t, and the closest I could find for an answer was this (Japanese) one . After the syntax, I get a bunch of compiler errors:

private:
    //ID3D11Device* device;
    _com_ptr_t <_com_IIID<ID3D11Device, &__uuidof(ID3D11Device)>> device;

error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
error C2065: 'device' : undeclared identifier

By the way, I can use this to return COM pointers from functions and ensure that they will be deleted when they leave the call area, right?

+5
source share
3 answers

COM, :

1) # , _com_ptr_t.

2) CComPtr, COM , AddRef/Release, .

, , #import, 1). , #import , COM HRESULT _com_error. IMHO, ++ COM-.

+7

_com_ptr_t . , IHTMLDocument3Ptr:

typedef _com_ptr_t <_com_IIID<IHTMLDocument3, &__uuidof(IHTMLDocument3)>> IHTMLDocument3Ptr;

:

_COM_SMARTPTR_TYPEDEF(IHTMLDocument3, IID_IHTMLDocument3);

IHTMLDocument3Ptr, :

IHTMLDocument3Ptr htmlDocument3;

CComQIPtr, :

CComQIPtr<IHTMLDocument3> htmlDocument3;

"comdefsp.h" COM- (https://singularity.svn.codeplex.com/svn/base/Windows/Inc/comdefsp.h). "comdef.h" . , IDispatch :

IDispatchPtr dispatch;

CComPtr :

CComPtr<IDispatch> dispatch;

_com_ptr_t = ATL

_com_ptr_t CComPtr/CComQIPtr , ATL.

COM-, ATL, _bstr_t ( CComBSTR) _variant_t ( CComVariant).

+14

The second error code in your question means that the compiler does not know one (or both) to the type <: you need to include appropriate headers to provide both _com_ptr_t, and _com_IIIDare valid and known types. Alternatively, it >>can be parsed as a shift-right statement, place a space between them to ensure the correct transfer of pairs.

comdef.hshould fix both problems (see code)

+3
source

All Articles