gpt4 book ai didi

c++ - 如何摆脱 FormatMessageA 错误消息中附加的 ".\r\n"字符?

转载 作者:行者123 更新时间:2023-11-28 02:25:45 26 4
gpt4 key购买 nike

我正在用 std::errc 替换 boost::system::error_code。 error_code 支持通过error_code::message() 以字符串格式显示错误信息。但我认为在 STL 中,我们没有预先定义的方法来做到这一点。我说得对吗?

如果我错了,请告诉我在不编写自己的函数的情况下执行此操作的默认方法。如果我是对的,请帮助我格式化以下程序中的错误消息(取自 stackoverflow)

//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();

LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);

std::string message(messageBuffer, size);

//Free the buffer.
LocalFree(messageBuffer);

return message;
}

int main()
{
SetLastError((DWORD)std::errc::operation_canceled);

string str(GetLastErrorAsString());
cout << "str = " << str;
return 0;
}

输出:

str = 此信号量的先前所有权已结束。\r\n

boost::system::error_code::message() 的输出没有这些额外的“.\r\n”。无论如何,通过调整提供给 FormatMessageA() 的参数,我们是否可以摆脱这些额外的字母?或者我必须削减自己(只需删除尾随的 3 个字符)?如果我只是盲目地删除它们,是否有可能出现没有这些字符的错误消息?

最佳答案

添加以下标志有效;

FORMAT_MESSAGE_MAX_WIDTH_MASK (0x000000FF) : The function ignores regular line breaks in the message definition text. The function stores hard-coded line breaks in the message definition text into the output buffer. The function generates no new line breaks.

std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();

LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);

std::string message(messageBuffer, size);

//Free the buffer.
LocalFree(messageBuffer);

return message;
}

int main()
{
SetLastError((DWORD)std::errc::operation_canceled);

string str(GetLastErrorAsString());
cout << "str = " << str;
return 0;
}

关于c++ - 如何摆脱 FormatMessageA 错误消息中附加的 ".\r\n"字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30726261/

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