Click here to Skip to main content
15,896,111 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 214.5K   2.5K   42  
Two classes that provide richer interfaces and easier semantics to pass classes via COM/DCOM
/////////////////////////////////////////////////////////////////
//                                                             //
//           Usefull helper functions                          //
//-------------------------------------------------------------//
//                                                             //
//             By Eugene Khodakovsky                           //
//                  June, 2000                                 //
//             ekhodakovsky@yahoo.com                          //
/////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <fstream.h>
#include "SLUtil.h"


// C:/WINDOWS
const CString GetWindowsDirectory()
{
	TCHAR	path[MAX_PATH] = {0};
	::GetWindowsDirectory(path,sizeof(path)/sizeof(TCHAR));
	return CString(path);
}

// C:/WINDOWS/SYSTEM
const CString GetSystemDirectory()
{
	TCHAR	path[MAX_PATH] = {0};
	::GetSystemDirectory(path,sizeof(path)/sizeof(TCHAR));
	return CString(path);
}

const CString GetUserName()
{
	TCHAR	name[MAX_PATH] = {0};
	DWORD	nSize = sizeof(name)/sizeof(TCHAR);
	::GetUserName(name,&nSize);
	return CString(name);
}

const CString GetComputerName()
{
	TCHAR	name[MAX_PATH] = {0};
	DWORD	nSize = sizeof(name)/sizeof(TCHAR);
	::GetComputerName(name,&nSize);
	return CString(name);
}

// C
const CString GetSystemDrive()
{
	CString	strPath;
	strPath = GetSystemDirectory();
	int idx = strPath.Find(':');
	ATLASSERT(idx != 0);
	if(idx != 0)
	{
		strPath = strPath.Left(idx);
	}
	return strPath;
}

//////////////////////////////////////////////////////////////////////////////
// CComBSTR
CComBSTR operator+(CComBSTR& v1,CComBSTR& v2)
{
	CComBSTR bstr = v1; bstr += v2; return bstr;
}
CComBSTR operator+(CString v1,CComBSTR& v2)
{
	CComBSTR bstr = v1; bstr += v2; return bstr;
}
CComBSTR operator+(LPCSTR v1,CComBSTR& v2)
{
	CComBSTR bstr = v1; bstr += v2; return bstr;
}
CComBSTR operator+(CComBSTR& v1,LPCSTR v2)
{
	CComBSTR bstr = v1; bstr += v2; return bstr;
}
CComBSTR operator+(CComBSTR& v1,CString v2)
{
	CComBSTR bstr = v1; bstr += CComBSTR(v2); return bstr;
}

//////////////////////////////////////////////////////////////////////////////
// GUID helper

void ProgIDFromCLSID(const CLSID& clsid,CComBSTR& comBSTR)
{
	LPOLESTR bstr;
	::ProgIDFromCLSID (clsid,&bstr);
	comBSTR = CString(bstr);
	CoTaskMemFree(bstr);
}
void StringFromCLSID(const CLSID& clsid,CComBSTR& comBSTR)
{
	LPOLESTR bstr;
	::StringFromCLSID (clsid,&bstr);
	comBSTR = CString(bstr);
	CoTaskMemFree(bstr);
}
void StringFromIID(const CLSID& clsid,CComBSTR& comBSTR)
{
	LPOLESTR bstr;
	::StringFromIID (clsid,&bstr);
	comBSTR = CString(bstr);
	CoTaskMemFree(bstr);
}
void CLSIDFromString(CComBSTR& comBSTR,CLSID& clsid)
{
	LPOLESTR bstr;
	comBSTR.CopyTo(&bstr);
	::CLSIDFromString(bstr,&clsid);
	::SysFreeString(bstr);
}

void IIDFromString(CComBSTR& comBSTR,CLSID& clsid)
{
	LPOLESTR bstr;
	comBSTR.CopyTo(&bstr);
	::IIDFromString(bstr,&clsid);
	::SysFreeString(bstr);
}
void CLSIDFromProgID(CComBSTR& comBSTR,CLSID& clsid)
{
	LPOLESTR bstr;
	comBSTR.CopyTo(&bstr);
	::CLSIDFromProgID(bstr,&clsid);
	::SysFreeString(bstr);
}

//////////////////////////////////////////////////////////////////////////////
// Notepad

void ShowInNotepad(const CString& strText)
{
	CString strPathName = GetSystemDrive() + _T(":/Temp/SLText.txt");
	ofstream file(strPathName);
	CString str(strText); 
	str.Replace('\r',' ');
	file << str;
	file.close();
	::WinExec("notepad.exe " + strPathName,true);
}

void ShowInNotepad(const CComBSTR& bstrText)
{
	CString strText = bstrText;
	ShowInNotepad(strText);
}

//////////////////////////////////////////////////////////////////////////////
// String Tokenaizer

CString GetNextToken(CString& strSrc, const CString strDelim)
{
	CString token;
	int idx = strSrc.FindOneOf(strDelim);
	if(idx != -1)
	{
		token  = strSrc.Left(idx);
		strSrc = strSrc.Right(strSrc.GetLength() - (idx + 1) );
	}
	else
	{
		token = strSrc;
		strSrc.Empty();
	}
	return token;
}

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