|
// StringHelpers.h
//
// Author : David Shepherd
// Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_STRINGHELPERS_H__4468EFA1_2854_11D6_B6A6_856C06A39D46__INCLUDED_)
#define AFX_STRINGHELPERS_H__4468EFA1_2854_11D6_B6A6_856C06A39D46__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// helper for client -> server
template <class T> // takes most standard containers
auto_array_ptr<LPCOLESTR> ComClientStringArrayToServer(const T &Container)
{
// create a string array suitable for passing over com
auto_array_ptr<LPCOLESTR> pArray(new LPCOLESTR [Container.size()]);
// initialise each element
DWORD Index=0;
T::const_iterator iter=Container.begin();
while(iter!=Container.end())
{
pArray.get()[Index++]=iter->c_str();
iter++;
}
return pArray;
}
// helper for client <- server
template <class T> // takes most standard containers
void ComClientStringArrayFromServer(DWORD Count,LPCOLESTR *pArray,T &Container)
{
// check parameters
if(pArray==NULL)
{
throw std::exception();
}
// place each string into the container and free the remote storage
for(long l=0; l<(long)Count; l++)
{
LPCOLESTR pString=pArray[l];
if(pString==NULL)
{
throw std::exception();
}
Container.push_back(pString);
CoTaskMemFree((LPVOID)pString);
}
// free the array remote storage
CoTaskMemFree((LPVOID)pArray);
}
// helper for server -> client
template <class T> // takes most standard containers
LPCOLESTR *ComServerStringArrayToClient(const T &Container)
{
// create a string array suitable for passing over com
LPCOLESTR *pArray=
(LPCOLESTR *)CoTaskMemAlloc(Container.size()*sizeof(LPCOLESTR));
if(pArray==NULL)
{
throw std::exception();
}
// initialise each element
DWORD Index=0;
T::const_iterator iter=Container.begin();
while(iter!=Container.end())
{
LPOLESTR pString=
(LPOLESTR)CoTaskMemAlloc((iter->length()+1)*sizeof(OLECHAR));
if(pString==NULL)
{
throw std::exception();
}
wcscpy(pString,iter->c_str());
pArray[Index++]=pString;
iter++;
}
return pArray;
}
// helper for server <- client
template <class T> // takes most standard containers
void ComServerStringArrayFromClient(DWORD Count,LPCOLESTR *pArray,T &Container)
{
// check parameters
if(pArray==NULL)
{
throw std::exception();
}
// place each string into the container
for(long l=0; l<(long)Count; l++)
{
LPCOLESTR pString=pArray[l];
if(pString==NULL)
{
throw std::exception();
}
Container.push_back(pString);
}
}
#endif // !defined(AFX_STRINGHELPERS_H__4468EFA1_2854_11D6_B6A6_856C06A39D46__INCLUDED_)
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.