📄 oleconn.cpp
字号:
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#ifdef AFXCTL_CORE3_SEG
#pragma code_seg(AFXCTL_CORE3_SEG)
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
#define GetConnectionPtr(pTarget, pEntry) \
(LPCONNECTIONPOINT)((char*)pTarget + pEntry->nOffset + \
offsetof(CConnectionPoint, m_xConnPt))
/////////////////////////////////////////////////////////////////////////////
// CConnectionPoint
CConnectionPoint::CConnectionPoint() :
m_pUnkFirstConnection(NULL),
m_pConnections(NULL)
{
}
CConnectionPoint::~CConnectionPoint()
{
POSITION pos = GetStartPosition();
while (pos != NULL)
{
LPUNKNOWN pUnk = GetNextConnection(pos);
ASSERT(pUnk != NULL);
pUnk->Release();
}
if (m_pConnections != NULL)
delete m_pConnections;
}
POSITION CConnectionPoint::GetStartPosition() const
{
ASSERT(m_pConnections == NULL || m_pUnkFirstConnection == NULL);
if (m_pUnkFirstConnection != NULL)
return (POSITION)-1;
if (m_pConnections == NULL || m_pConnections->GetSize() == 0)
return NULL;
return (POSITION)1;
}
LPUNKNOWN CConnectionPoint::GetNextConnection(POSITION& pos) const
{
ASSERT(pos != NULL);
if (pos == (POSITION)-1)
{
ASSERT(m_pUnkFirstConnection != NULL);
ASSERT(m_pConnections == NULL);
pos = NULL;
return m_pUnkFirstConnection;
}
ASSERT(m_pConnections != NULL);
ASSERT((long)pos > 0 && (long)pos <= m_pConnections->GetSize());
int nIndex = (long)pos - 1;
pos = (POSITION)((long)pos + 1);
if ((long)pos > m_pConnections->GetSize())
pos = NULL;
return (LPUNKNOWN)m_pConnections->GetAt(nIndex);
}
const CPtrArray* CConnectionPoint::GetConnections()
{
ASSERT_VALID(this);
if (m_pConnections == NULL)
CreateConnectionArray();
ASSERT(m_pConnections != NULL);
return m_pConnections;
}
void CConnectionPoint::OnAdvise(BOOL)
{
ASSERT_VALID(this);
}
int CConnectionPoint::GetMaxConnections()
{
ASSERT_VALID(this);
// May be overridden by subclass.
return -1;
}
LPCONNECTIONPOINTCONTAINER CConnectionPoint::GetContainer()
{
CCmdTarget* pCmdTarget = (CCmdTarget*)((BYTE*)this - m_nOffset);
#ifdef _DEBUG
pCmdTarget->CCmdTarget::AssertValid();
#endif
LPCONNECTIONPOINTCONTAINER pCPC = NULL;
if (SUCCEEDED((HRESULT)pCmdTarget->ExternalQueryInterface(
&IID_IConnectionPointContainer, (LPVOID*)&pCPC)))
{
ASSERT(pCPC != NULL);
}
return pCPC;
}
void CConnectionPoint::CreateConnectionArray()
{
ASSERT(m_pConnections == NULL);
m_pConnections = new CPtrArray;
if (m_pUnkFirstConnection != NULL)
{
m_pConnections->Add(m_pUnkFirstConnection);
m_pUnkFirstConnection = NULL;
}
ASSERT(m_pConnections != NULL);
ASSERT(m_pUnkFirstConnection == NULL);
}
int CConnectionPoint::GetConnectionCount()
{
if (m_pUnkFirstConnection != NULL)
return 1;
if (m_pConnections == NULL)
return 0;
return m_pConnections->GetSize();
}
LPUNKNOWN CConnectionPoint::QuerySinkInterface(LPUNKNOWN pUnkSink)
{
LPUNKNOWN pUnkReturn = NULL;
pUnkSink->QueryInterface(GetIID(), reinterpret_cast<void**>(&pUnkReturn));
return pUnkReturn;
}
STDMETHODIMP_(ULONG) CConnectionPoint::XConnPt::Release()
{
METHOD_PROLOGUE_EX_(CConnectionPoint, ConnPt)
return (ULONG)pThis->InternalRelease();
}
STDMETHODIMP_(ULONG) CConnectionPoint::XConnPt::AddRef()
{
METHOD_PROLOGUE_EX_(CConnectionPoint, ConnPt)
return (ULONG)pThis->InternalAddRef();
}
STDMETHODIMP CConnectionPoint::XConnPt::QueryInterface(
REFIID iid, LPVOID* ppvObj)
{
METHOD_PROLOGUE_EX_(CConnectionPoint, ConnPt)
ASSERT(AfxIsValidAddress(ppvObj, sizeof(LPVOID), FALSE));
if (IsEqualIID(iid, IID_IUnknown) ||
IsEqualIID(iid, IID_IConnectionPoint))
{
*ppvObj = this;
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
STDMETHODIMP CConnectionPoint::XConnPt::GetConnectionInterface(IID* pIID)
{
METHOD_PROLOGUE_EX_(CConnectionPoint, ConnPt)
ASSERT(AfxIsValidAddress(pIID, sizeof(IID)));
*pIID = pThis->GetIID();
return S_OK;
}
STDMETHODIMP CConnectionPoint::XConnPt::GetConnectionPointContainer(
IConnectionPointContainer** ppCPC)
{
METHOD_PROLOGUE_EX_(CConnectionPoint, ConnPt)
ASSERT(AfxIsValidAddress(ppCPC, sizeof(LPCONNECTIONPOINT)));
if ((*ppCPC = pThis->GetContainer()) != NULL)
return S_OK;
return E_FAIL;
}
STDMETHODIMP CConnectionPoint::XConnPt::Advise(
LPUNKNOWN pUnkSink, DWORD* pdwCookie)
{
METHOD_PROLOGUE_EX_(CConnectionPoint, ConnPt)
ASSERT(AfxIsValidAddress(pUnkSink, sizeof(IUnknown), FALSE));
ASSERT((pdwCookie == NULL) || AfxIsValidAddress(pdwCookie, sizeof(DWORD)));
if (pUnkSink == NULL)
return E_POINTER;
LPUNKNOWN lpInterface;
int cMaxConn = pThis->GetMaxConnections();
if ((cMaxConn >= 0) && (pThis->GetConnectionCount() == cMaxConn))
{
return CONNECT_E_ADVISELIMIT;
}
if ((lpInterface = pThis->QuerySinkInterface(pUnkSink)) != NULL)
{
if (pThis->m_pUnkFirstConnection == NULL &&
pThis->m_pConnections == NULL)
{
pThis->m_pUnkFirstConnection = lpInterface;
}
else
{
if (pThis->m_pConnections == NULL)
pThis->CreateConnectionArray();
pThis->m_pConnections->Add(lpInterface);
}
pThis->OnAdvise(TRUE);
if (pdwCookie != NULL)
*pdwCookie = (DWORD)lpInterface;
return S_OK;
}
return E_NOINTERFACE;
}
STDMETHODIMP CConnectionPoint::XConnPt::Unadvise(DWORD dwCookie)
{
METHOD_PROLOGUE_EX_(CConnectionPoint, ConnPt)
if (pThis->m_pUnkFirstConnection != NULL)
{
if ((DWORD)pThis->m_pUnkFirstConnection == dwCookie)
{
pThis->m_pUnkFirstConnection->Release();
pThis->m_pUnkFirstConnection = NULL;
pThis->OnAdvise(FALSE);
return S_OK;
}
else
{
return CONNECT_E_NOCONNECTION;
}
}
if (pThis->m_pConnections == NULL)
return CONNECT_E_NOCONNECTION;
LPUNKNOWN pUnkSink;
int cConnections = pThis->m_pConnections->GetSize();
for (int i = 0; i < cConnections; i++)
{
pUnkSink = (LPUNKNOWN)(pThis->m_pConnections->GetAt(i));
if ((DWORD)pUnkSink == dwCookie)
{
pUnkSink->Release();
pThis->m_pConnections->RemoveAt(i);
pThis->OnAdvise(FALSE);
return S_OK;
}
}
return CONNECT_E_NOCONNECTION;
}
/////////////////////////////////////////////////////////////////////////////
// CEnumConnections
class CEnumConnections : public CEnumArray
{
public:
CEnumConnections(const void* pvEnum, UINT nSize);
~CEnumConnections();
void AddConnection(CONNECTDATA* pConn);
protected:
virtual BOOL OnNext(void* pv);
virtual CEnumArray* OnClone();
UINT m_nMaxSize; // number of items allocated (>= m_nSize)
DECLARE_INTERFACE_MAP()
};
BEGIN_INTERFACE_MAP(CEnumConnections, CEnumArray)
INTERFACE_PART(CEnumConnections, IID_IEnumConnections, EnumVOID)
END_INTERFACE_MAP()
CEnumConnections::CEnumConnections(const void* pvEnum, UINT nSize) :
CEnumArray(sizeof(CONNECTDATA), pvEnum, nSize, TRUE)
{
m_nMaxSize = 0;
}
CEnumConnections::~CEnumConnections()
{
if (m_pClonedFrom == NULL)
{
UINT iCP;
CONNECTDATA* ppCP = (CONNECTDATA*)(VOID *)m_pvEnum;
for (iCP = 0; iCP < m_nSize; iCP++)
RELEASE(ppCP[iCP].pUnk);
}
// destructor will free the actual array (if it was not a clone)
}
BOOL CEnumConnections::OnNext(void* pv)
{
if (!CEnumArray::OnNext(pv))
return FALSE;
// outgoing connection point needs to be AddRef'ed
// (the caller has responsibility to release it)
((CONNECTDATA*)pv)->pUnk->AddRef();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -