char * to unsigned short *

hi,
I have to pass a string to a function, which takes input in "unsigned short *" format? How can I do this conversion?

Thanks in advance,
Prashant

[168 byte] By [PrashantTholia] at [2007-12-24]
# 1

If you function actually requires a Unicode string defined as unsigned short * (instead of usual const wchar_t *), it seems you first should convert your char * string to wchar_t * one (i.e. to Unicode), and then cast the pointer to unsigned short *.

One of solutions (for Visual Studio 2005), based on CA2W macro, is:

#include <atlbase.h>

#include <atlconv.h>

. . .

char s[] = "My string";

CA2W w = s;

unsigned short * p = (unsigned short *)(LPWSTR)w;

myFunction(p);

I hope this helps.

Viorel. at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2
In addition to Viorel's (hope I spelled right) comment, if your windows application is unicode ready make use of TCHAR data types and _T macros for string definitions and also use "t" set of string copy and other string related functions. So that your source code will be compilable in both Unicode and ASCII builds.
Sarath. at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 3
Additionally, see this FAQ.
MariusBancila at 2007-8-31 > top of Msdn Tech,Visual C++,Visual C++ Language...