[英]argument of type “char *” is incompatible with parameter of type "STRSAFE_LPCWSTR
I have the following: 我有以下几点:
DYNAMIC_TIME_ZONE_INFORMATION dtzRecorder;
GetDynamicTimeZoneInformation(&dtzRecorder);
I normally do the following to copy a new name: 我通常会执行以下操作来复制新名称:
StringCchCopy(dtzRecorder.TimeZoneKeyName, 128, L"GMT Standard Time");
But now I need to do the following: 但现在我需要执行以下操作:
char tzKey[51];
std::string timezone("someTimeZOneName");
strncpy_s(MyStruct.tzKey, timezone.c_str(), _TRUNCATE);
StringCchCopy(dtzRecorder.TimeZoneKeyName, 128, MyStruct.tzKey); <--Error
But I get the error: 但是我得到了错误:
argument of type "char *" is incompatible with parameter of type "STRSAFE_LPCWSTR" “ char *”类型的参数与“ STRSAFE_LPCWSTR”类型的参数不兼容
How can I copy this to dtzRecorder.TimeZoneKeyName?? 如何将其复制到dtzRecorder.TimeZoneKeyName?
The basic problem is that dtzRecorder.TimeZoneKeyName
is a wide string ( wchar_t[]
), but tzKey
is a narrow string ( char[]
). 基本问题是
dtzRecorder.TimeZoneKeyName
是一个宽字符串( wchar_t[]
),而tzKey
是一个窄字符串( char[]
)。
The easiest way to solve it would be to use wchar_t
for tzKey
, too: 解决此问题的最简单方法是也将
wchar_t
用于tzKey
:
wchar_t tzKey[51];
std::wstring timezone(L"someTimeZOneName");
wcsncpy_s(MyStruct.tzKey, timezone.c_str(), _TRUNCATE);
StringCchCopy(dtzRecorder.TimeZoneKeyName, 128, MyStruct.tzKey);
LPSTR
is Microsoft for "Long Pointer to STRing" or char *
, LPWSTR
is Microsoft for "Long Pointer to Wide-c STring" or wchar_t *
. LPSTR
是Microsoft的“指向STRing的长指针”或char *
, LPWSTR
是Microsoft的“指向Wide-c STring的长指针”或wchar_t *
。 Additionally LPCSTR
and LPCWSTR
refer to const variants. 另外,
LPCSTR
和LPCWSTR
引用const变体。
The error you see comes from passing an LPCSTR
(const character pointer) to a function expecting an LPWSTR
(non-const unicode/wide character pointer). 您看到的错误来自将
LPCSTR
(常量字符指针)传递给需要LPWSTR
(非常量Unicode /宽字符指针)的函数。
Wide string constants are denoted with an L
prefix ( L"wide"
), generally have the type wchar_t*
and require a variant of std::string
called std::wstring
. 宽字符串常量用
L
前缀( L"wide"
)表示,通常具有wchar_t*
类型,并且需要std::string
的变体std::wstring
。
Which is the default for most Windows system calls is handled by a general project setting "Character Set", if it is "Unicode" then wide strings are required. 大多数Windows系统调用的默认设置由常规项目设置“字符集”处理,如果为“ Unicode”,则需要宽字符串。 Support for this is provided by
<tchar.h>
see https://msdn.microsoft.com/en-us/library/dybsewaf.aspx <tchar.h>
提供对此的支持,请参见https://msdn.microsoft.com/zh-cn/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);
Alternatively you can just be explicit about using wides 另外,您也可以明确使用范围
wchar_t tzKey[51]; // note: this is not 51 bytes
std::wstring timezone(L"timezonename");
wscncpy_s(tzKey, timezone.c_str(), _TRUNCATE);
As an aside, why not simply do this: 顺便说一句,为什么不简单地这样做:
std::wstring timezone(L"tzname");
timezone.erase(50); // limit length
Why waste time copying the value when you could just insert a null-terminator at the limit? 当您只可以在限制处插入空终止符时,为什么要浪费时间来复制值?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.