侧边栏壁纸
博主头像
G

  • 累计撰写 85 篇文章
  • 累计创建 48 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录

windows C++ 将系统错误码转换为字符串

G
G
2024-04-18 / 0 评论 / 0 点赞 / 125 阅读 / 161 字 / 正在检测是否收录...
std::string GetLastErrorAsString() {
	DWORD errorCode = GetLastError();
	if (errorCode == 0) {
		return "No error.";
	}
	LPSTR messageBuffer = nullptr;
	size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		nullptr, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, nullptr);
	std::string message(messageBuffer, size);
	LocalFree(messageBuffer);
	return message;
}


std::string GetHRESULTErrorAsString(HRESULT hr) {
	if (SUCCEEDED(hr)) {
		return "No error.";
	}
	LPSTR messageBuffer = nullptr;
	size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		nullptr, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, nullptr);
	std::string message(messageBuffer, size);
	LocalFree(messageBuffer);
	return message;
}


std::string GetLSTATUSAsString(LSTATUS errorCode) {
	if (errorCode == ERROR_SUCCESS) {
		return "No error.";
	}
	LPSTR messageBuffer = nullptr;
	size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		nullptr, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, nullptr);
	std::string message(messageBuffer, size);
	LocalFree(messageBuffer);
	return message;
}
0

评论区