被LLX%或%LLU不应该用wsprintf工作?

问题描述 投票:1回答:1

我使用Visual Studio 2017年C ++。当我用printf与规范,如LLX%或%LLU一切正常。如果我使用相同的格式规范,LLU%或%LLX,与wsprintf,我收到垃圾的缓冲区,而不是我曾与printf的得到的结果。

我的问题是:有没有办法让wsprintf让您在使用LLX%和/或%LLU时得到的结果?

以下是一个演示printf和wsprintf的不同的行为非常简单的控制台程序。

#include "stdafx.h"
#include <Windows.h>
#include <inttypes.h>


int main()
{
  DWORD64 OffsetHWM = 0x7123456789012345;

  WCHAR   BufferBytes[256] = { 0 };  // initialized - no junk in there

  // the wprintf below works as expected

  wprintf(L"from wprintf : %8llX\n", OffsetHWM);

  // this call to wsprintf isn't filling the buffer with the expected value

  wsprintf(BufferBytes, L"%8llX\n", OffsetHWM);
  wprintf(L"from wsprintf: %s\n", BufferBytes);    // prints junk

  wprintf(L"\n");                                  // just for neatness

  wsprintf(BufferBytes, L"%8" PRIx64 "\n", OffsetHWM);
  wprintf(L"from wsprintf: %s\n", BufferBytes);

  // this truncates (as expected) the value of OffsetHWM - not useful

  wsprintf(BufferBytes, L"%8lx\n", OffsetHWM);
  wprintf(L"from wsprintf: %s\n", BufferBytes);

  return 0;
}
visual-c++ printf
1个回答
-1
投票

wprintf()不应该被使用了,它是调用Windows的特定功能,无论是wsprintfA()或wsprintfW()做实际的工作,这两者有以下注意事项对他们的Windows开发人员中心文档站点(https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-wsprintfa):

注意不要使用。请考虑使用以下功能之一,而不是:StringCbPrintf,StringCbPrintfEx,StringCchPrintf,或StringCchPrintfEx。请参阅安全注意事项。

© www.soinside.com 2019 - 2024. All rights reserved.