Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Understanding Custom Marshaling Part 1

, 18 Aug 2006
Learn the fundamental principles of COM custom marshaling by code examples.
custommarshaling01_src.zip
utilities
BasicSample01
Interfaces
Implementations
Clients
BasicSample01Interfaces
BasicSample01Interfaces.aps
BasicSample01Interfaces.def
BasicSample01Interfaces.ncb
BasicSample01Interfaces.rgs
BasicSample01Interfacesps.def
Immutable.rgs
ImmutableObjectFactory.rgs
BasicSample01Interfaces.vcproj.vspscc
BasicSample01InterfacesPS.vcproj.vspscc
BasicSample01InterfacesImpl
BasicSample01InterfacesImpl.aps
BasicSample01InterfacesImpl.def
BasicSample01InterfacesImpl.rgs
BasicSample01InterfacesImplps.def
ImmutableImpl.rgs
ImmutableObjectFactoryImpl.rgs
BasicSample01InterfacesImpl.ncb
BasicSample01InterfacesImpl.vcproj.vspscc
BasicSample01InterfacesImplPS.vcproj.vspscc
VCConsoleClient01
VCConsoleClient03
VCConsoleClient02
VCConsoleClient01.ncb
VCConsoleClient03.ncb
VCConsoleClient02.ncb
#include <comdef.h>

#pragma warning( disable : 4290 )

// This code is derived from the CComVariant implementation
// of WriteToStream
inline unsigned long GetVariantSize(VARIANT& var)
{
	VARTYPE vt = var.vt;
	unsigned long sz = sizeof(VARTYPE);
	HRESULT hr = 0;

	switch (vt)
	{
	case VT_UNKNOWN:
	case VT_DISPATCH:
		{
			{
				IUnknown* punkVal = var.punkVal;		
				CComPtr<IPersistStream> spStream;
				if (punkVal != NULL)
				{
					hr = punkVal->QueryInterface(IID_IPersistStream, (void**)&spStream);
					if (FAILED(hr))
						return sz;
					ULARGE_INTEGER pcbSize;
					hr = spStream->GetSizeMax(&pcbSize);
					if (FAILED(hr))
						return sz;
					sz += pcbSize.LowPart;
					sz += 256;
				}
			}
		}
	case VT_UI1:
	case VT_I1:
		sz  +=  sizeof(BYTE);
		break;
	case VT_I2:
	case VT_UI2:
	case VT_BOOL:
		sz  +=  sizeof(short);
		break;
	case VT_I4:
	case VT_UI4:
	case VT_R4:
	case VT_INT:
	case VT_UINT:
	case VT_ERROR:
	case VT_NULL:
		sz  +=  sizeof(long);
		break;
	case VT_R8:
	case VT_CY:
	case VT_DATE:
		sz  +=  sizeof(double);
		break;
	case VT_BSTR:
		sz += ((SysStringLen(var.bstrVal) * sizeof(OLECHAR))+6);
		break;
	case VT_VARIANT:
		sz += sizeof(VARIANT);
		sz += GetVariantSize(*(var.pvarVal));
	default:
		break;
	}
	return sz;
}

// IStream / C++ stream operator support functions

template <class _T_> IStream& operator<<( IStream& stream, const _T_& data ) throw(_com_error)
{
	_com_util::CheckError(stream.Write( &data, sizeof(data), 0));
	return stream;
}

template <class _T_> IStream& operator>>( IStream& stream, _T_& data ) throw(_com_error)
{
	_com_util::CheckError(stream.Read( &data, sizeof(data), 0));
	return stream;
}

inline IStream& operator<<( IStream& stream, const char* data ) throw(_com_error)
{
	long length = (long)strlen(data) + 1;

	stream<<length;
	_com_util::CheckError(stream.Write( data, length*sizeof(*data), 0));

	return stream;
}

inline IStream& operator>>( IStream& stream, char* data ) throw(_com_error)
{
	long length;

	stream>>length;
	_com_util::CheckError(stream.Read( data, length*sizeof(*data), 0));
	return stream;
}

inline IStream& operator<<( IStream& stream, const wchar_t *data ) throw(_com_error)
{
	long length = (long)wcslen(data) + 1;

	stream << length;
	_com_util::CheckError(stream.Write( data, length*sizeof(*data), 0));

	return stream;
}

inline IStream& operator>>( IStream& stream, wchar_t *data ) throw(_com_error)
{
	long length;

	stream>>length;
	_com_util::CheckError(stream.Read( data, length * sizeof(*data), 0));
	return stream;
}

/*inline IStream& operator>>( IStream& stream, CString &data ) throw(_com_error)
{
	long length;
	LPWSTR wszData;

	stream>>length;

	wszData = data.GetBuffer(length+1);

	_com_util::CheckError(stream.Read( (void*)wszData, length * sizeof(*wszData), 0));

	data.ReleaseBuffer();
	return stream;
}*/

inline IStream& operator<<( IStream& stream, const _bstr_t& data ) throw(_com_error)
{
	CComBSTR bstr;

	bstr.Attach(data);

	_com_util::CheckError(bstr.WriteToStream(&stream));
	bstr.Detach();

	return stream;
}

inline IStream& operator>>( IStream& stream, _bstr_t& data ) throw(_com_error)
{
	CComBSTR bstr;

	_com_util::CheckError(bstr.ReadFromStream(&stream));

	_bstr_t  bs(bstr.Detach(),false);	// use ref counting to prevent copy

	data = bs;

	return stream;
}

inline IStream& operator>>( IStream& stream, CComBSTR& data ) throw(_com_error)
{
	_com_util::CheckError(data.ReadFromStream(&stream));
	return stream;
}

inline IStream& operator<<( IStream& stream, CComBSTR& data ) throw(_com_error)
{
	_com_util::CheckError(data.WriteToStream(&stream));
	return stream;
}

#pragma warning( default : 4290 )

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Lim Bio Liong
Web Developer
Singapore Singapore
Lim Bio Liong is a Specialist at a leading Software House in Singapore.
 
Bio has been in software development for over 10 years. He specialises in C/C++ programming and Windows software development.
 
Bio has also done device-driver development and enjoys low-level programming. Bio has recently picked up C# programming and has been researching in this area.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 18 Aug 2006
Article Copyright 2006 by Lim Bio Liong
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid