Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / WTL

Passing C++ classes across DCOM

Rate me:
Please Sign up or sign in to vote.
4.64/5 (10 votes)
15 Nov 20012 min read 213.9K   2.5K   42  
Two classes that provide richer interfaces and easier semantics to pass classes via COM/DCOM
/////////////////////////////////////////////////////////////////
//                                                             //
//                CConnection                                  //
//-------------------------------------------------------------//
//             By Eugene Khodakovsky                           //
//                  June, 2000                                 //
//             ekhodakovsky@yahoo.com                          //
//            Last Update: Nov., 2001                          //
/////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Connection.h"

#ifdef __AFX_H__
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif

//////////////////////////////////////////////////////////////////////
// CConnection
//////////////////////////////////////////////////////////////////////

CConnection::CConnection()
{
	Clear();
}

CConnection::~CConnection()
{
}

void CConnection::Clear()
{
	CDcomObj::Clear();
	
	m_strComputerName		= _T("");
	m_strUserName			= _T("");
	m_strPassword			= _T("");
	m_strServerName			= _T("");
	m_strApplicationName	= _T("");
	m_strConnectionHandle	= _T("");
}

void CConnection::Copy(const CSLObject& objectSrc)
{
	RIGHT(CConnection);
	CDcomObj::Copy(objectSrc);

	m_strComputerName	  = right.m_strComputerName;
	m_strUserName		  = right.m_strUserName;
	m_strPassword		  = right.m_strPassword;
	m_strServerName		  = right.m_strServerName;
	m_strApplicationName  = right.m_strApplicationName;
	m_strConnectionHandle = right.m_strConnectionHandle;
}

void CConnection::Write(CComSafeArray* safeArray,long& index)
{
	SA_BEGIN_WRITE(CDcomObj);

	SA_WRITE(m_strComputerName);
	SA_WRITE(m_strUserName);
	SA_WRITE(m_strPassword);
	SA_WRITE(m_strServerName);
	SA_WRITE(m_strApplicationName);
	SA_WRITE(m_strConnectionHandle);
}

void CConnection::Read(CComSafeArray* safeArray,long& index)
{
	SA_BEGIN_READ(CDcomObj);

	SA_READ(m_strComputerName);
	SA_READ(m_strUserName);
	SA_READ(m_strPassword);
	SA_READ(m_strServerName);
	SA_READ(m_strApplicationName);
	SA_READ(m_strConnectionHandle);
}

bool CConnection::IsEqual(const CSLObject& objectSrc)
{
	RIGHT(CConnection);
	return
	m_strComputerName	== right.m_strComputerName	&&
	m_strUserName		== right.m_strUserName		&&
	m_strPassword		== right.m_strPassword		&&
	m_strServerName		== right.m_strServerName	&&
	m_strApplicationName== right.m_strApplicationName	
	;
}

const CLSID CConnection::GetConnectionHandleCLSID() const
{
	CLSID clsid;
	CLSIDFromString(m_strConnectionHandle,&clsid);
	return clsid;
}

void CConnection::SetupConnectionHandle()
{
	CLSID clsid;
	CoCreateGuid(&clsid);

	LPOLESTR bstr;
	::StringFromCLSID (clsid,&bstr);
	m_strConnectionHandle = bstr;
	CoTaskMemFree(bstr);
}


//////////////////////////////////////////////////////////////////////
// CConnectionArray
//////////////////////////////////////////////////////////////////////

bool CConnectionArray::AddConnection(CConnection& conn)
{
	CConnection* pNewConn = new CConnection(conn);
	pNewConn->SetupConnectionHandle();
	Add(pNewConn);
	conn.m_strConnectionHandle = pNewConn->m_strConnectionHandle;
	return true;
}

static bool find_by_handle( CSLObject *pObj, void*pValue )
{
	CConnection*pConn		= (CConnection*)pObj;
	CComBSTR* pConnHandle	= (CComBSTR*)pValue;
	
	if(pConn->GetConnectionHandle() == *pConnHandle)
		return true;
	return false;
}

CConnection* CConnectionArray::Find(CComBSTR& connHandle)
{
    return FirstThat(find_by_handle,&connHandle);
}

CConnection* CConnectionArray::Remove(CComBSTR& connHandle)
{
	CConnection* pConn = Find(connHandle);
	if(pConn)
		CSLArray::Remove(pConn);
	return pConn;
}

static bool find_by_user( CSLObject *pObj, void*pValue )
{
	CConnection*pConn	= (CConnection*)pObj;
	CString* pUser		= (CString*)pValue;
	if(pConn->GetUserName() == *pUser)
		return true;
	return false;
}

CConnection* CConnectionArray::FindByUserName(CString strUser)
{
    return FirstThat(find_by_user,&strUser);
}

void CConnectionArray::RemoveAndDestroy(CConnection& conn)
{
	CComBSTR handle = conn.GetConnectionHandle();
	CConnection* pConn = Remove(handle);
	if(pConn)
	{
		pConn->ShutDown();
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions