C++ 从char*到wchar\t的转换

C++ 从char*到wchar\t的转换,c++,winapi,C++,Winapi,我需要将char*转换为wchar。 以下是我的表现 char * retrunValue= getData(); size_t origsize = strlen(returnValue) + 1; const size_t newsize = 200; size_t convertedChars = 0; wchar_t wcstring[newsize]; mbstowcs_s(&convertedChars, wcstring, origsize, returnValue, _T

我需要将char*转换为wchar。 以下是我的表现

char * retrunValue= getData();
size_t origsize = strlen(returnValue) + 1;
const size_t newsize = 200;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, returnValue, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
函数的作用是:返回字符*值,例如“C:/Documents and Settings” 当我试图打印转换后的值“wcstring”时:该值不正确:类似于“C:/Documen9”或垃圾。 1-请告诉我以这种方式将char*转换为wchar安全吗,就像我现在做的那样 2-如何获取getData()函数返回的原始值

谢谢, 问候

更新:

size_t origsize = strlen(returnValue) + 1;
const size_t newsize = 200;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
wsprintf(wcstring, newsize, L"%S (wchar_t *)", returnValue);
添加了这个,但它说。 类型为“size\u t”的参数与类型为“LPCWSTR”的参数不兼容

你穿错了尺寸

您传递的大小不正确。

请勿使用
mbstowcs()
进行此类转换,除非
char*
确实指向多字节字符串(并非每个
char
字符串都是多字节字符串!)。使用127以上的
char
值(例如umlauts和其他特殊字符)时,该字符串可能会中断

由于您仍想使用另一个字符串,只需使用
wsprintf()

除非
char*
确实指向多字节字符串(并非每个
char
字符串都是多字节字符串!),否则不要使用
mbstowcs()
进行此类转换。使用127以上的
char
值(例如umlauts和其他特殊字符)时,该字符串可能会中断

由于您仍想使用另一个字符串,只需使用
wsprintf()


请参见此处了解我的一个好函数:请参见此处了解我的一个好函数:谢谢我尝试过。wcstring值为0x023bf9e8“C:\DocumÈ(wchar_t*)”,返回值为0x023bf890。而我的原始值c:/documents和settings。我不知道为什么我的返回值是错误的。谢谢,我试过了。wcstring值为0x023bf9e8“C:\DocumÈ(wchar_t*),返回值为0x023bf890“C:”而我的原始值为C:/文档和设置。我不知道为什么返回的值是错误的。
mbstowcs_s(&convertedChars, wcstring, newsize, returnValue, _TRUNCATE);
                                      ^^^
// Visual Studio
wsprintf(wcstring, newsize, L"%S (wchar_t *)", returnValue);

// GCC
wsprintf(wcstring, newsize, L"%ls (wchar_t *)", returnValue);