//将错误码转字符串,若未传入参数则将当前错误码转字符串返回
std::string last_error(int errCode = 0);//函数声明
std::string last_error(int errcode){
#ifndef WIN32
if (errcode == 0){
return strerror(errno);
}
return strerror(errcode);
#endif // !WIN32
if (errcode == 0){
errcode = GetLastError();
}
std::string err;
LPSTR lp_buffer = NULL;
if (0 == FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lp_buffer,
0, NULL)){
err = "{未定义错误描述(%d)}" + errcode;
} else{//成功
err = lp_buffer;
}
return err;
}
C++错误码转string字符串
最新推荐文章于 2024-08-15 05:02:54 发布