www.pudn.com > Ge_opc_Server_v1.rar > OPCUTIL.CPP
// OPCUtil.cpp
//
// This file contains some string utility functions
// for the OPC Server server.
//
//
// (c) COPYRIGHT 1996, INTELLUTION INC.
// ALL RIGHTS RESERVED
//
// Original Author: Al Chisholm
//
// Modification Log:
// Vers Date By Notes
// ---- -------- --- -----
// 0.01 12/31/96 ACC
//
//
#define WIN32_LEAN_AND_MEAN
#include "OPCLHEpipeview.h"
#include "string.h"
///////////////////////////////////////
// Utility function to malloc a BSTR
// and copy a WSTR into it
//
///////////////////////////////////////
BSTR BSTRFromWSTR(const WCHAR * s, IMalloc *pmem)
{
BSTR bstr;
int length;
length = wcslen(s) + 1; // number of WCHARS plus NUL
// get space (add 4 more bytes for the BSTR size DWORD)
//
if(pmem) bstr = (BSTR) pmem->Alloc( length + sizeof(DWORD));
else bstr = new WCHAR[ length + 2];
if(bstr == NULL) return NULL;
bstr[0] = length ; // includes NUL but not DWORD
bstr[1] = 0;
wcscpy(&bstr[2] , s);
return &bstr[2];
}
///////////////////////////////////////
// Utility function to malloc a WSTR
// and copy a BSTR into it
//
///////////////////////////////////////
WCHAR * WSTRFromBSTR(BSTR bstr, IMalloc *pmem)
{
WCHAR *wstr;
bstr--;
bstr--;
if(pmem) wstr = (WCHAR*) pmem->Alloc( bstr[0] );
else wstr = new WCHAR[bstr[0]];
if(wstr == NULL) return NULL;
wcscpy(wstr, &bstr[2]);
return wstr;
}
///////////////////////////////////////
// Utility function to Free a BSTR
// which was allocated above
//
///////////////////////////////////////
void BSTRFree(BSTR bstr, IMalloc *pmem)
{
if(bstr == NULL) return;
bstr--;
bstr--;
if(pmem) pmem->Free(bstr);
else delete [] bstr;
}
///////////////////////////////////////
// Clone a Wide String
//
///////////////////////////////////////
WCHAR * WSTRClone(const WCHAR *oldstr, IMalloc *pmem)
{
WCHAR *newstr;
if(pmem) newstr = (WCHAR*)pmem->Alloc(sizeof(WCHAR) * (wcslen(oldstr) + 1));
else newstr = new WCHAR[wcslen(oldstr) + 1];
if(newstr) wcscpy(newstr, oldstr);
return newstr;
}
///////////////////////////////////////
// Free a Wide String
//
///////////////////////////////////////
void WSTRFree(WCHAR * c, IMalloc *pmem)
{
if(c == NULL) return;
if(pmem) pmem->Free(c);
else delete c;
}