gpt4 book ai didi

c++ - WinApi FormatMessage 导致段错误

转载 作者:行者123 更新时间:2023-11-28 00:31:14 29 4
gpt4 key购买 nike

我用的是CodeBlocks和Mingw:g++版本是4.7.1

例子来自 MSDN (请参阅最后一个示例,就在“要求”部分之前):

// Formats a message string using the specified message and variable
// list of arguments.
LPWSTR GetFormattedMessage(LPWSTR pMessage, ...)
{
LPWSTR pBuffer = NULL;

va_list args = NULL;
va_start(args, pMessage);

FormatMessage(FORMAT_MESSAGE_FROM_STRING |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
pMessage,
0,
0,
(LPWSTR)&pBuffer,
0,
&args);

va_end(args);

return pBuffer;
}

它在调用 FormatMessage 时出现段错误。您知道为什么会这样吗?我该如何解决?

我是这样调用它的:

int x = 3, y = 5;
GetFormattedMessage(_T("%1 : %2"), x, y);

我使用 FormatMessage 是因为我不能在 mingw 上使用 _stprintf 函数,_stprintf 是 swprintf 的定义,而 swprintf 本身没有被定义为修复其中的一些错误 %)

最佳答案

FormatMessage 要求您在消息字符串中传递类型信息。如果你不这样做,它会假设你的参数是 C 风格的字符串。 MSDN 说:

The default is to treat each value as a pointer to a null-terminated string.

当你传递整数而不是字符串时,你的调用应该是这样的:

GetFormattedMessage(_T("%1!d! : %2!d!"), x, y);

关于c++ - WinApi FormatMessage 导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22854014/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com