VarWstrCat

Append wide characters, or another BSTR, to a BSTR.
////////////////////////////////////////////////////////////////////////////////////////
// VarWstrCat
//
// To a BSTR, efficiently append (concatenate) wide characters (or another BSTR).
//
// Initial value of the BSTR can be NULL.
// 2003 - Glenn Slayden
//
////////////////////////////////////////////////////////////////////////////////////////
HRESULT VarWstrCat(BSTR *pbs,TCHAR *wsz)
{
	if (!wsz)
		return NOERROR;

	if (!pbs)
		return E_POINTER;

	int c0 = *pbs ? SysStringLen(*pbs) : 0;
	int c1 = lstrlen(wsz);
	int c = c0 + c1;

	BSTR bs = SysAllocStringLen(NULL,c);
	if (!bs)
		return E_OUTOFMEMORY;

	if (*pbs)
	{
		memcpy(bs,*pbs,c0 * sizeof(TCHAR));
		SysFreeString(*pbs);
	}
	memcpy(&bs[c0],wsz,c1 * sizeof(TCHAR));

	*pbs = bs;

	return NOERROR;
}