简体   繁体   English

`STRSAFE_NO_TRUNCATION`和`STRSAFE_NULL_ON_FAILURE`值的区别是什么?

[英]What the difference for the `STRSAFE_NO_TRUNCATION` and `STRSAFE_NULL_ON_FAILURE` values?

The StringCchCopyEx function has values STRSAFE_NULL_ON_FAILURE and STRSAFE_NO_TRUNCATION for own flags. StringCchCopyEx函数值STRSAFE_NULL_ON_FAILURESTRSAFE_NO_TRUNCATION为自己的标志。 I am reading MSDN and I don't see their difference: 我正在阅读MSDN ,我看不出他们的区别:

STRSAFE_NULL_ON_FAILURE STRSAFE_NULL_ON_FAILURE

If the function fails, pszDest is set to an empty string ( TEXT("") ). 如果函数失败,则pszDest设置为空字符串( TEXT("") )。 In the case of a STRSAFE_E_INSUFFICIENT_BUFFER failure, any truncated string is overwritten. 如果STRSAFE_E_INSUFFICIENT_BUFFER失败,则会覆盖任何截断的字符串。

STRSAFE_NO_TRUNCATION STRSAFE_NO_TRUNCATION

As in the case of STRSAFE_NULL_ON_FAILURE , if the function fails, pszDest is set to an empty string ( TEXT("") ). STRSAFE_NULL_ON_FAILURE的情况STRSAFE_NULL_ON_FAILURE ,如果函数失败,则pszDest设置为空字符串( TEXT("") )。 In the case of a STRSAFE_E_INSUFFICIENT_BUFFER failure, any truncated string is overwritten. 如果STRSAFE_E_INSUFFICIENT_BUFFER失败,则会覆盖任何截断的字符串。

Also I don't see the difference in my code results: 另外,我没有看到代码结果的差异:

LPCTSTR pSrc = L"ASDFGHJK";

size_t charsCount = 5;
size_t buffer_size = charsCount * sizeof(TCHAR);
TCHAR *buffer = (TCHAR *) malloc(buffer_size);
ZeroMemory(buffer, buffer_size);    

// Now I generate the STRSAFE_E_INSUFFICIENT_BUFFER result...

LPTSTR pDestEnd = NULL;
size_t remaind = 0;

// The MEMORY content after the StringCchCopyEx executing:
// 00 00 53 00 44 00 46 00 00 00 fd fd fd fd ab ab ab ab ab
// DWORD dwFlags = STRSAFE_FILL_BYTE('X') | STRSAFE_NO_TRUNCATION;

// The MEMORY content after the StringCchCopyEx executing:
// 00 00 53 00 44 00 46 00 00 00 fd fd fd fd ab ab ab ab ab
DWORD dwFlags = STRSAFE_FILL_BYTE('X') | STRSAFE_NULL_ON_FAILURE;

HRESULT result = StringCchCopyEx(buffer, charsCount, pSrc, &pDestEnd, &remaind, dwFlags);

For both cases the memory has the same content... What the difference for the STRSAFE_NO_TRUNCATION and STRSAFE_NULL_ON_FAILURE values? 对于这两种情况,内存具有相同的内容...... STRSAFE_NO_TRUNCATIONSTRSAFE_NULL_ON_FAILURE值的区别是什么?

A safe string function with confuzzling documentation, what could go wrong? 一个安全的字符串函数与令人费解的文档,可能会出错? The difference between the two is exactly where the zero-terminator is written when you also use STRSAFE_FILL_ON_FAILURE with a non-zero filler: 当您将STRSAFE_FILL_ON_FAILURE与非零填充符一起使用时,两者之间的差异正是写入零终止符的位置:

  • Use STRSAFE_NO_TRUNCATION if you want to see the filler in the resulting string, the zero is written at the end. 如果要在结果字符串中查看填充符,请使用STRSAFE_NO_TRUNCATION,最后写入零。
  • Use STRSAFE_NULL_ON_FAILURE to always get an empty string. 使用STRSAFE_NULL_ON_FAILURE始终获取空字符串。 The filler is still written but past the 0. 填充物仍然写入但超过0。

Do beware of a flaw in the way the filler works. 请注意填料的工作方式存在缺陷。 The probably reason behind the confusing docs. 令人困惑的文档背后的可能原因。 It can only be a value between 0 and 255, that of course can only really work properly for StringCchCopyExA(). 它只能是介于0到255之间的值,当然,它只能在StringCchCopyExA()中正常工作。 The Unicode version will produce Chinese, "塘塘塘塘" in your case, pretty much forcing you to use 0 as the filler. 在你的情况下,Unicode版本将产生中文“塘塘塘塘”,几乎迫使你使用0作为填充物。 In which case there is indeed no difference anymore and the docs are accurate enough. 在这种情况下,确实没有任何区别,并且文档足够准确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM