mirror of
https://github.com/lifegpc/c-utils.git
synced 2026-06-06 05:08:45 +08:00
Update
This commit is contained in:
14
err.cpp
14
err.cpp
@@ -69,3 +69,17 @@ char* err_get_errno_message(int errnum) {
|
||||
if (!cpp2c::string2char(msg, tmp)) return nullptr;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
bool err::get_winerror(std::string& out, int32_t errnum) {
|
||||
LPWSTR errbuf;
|
||||
DWORD len;
|
||||
if (!(len = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&errbuf, 0, nullptr))) {
|
||||
if (errbuf) LocalFree(errbuf);
|
||||
return false;
|
||||
}
|
||||
bool re = wchar_util::wstr_to_str(out, std::wstring(errbuf, len), CP_UTF8);
|
||||
LocalFree(errbuf);
|
||||
return re;
|
||||
}
|
||||
#endif
|
||||
|
||||
12
err.h
12
err.h
@@ -1,6 +1,9 @@
|
||||
#ifndef _UTILS_ERR_H
|
||||
#define _UTILS_ERR_H
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#include <stdint.h>
|
||||
}
|
||||
#include <string>
|
||||
|
||||
namespace err {
|
||||
@@ -11,6 +14,15 @@ namespace err {
|
||||
* @returns true if successed
|
||||
*/
|
||||
bool get_errno_message(std::string &out, int errnum);
|
||||
#if _WIN32
|
||||
/**
|
||||
* @brief Get error message for HRESULT
|
||||
* @param out Output string
|
||||
* @param errnum HRESULT
|
||||
* @return true if successed
|
||||
*/
|
||||
bool get_winerror(std::string &out, int32_t errnum);
|
||||
#endif
|
||||
}
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user