windows 使用 strcpy_s 作为 TCHAR 指针(Microsoft 特定)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2946916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:33:20  来源:igfitidea点击:

using strcpy_s for TCHAR pointer (Microsoft Specific)

c++windowsvisual-c++

提问by Cheok Yan Cheng

I was wondering which is the correct way?

我想知道哪个是正确的方法?

_tcscpy(tchar_pointer, _tcslen(tchar_pointer), _T("Hello World"));

or

或者

_tcscpy(tchar_pointer, _tcsclen(tchar_pointer), _T("Hello World"));

or

或者

_tcscpy(tchar_pointer, ???, _T("Hello World"));

回答by bara

Assuming you are using _tcscpy_sand not _tcscpy, the second parameter should be the actual size of the array, and not the length of the currently contained string. For example:

假设您使用的是_tcscpy_sand not _tcscpy,第二个参数应该是数组的实际大小,而不是当前包含的字符串的长度。例如:

TCHAR dest[20];
_tcscpy_s(dest, _countof(dest), _T("Hello"));

You can even use the 2-parameter version that will not require the size parameter:

您甚至可以使用不需要 size 参数的 2 参数版本:

_tcscpy_s(dest, _T("Hello"));

If tchar_pointeris actually a pointer and not an array (as implied by its name) you need to be very careful when determining what its actual capacity is. More context would be needed to suggest the right approach, but using the length of the contained string to compute the size of the buffer is almost certainly the wrong approach.

如果tchar_pointer实际上是一个指针而不是一个数组(如其名称所暗示的那样),则在确定其实际容量时需要非常小心。需要更多上下文来建议正确的方法,但使用包含的字符串的长度来计算缓冲区的大小几乎肯定是错误的方法。

回答by egrunin

the tchar pointer are coming from external, and my side has no idea how large the buffer referred by the pointer is

tchar 指针来自外部,我这边不知道指针引用的缓冲区有多大

If this is so, then none of these do what you want.

如果是这样,那么这些都不是你想要的。

The way all the "safe" functions work is that you tell them how big the target buffer is.

所有“安全”函数的工作方式是告诉他们目标缓冲区有多大。

You don't know? You can't use those functions.

你不知道?你不能使用这些功能。

int buffer_size = _tcslen(xxx) * sizeof(TCHAR)

This will not work. All it will guarantee is that the string copied is not longer than whatever is already in the buffer. If the buffer has not been initialized, it will fail; if the buffer begins with a '\0', nothing will be copied; and so forth.

这是行不通的。它将保证复制的字符串不长于缓冲区中已有的字符串。如果缓冲区没有被初始化,就会失败;如果缓冲区以 a 开头'\0',则不会复制任何内容;等等。

回答by Windows programmer

bara's answer is very important for you, because the correct answer differs from all of the ideas you listed. But bara didn't explain to you the difference between _tcslen and _tcsclen, so I'll do that.

bara 的答案对您来说非常重要,因为正确答案与您列出的所有想法都不同。但是bara 没有向你解释_tcslen 和_tcsclen 之间的区别,所以我会这样做。

In a Unicode compilation, _tcslen and _tcsclen will both count the number of TCHARs in the current value of the string. Each TCHAR is one wchar_t.

在 Unicode 编译中,_tcslen 和 _tcsclen 都将计算字符串当前值中 TCHAR 的数量。每个 TCHAR 是一个 wchar_t。

In a Multibyte compilation, _tcslen will count the number of TCHARs in the current value of the string. Each TCHAR is one char. But _tcsclen will count the number of multibyte characters in the current value of the string. Each multibyte character is one or more TCHARs, each multibyte character is one or more chars. Different multibyte characters can have different lengths, depending on the code page and the individual characters.

在多字节编译中,_tcslen 将计算字符串当前值中 TCHAR 的数量。每个 TCHAR 是一个字符。但是 _tcsclen 将计算字符串当前值中的多字节字符数。每个多字节字符是一个或多个 TCHAR,每个多字节字符是一个或多个字符。不同的多字节字符可以有不同的长度,这取决于代码页和单个字符。

回答by sth

If tchar_pointeris a pointer (as opposed to an array) you are supposed to know yourself how big the pointed-to buffer is. If you don't know, you cannot find out from the raw pointer, strcpy_sdoesn't help there.

如果tchar_pointer是指针(而不是数组),您应该知道自己指向的缓冲区有多大。如果你不知道,你就无法从原始指针中找到,在strcpy_s那里没有帮助。