LPSTR
是 Microsoft 的“Long Pointer to STRing”或char *
,LPWSTR
是 Microsoft 的“Long Pointer to Wide-c STring”或wchar_t *
. 另外LPCSTR
并LPCWSTR
参考 const 变体。
您看到的错误来自将LPCSTR
(const 字符指针)传递给期望LPWSTR
(非常量 unicode/宽字符指针)的函数。
宽字符串常量用L
前缀 ( L"wide"
) 表示,通常具有类型wchar_t*
并需要std::string
调用的变体std::wstring
。
这是大多数 Windows 系统调用的默认设置,由通用项目设置“字符集”处理,如果是“Unicode”,则需要宽字符串。<tchar.h>
请参阅https://msdn.microsoft.com/en-us/library/dybsewaf.aspx提供对此的支持
#include <tchar.h>
// tchar doesn't help with std::string/std::wstring, so use this helper.
#ifdef _UNICODE
#define TSTRING std::wstring
#else
#define TSTRING std::string
#endif
// or as Matt points out
typedef std::basic_string<_TCHAR> TSTRING;
// Usage
TCHAR tzKey[51]; // will be char or wchar_t accordingly
TSTRING timezone(_T("timezonename")); // string or wstring accordingly
_tscncpy_s(tzKey, timezone.c_str(), _TRUNCATE);
或者,您可以明确地使用宽
wchar_t tzKey[51]; // note: this is not 51 bytes
std::wstring timezone(L"timezonename");
wscncpy_s(tzKey, timezone.c_str(), _TRUNCATE);
顺便说一句,为什么不简单地这样做:
std::wstring timezone(L"tzname");
timezone.erase(50); // limit length
当您可以在限制处插入空终止符时,为什么要浪费时间复制值?