让Irrlicht支持中文后,编程过程中不免经常会用到char* 到wchar_t*的转换,看了上次转载的那篇关于L"xx"的文章,写了个小函数供使用.
#define xmalloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (s))
#define xfree(p) HeapFree (GetProcessHeap(), 0, (p))
#define xfree(p) HeapFree (GetProcessHeap(), 0, (p))
wchar_t *str2wstrptr(const char *pStr)
{
PSTR pMultiByteStr = (PSTR)pStr;
PWSTR pWideCharStr;
int nLenOfWideCharStr;
// 利用API函数MultiByteToWideChar()来把a转化成unicode字符
nLenOfWideCharStr = MultiByteToWideChar( CP_ACP, 0, pMultiByteStr, -1, NULL, 0);
pWideCharStr = (PWSTR)xmalloc(nLenOfWideCharStr * sizeof(WCHAR));
MultiByteToWideChar( CP_ACP, 0, pMultiByteStr, -1, pWideCharStr, nLenOfWideCharStr );
return pWideCharStr;
}
{
PSTR pMultiByteStr = (PSTR)pStr;
PWSTR pWideCharStr;
int nLenOfWideCharStr;
// 利用API函数MultiByteToWideChar()来把a转化成unicode字符
nLenOfWideCharStr = MultiByteToWideChar( CP_ACP, 0, pMultiByteStr, -1, NULL, 0);
pWideCharStr = (PWSTR)xmalloc(nLenOfWideCharStr * sizeof(WCHAR));
MultiByteToWideChar( CP_ACP, 0, pMultiByteStr, -1, pWideCharStr, nLenOfWideCharStr );
return pWideCharStr;
}
调用str2wstrptr后用xfree释放xmalloc申请到的空间.
示例:
char name[] = "岁月无声";
wchar_t* pmyname= str2wstrptr(name);
转换后就可以在irrlicht用了:
ITextSceneNode *bill = Scene->addTextSceneNode(fonts[1],pmyname);
xfree(pmyname);
可以不用Scene->addTextSceneNode(fonts[1],L"岁月无声");了.