将 char[] 转换为 LPCWSTR [英] Convert char[] to LPCWSTR
问题描述
谁能帮我更正这段代码:
Can anyone help me to correct this code:
char szBuff[64];
sprintf(szBuff, "%p", m_hWnd);
MessageBox(NULL, szBuff, L"Test print handler", MB_OK);
错误是,它无法将第二个参数转换为 LPCWSTR.
The error is, it cant convert the 2nd parameter to LPCWSTR.
推荐答案
对于这个特定案例,修复非常简单:
For this specific case, the fix is quite simple:
wchar_t szBuff[64];
swprintf(szBuff, L"%p", m_hWnd);
MessageBox(NULL, szBuff, L"Test print handler", MB_OK);
也就是说,始终使用 Unicode 字符串.一般来说,在 Windows 上编程时,使用 wchar_t
和 UTF-16 可能是最简单的.当然,这取决于您必须与其他系统进行多少交互.
That is, use Unicode strings throughout. In general, when programming on Windows, using wchar_t
and UTF-16 is probably the simplest. It depends on how much interaction with other systems you have to do, of course.
对于一般情况,如果您有一个 ASCII(或 char *
)字符串,请使用 WideCharToMultiByte 用于一般情况,或者 mbstowcs
如@Matthew 指出的更简单的情况(mbstowcs
如果字符串在当前 C 语言环境中有效).
For the general case, if you've got an ASCII (or char *
) string, use either WideCharToMultiByte for the general case, or mbstowcs
as @Matthew points out for simpler cases (mbstowcs
works if the string is in the current C locale).
这篇关于将 char[] 转换为 LPCWSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!