簡體   English   中英

C ++ COM ATL DLL

[英]C++ COM ATL DLL

我是帶有v110_xp工具集的Visual Studio 2012專業版。 我想在COM類中“轉換”我的c ++動態庫。 該庫的結構如下:

struct A;
struct B;

class IClass {
public:
    virtual ~IClass() = 0;
    virtual A doA() = 0;
    virtual B doB() = 0;
    virtual void getA( A& a ) = 0;
    virtual void getB( B& b) = 0;
};
inline IClass::~IClass() {}

typedef std::unique_ptr< IClass > IClassPtr;
API_LIB IClassPtr ClassCreate( /* param */ );

現在,所有方法和功能都可以拋出派生自std :: exception的類(析構函數除外)。

我想將其設為COM類,以便可以從C#中使用它。 哪種齋戒方式可以做到這一點? ATL可以提供幫助嗎? 有人知道一些教程或書籍嗎? 我對COM沒有經驗。

您至少應該從IUnknown派生您的課程。 如果要在某些腳本中使用COM,則可以從IDispatch派生您的類。 對於COM來說,一本好書是Jonathan Bates撰寫的使用ATL創建輕量級組件。

但是,一些真正的基本實現可能看起來像這樣:

class MyCOM : public IUnknown
{
public:
    static MyCOM * CreateInstance()
    {
        MyCOM * p( new(std::nothrow) MyCOM() );
        p->AddRef();
        return p;
    }

    ULONG __stdcall AddRef()
    {
        return ++nRefCount_;
    }

    ULONG __stdcall Release()
    {
        assert( nRefCount_ > 0 );

        if( --nRefCount_ == 0 )
        {
            delete this;
            return 0;
        }

        return nRefCount_;
    }

    HRESULT __stdcall QueryInterface( const IID & riid, void** ppvObject )
    {
        if( riid == IID_IUnknown )
        {
            AddRef();
            *ppvObject = this;
            return S_OK;
        }

        // TO DO: add code for interfaces that you support...

        return E_NOINTERFACE;
    }

private:

    MyCOM()
    : nRefCount_( 0 ){}
    MyCOM(const MyCOM & ); // don't implement
    MyCOM & operator=(const MyCOM & ); // don't implement
    ~MyCOM(){}

    ULONG nRefCount_;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM