18Oct/100
How to convert between ANSI and UNICODE strings?
Useful C++ code for converting between ANSI an UNICODE strings:
Ansi to Unicode:
char *ansistr = "Hello"; int a = lstrlenA(ansistr); BSTR unicodestr = SysAllocStringLen(NULL, a); ::MultiByteToWideChar(CP_ACP, 0, ansistr, a, unicodestr, a); ::SysFreeString(unicodestr);
Unicode to Ansi:
BSTR unicodestr = 0;
SomeCOMFunction(&unicodestr);
int a = SysStringLen(unicodestr)+1;
char *ansistr = new char[a];
::WideCharToMultiByte(CP_ACP,
0,
unicodestr,
-1,
ansistr,
a,
NULL,
NULL);
delete[] ansistr;
::SysFreeString(unicodestr);