📄 ekoala3.cpp
字号:
return;
}
CKoalaClassFactory::~CKoalaClassFactory(void)
{
//CHAPTER6MOD
DeleteInterfaceImp(m_pImpIExtConn);
//End CHAPTER6MOD
return;
}
/*
* CKoalaClassFactory::QueryInterface
* CKoalaClassFactory::AddRef
* CKoalaClassFactory::Release
*/
STDMETHODIMP CKoalaClassFactory::QueryInterface(REFIID riid
, PPVOID ppv)
{
*ppv=NULL;
if (IID_IUnknown==riid || IID_IClassFactory==riid)
*ppv=this;
//CHAPTER6MOD
if (IID_IExternalConnection==riid && NULL!=m_pImpIExtConn)
*ppv=m_pImpIExtConn;
//End CHAPTER6MOD
if (NULL!=*ppv)
{
((LPUNKNOWN)*ppv)->AddRef();
return NOERROR;
}
return ResultFromScode(E_NOINTERFACE);
}
STDMETHODIMP_(ULONG) CKoalaClassFactory::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CKoalaClassFactory::Release(void)
{
if (0L!=--m_cRef)
return m_cRef;
delete this;
return 0;
}
/*
* CKoalaClassFactory::CreateInstance
*
* Purpose:
* Instantiates a Koala object returning an interface pointer.
*
* Parameters:
* pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
* being used in an aggregation.
* riid REFIID identifying the interface the caller
* desires to have for the new object.
* ppvObj PPVOID in which to store the desired
* interface pointer for the new object.
*
* Return Value:
* HRESULT NOERROR if successful, otherwise E_NOINTERFACE
* if we cannot support the requested interface.
*/
STDMETHODIMP CKoalaClassFactory::CreateInstance(LPUNKNOWN pUnkOuter
, REFIID riid, PPVOID ppvObj)
{
PCKoala pObj;
HRESULT hr;
*ppvObj=NULL;
hr=ResultFromScode(E_OUTOFMEMORY);
//Verify that a controlling unknown asks for IUnknown
if (NULL!=pUnkOuter && IID_IUnknown!=riid)
return ResultFromScode(CLASS_E_NOAGGREGATION);
//Create the object telling us to notify us when it's gone.
pObj=new CKoala(pUnkOuter, ObjectDestroyed);
if (NULL==pObj)
{
//This starts shutdown if there are no other objects.
g_cObj++;
ObjectDestroyed();
return hr;
}
if (pObj->Init())
hr=pObj->QueryInterface(riid, ppvObj);
g_cObj++;
/*
* Kill the object if initial creation or Init failed. If
* the object failed, we handle the g_cObj increment above
* in ObjectDestroyed.
*/
if (FAILED(hr))
{
delete pObj;
ObjectDestroyed(); //Handle shutdown cases.
}
return hr;
}
/*
* CKoalaClassFactory::LockServer
*
* Purpose:
* Increments or decrements the lock count of the serving
* IClassFactory object. When the number of locks goes to
* zero and the number of objects is zero, we shut down the
* application.
*
* Parameters:
* fLock BOOL specifying whether to increment or
* decrement the lock count.
*
* Return Value:
* HRESULT NOERROR always.
*/
STDMETHODIMP CKoalaClassFactory::LockServer(BOOL fLock)
{
if (fLock)
g_cLock++;
else
{
g_cLock--;
/*
* Fake an object destruction: this centralizes
* all the shutdown code in the ObjectDestroyed
* function, eliminating duplicate code here.
*/
g_cObj++;
ObjectDestroyed();
}
return NOERROR;
}
//CHAPTER6MOD
//Class factory implementation of IExternalConnection
/*
* CImpIExternalConnection::CImpIExternalConnection
* CImpIExternalConnection::~CImpIExternalConnection
*
* Parameters (Constructor):
* pObj PCKoalaClassFactory of the object we're in.
* pUnkOuter LPUNKNOWN to which we delegate.
*/
CImpIExternalConnection::CImpIExternalConnection
(PCKoalaClassFactory pObj, LPUNKNOWN pUnkOuter)
{
m_cRef=0;
m_pObj=pObj;
m_pUnkOuter=pUnkOuter;
m_cStrong=0;
m_cWeak=0;
return;
}
CImpIExternalConnection::~CImpIExternalConnection(void)
{
return;
}
/*
* CImpIExternalConnection::QueryInterface
* CImpIExternalConnection::AddRef
* CImpIExternalConnection::Release
*
* Purpose:
* Delegating IUnknown members for CImpIExternalConnection.
*/
STDMETHODIMP CImpIExternalConnection::QueryInterface(REFIID riid
, LPVOID *ppv)
{
return m_pUnkOuter->QueryInterface(riid, ppv);
}
STDMETHODIMP_(ULONG) CImpIExternalConnection::AddRef(void)
{
++m_cRef;
return m_pUnkOuter->AddRef();
}
STDMETHODIMP_(ULONG) CImpIExternalConnection::Release(void)
{
--m_cRef;
return m_pUnkOuter->Release();
}
/*
* CImpIExternalConnection::AddConnection
*
* Purpose:
* Informs the object that a strong connection has been made to it.
*
* Parameters:
* dwConn DWORD identifying the type of connection, taken
* from the EXTCONN enumeration.
* dwReserved DWORD reserved. This is used inside OLE and
* should not be validated.
*
* Return Value:
* DWORD The number of connection counts on the
* object, used for debugging purposes only.
*/
STDMETHODIMP_(DWORD) CImpIExternalConnection::AddConnection
(DWORD dwConn, DWORD dwReserved)
{
DWORD dwRet;
TCHAR szTemp[80];
if (EXTCONN_STRONG & dwConn)
{
dwRet=++m_cStrong;
wsprintf(szTemp
, TEXT("AddConnection cStrong=%lu"), m_cStrong);
}
#ifdef WIN32
if (EXTCONN_WEAK & dwConn)
{
dwRet=++m_cWeak;
wsprintf(szTemp
, TEXT("ReleaseConnection cWeak=%lu"), m_cWeak);
}
#endif
#ifndef WIN32
ODS(szTemp);
#else
MessageBox(NULL, szTemp
, TEXT("EKoala3: CKoalaClassFactory::IExternalConnection")
, MB_OK);
#endif
return dwRet;
}
/*
* CImpIExternalConnection::ReleaseConnection
*
* Purpose:
* Informs an object that a connection has been taken away from
* it in which case the object may need to shut down.
*
* Parameters:
* dwConn DWORD identifying the type of connection,
* taken from the EXTCONN enumeration.
* dwReserved DWORD reserved. This is used inside OLE and
* should not be validated.
* dwRerved DWORD reserved
* fLastReleaseCloses BOOL indicating if the last call to this
* function should close the object.
*
* Return Value:
* DWORD The number of remaining connection counts on
* the object, used for debugging purposes only.
*/
STDMETHODIMP_(DWORD) CImpIExternalConnection::ReleaseConnection
(DWORD dwConn, DWORD dwReserved, BOOL fLastReleaseCloses)
{
DWORD dwRet;
TCHAR szTemp[80];
if (EXTCONN_STRONG & dwConn)
{
/*
* Note: We don't need to close ourselves when the last
* strong lock is removed; we're just implementing this
* interface for demonstration.
*/
dwRet=--m_cStrong;
wsprintf(szTemp
, TEXT("ReleaseConnection cStrong=%lu"), m_cStrong);
}
#ifdef WIN32
if (EXTCONN_WEAK & dwConn)
{
dwRet=--m_cWeak;
wsprintf(szTemp
, TEXT("ReleaseConnection cWeak=%lu"), m_cWeak);
}
#endif
#ifndef WIN32
ODS(szTemp);
#else
MessageBox(NULL, szTemp
, TEXT("EKoala3: CKoalaClassFactory::IExternalConnection")
, MB_OK);
#endif
return dwRet;
}
//End CHAPTER6MOD
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -