Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ swprintf意外结果_C++_C - Fatal编程技术网

C++ swprintf意外结果

C++ swprintf意外结果,c++,c,C++,C,我正在尝试使用(swprintf的安全版本),在向字符串添加int时得到了意外的结果。 如果我在int-all之后附加相同的字符串。所以我的代码是: WCHAR buffer[256]; LPCWSTR pszFormat = L"%s %d"; WCHAR* pszTxt = dataPath.Buffer;//* status = RtlStringCbPrintfW(buffer, sizeof(buffer), pszFormat, pszTxt, 1);

我正在尝试使用(swprintf的安全版本),在向字符串添加int时得到了意外的结果。 如果我在int-all之后附加相同的字符串。所以我的代码是:

WCHAR buffer[256];

    LPCWSTR pszFormat = L"%s %d";

    WCHAR* pszTxt = dataPath.Buffer;//*

    status = RtlStringCbPrintfW(buffer, sizeof(buffer), pszFormat, pszTxt, 1);
  • 这里是dataPath。所以dataPath.Buffer是PWCH
    这里可以看到缓冲区的值是:buffer=wchar\u t[168]“\Device\HarddiskVolume2\foo\Data??C??”
观察缓冲区阵列时,我可以看到“?”是:

0xcc00 '?'
0xcccc '?'
然后在一些字节之后实际定位目标值1。 dataPath.Buffer的值:

   +0x048 DataPath         : _UNICODE_STRING "\Device\HarddiskVolume2\foo\Data"
      +0x000 Length           : 0x8c
      +0x002 MaximumLength    : 0x8c
      +0x008 Buffer           : 0xffffc000`01cd1d00  "\Device\HarddiskVolume2\foo\Data"

那么原因是什么,空终止字符?不应该使用swprintf自动正确处理?

您链接到的
UNICODE\u字符串
文档说明
缓冲区
不一定以null结尾,并且
%s
格式需要以null结尾的字符串

通过指定精度,可以限制使用
%s
打印的字符串的长度:

WCHAR* pszTxt = dataPath.Buffer;
int Len = dataPath.Length;

RtlStringCbPrintfW(buffer, sizeof(buffer), L"%.*s %d", Len, pszTxt, 1);

应该用于UNICODE_字符串结构。

为什么这个不起作用?RtlStringCbPrintfW(缓冲区,大小为(缓冲区),L“%.*s%s”,dataPath.Length,dataPath.buffer,L“\\”)。。L“\\”是空终止的。wprk没有什么?您得到的
缓冲区的值是多少?比如“\Device\HarddiskVolume2\foo\Data??C?”。我找到了正确答案UNICODE_字符串有一个特殊参数%wZ,它可以工作,所以solvedOkay,所以使用
%wZ
是您问题的实际答案。对不起,误会了。
%wZ