|

How to trigger a compile to use Unicode
Add _UNICODE and UNICODE to the defines section in the C++
program options tab.
Convert From ASCII To Unicode
If the desired result is simply to move from ASCII to
Unicode, then maybe the easiest thing to do is to move the data into a _bstr_t.
This data type has a constructor that accepts a char*.
In order to use _bstr_t, you will need to insert
#include <comdef.h> in the source file.
Convert Between ASCII And Unicode
The simplest way to convert between the two is to use the
USES_CONVERSION macro. Include ‘USES_CONVERSION’ anywhere in the code.
Thereafter, it is possible to use
|
T2COLE |
From a CONSTANT TSTR to a CONSTANT OLESTR |
|
T2OLE |
From a CONSTANT TSTR to an OLESTR |
|
OLE2CT |
From a CONSTANT OLESTR to a CONSTANT TSTR |
|
OLE2T |
From a CONSTANT OLESTR to a TSTR |
One little thing to keep in mind – do not include the
conversion in a loop. The reason you want to avoid that is because the memory
consumed by the conversion is not released until the USES_CONVERSION; goes out
of scope. If you must use the conversion within a loop then enclose the
conversion code with curly braces. When the close curly brace gets hit then the
USES_CONVERSION’ goes out of scope and memory is released.
These macros are dependent upon the inclusion of
atlbase.h.
So, enough of the talk. How are these macros actually used?
USES_CONVERSION;
CString cs = OLE2T( bstrSomeUnicodeString );
OK, that seems simple enough, but what is the purpose of
all of those macros that result in a CONSTANT? The point of those is to keep the
compiler happy. There are some functions that have a string argument that is
flagged as a CONSTANT. If, for example, you have a function that wants a
CONSTANT Unicode string pointer and you feed it with the results from T2OLE,
then the compiler will generate an error, saying that it can’t convert from
unsigned int * to const unsigned int *. Your choices are to explicitly add the
proper casting yourself or to use the version of the macro that produces the
CONSTANT pointer in the first place.
 |