相信积累的力量

Windows,Com,Second Example

---恢复内容开始---

#include <windows.h>
#include <shobjidl.h> 
#include <atlbase.h> // Contains the declaration of CComPtr.
// {EEA4592E-D18C-4FE1-939F-7A9771B27289}
static const GUID IID_IX = 
{ 0xeea4592e, 0xd18c, 0x4fe1, { 0x93, 0x9f, 0x7a, 0x97, 0x71, 0xb2, 0x72, 0x89 } };

__interface IX : public IUnknown
{
    virtual void fnc_x() = 0;

};
// {62B59A4E-E480-4E15-B57F-707906502BCD}
static const GUID IID_IY = 
{ 0x62b59a4e, 0xe480, 0x4e15, { 0xb5, 0x7f, 0x70, 0x79, 0x6, 0x50, 0x2b, 0xcd } };

__interface IY : public IUnknown
{
    virtual void fnc_y() = 0;
};
// {D5086CAB-CF19-4CEC-8CF7-7B5F03A53BF3}
static const GUID IID_IZ = 
{ 0xd5086cab, 0xcf19, 0x4cec, { 0x8c, 0xf7, 0x7b, 0x5f, 0x3, 0xa5, 0x3b, 0xf3 } };

__interface IZ : public IUnknown
{
   virtual void __stdcall fnc_z() = 0;
};

class MyClass : public IX, public IY
{
public:
    MyClass() : m_cRef(0) {}
    ~MyClass()
    {
        MessageBox(0, _T("MyClass dtor"), _T("message"), MB_OK);
    }

    virtual HRESULT __stdcall QueryInterface(REFIID riid, _COM_Outptr_ void **ppvObject)
    {
        if (riid == IID_IUnknown)
        {
            MessageBox(0, _T("QueryInterface IID_IUnknow"), _T("message"), MB_OK);
            *ppvObject = static_cast<IX*>(this);
        }
        else if (riid == IID_IX)
        {
            MessageBox(0, _T("QueryInterface IID_IX"), _T("message"), MB_OK);
            *ppvObject = static_cast<IX*>(this);
        }
        else if (riid == IID_IY)
        {
            MessageBox(0, _T("QueryInterface IID_IY"), _T("message"), MB_OK);
            *ppvObject = static_cast<IY*>(this);
        }
        else
        {
            MessageBox(0, _T("QueryInterface not exist"), _T("message"), MB_OK);
            *ppvObject = NULL;
            return E_NOINTERFACE;
        }
        reinterpret_cast<IUnknown*>(*ppvObject)->AddRef();
        return S_OK;

    }
    virtual ULONG __stdcall AddRef()
    {
        return InterlockedIncrement(&m_cRef);

    }
    virtual ULONG __stdcall Release()
    {
        if (InterlockedDecrement(&m_cRef) == 0)
        {
           delete this;
           return 0;
        }
        return m_cRef;

    }
    virtual void fnc_x()
    {
        MessageBox(0, _T("MyClass fnc_x"), _T("message"), MB_OK);
    }
    virtual void fnc_y()
    {
        MessageBox(0, _T("MyClass fnc_y"), _T("message"), MB_OK);
    }


private:
    long m_cRef;

};
IUnknown * CreateInstance()
{

    IUnknown * pI = static_cast<IX*>(new MyClass);
    pI->AddRef();
    return pI;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    HRESULT hr;
    IUnknown * pIunknown = CreateInstance();
    IX * pIX = NULL;
    hr = pIunknown->QueryInterface(IID_IX, (void**)(&pIX));
    if (SUCCEEDED(hr))
    {
        pIX->fnc_x();
        pIX->Release();
    }
    IZ * pIz = NULL;
    hr = pIunknown->QueryInterface(IID_IZ, (void**)&pIz);
    if (SUCCEEDED(hr))
    {
        pIz->fnc_z();
        pIz->Release();
    }
    else
    {


    }
    pIunknown->Release();
}

 

---恢复内容结束---

编辑器加载中...

posted @ 2013-06-21 08:58  ThreeF  阅读(264)  评论(0编辑  收藏  举报

相信积累的力量