Re: ATL COM library - BSTR question
"Ismo Salonen" <Ismo.Salonen@codeit.fi> ha scritto nel messaggio
news:evVJa6dTIHA.5516@TK2MSFTNGP02.phx.gbl...
Maybe he is using UTF-8 ? then the conversion is possible with
CU2W / CW2U macros (from memory)
Are these macros new additions to VS2008?
For VC++7.1 I developed something similar, based on ATL conversion helper
classes (CA2W/CW2A):
<code>
//----------------------------------------------------------------------------
// Class: CW2UEX
// Descr: Converts from Unicode UTF-16 (WideChars) to Unicode UTF-8
//----------------------------------------------------------------------------
template< int t_nBufferLength = 128 >
class CW2UEX
{
public:
CW2UEX( LPCWSTR psz ) throw(...) :
m_psz( m_szBuffer )
{
Init( psz );
}
~CW2UEX() throw()
{
if( m_psz != m_szBuffer )
{
free( m_psz );
}
}
operator LPSTR() const throw()
{
return( m_psz );
}
private:
void Init( LPCWSTR psz ) throw(...)
{
if (psz == NULL)
{
m_psz = NULL;
return;
}
int nLengthW = lstrlenW( psz )+1;
// One Unicode UTF-16 character could be converted
// up to 4 UTF-8 characters
int nLengthUtf8 = nLengthW * 4;
if( nLengthUtf8 > t_nBufferLength )
{
m_psz = static_cast< LPSTR >( malloc( nLengthUtf8*
sizeof( char ) ) );
if (m_psz == NULL)
{
AtlThrow( E_OUTOFMEMORY );
}
}
if (::WideCharToMultiByte( CP_UTF8, 0, psz, nLengthW,
m_psz, nLengthUtf8, NULL, NULL ) == 0)
{
AtlThrowLastWin32();
}
}
public:
LPSTR m_psz;
char m_szBuffer[t_nBufferLength];
private:
CW2UEX( const CW2UEX& ) throw();
CW2UEX& operator=( const CW2UEX& ) throw();
};
typedef CW2UEX<> CW2U;
</code>
Giovanni