C++ FormatMessage中是否有明确的大小限制?
当传递给C++ FormatMessage中是否有明确的大小限制?,c++,c,winapi,C++,C,Winapi,当传递给FormatMessage的参数太长时,我遇到了一个问题 void testMessage(UINT id, ...) { va_list argList; va_start(argList, id); LPTSTR buff = NULL; const char* str = "The following value is invalid: %1"; DWORD success = FormatMessage(FORMAT_MESSAGE_
FormatMessage
的参数太长时,我遇到了一个问题
void testMessage(UINT id, ...)
{
va_list argList;
va_start(argList, id);
LPTSTR buff = NULL;
const char* str = "The following value is invalid: %1";
DWORD success = FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
str, 0, 0, (LPSTR) &buff, 0, &argList);
if(0 == success)
{
DWORD err = GetLastError();
//...
}
va_end(argList);
//...
}
int main(int argc, char** argv)
{
const char* arg = NULL;
// ...
// Initialize arg to some big string about 33,000 bytes long.
// ...
test(0, arg);
}
我得到的错误是error\u MORE\u DATA
(234)。当我将arg
的大小减少到32000字节左右时,问题没有出现,但不清楚限制是否与传入参数的大小或生成的字符串的总大小有关。onFormatMessage
中提到了lpBuffer
参数,“此缓冲区不能大于64K字节。”
我可以通过执行更多的错误检查并对传入该函数的参数的大小设置一些合理的限制来轻松解决这个问题,但对于我和其他人的将来参考,最好知道真正的限制是什么。您是在调用
FormatMessageA
还是FormateMessageW
?如果调用FormatMessageA
,32K ASCII消息将被编组为64K Unicode消息。今天的Windows内部使用Unicode,“A”系列函数只是“W”函数的包装。啊,我想我是在调用FormatMessageA,因为我没有定义Unicode宏。这听起来像是一种解释。谢谢