如果我调用通过GetLastError报告错误的Win32函数,例如RegisterClassEx,如何为该错误抛出std :: system_error?
bas*_*h.d 14
检查GetLastError()喜欢的值
DWORD dwErrVal = GetLastError();
std::error_code ec (dwErrVal, std::system_category());
throw std::system_error(ec, "Exception occurred");
Run Code Online (Sandbox Code Playgroud)
见这里的error_code,并在这里进行std::system_error.
小智 9
请注意,根据您的供应商,比较std::error_code类别中的a std::system_category()通常不会与std::errc枚举常量一起使用.例如
std::error_code(ERROR_FILE_NOT_FOUND, std::system_category()) != std::errc::no_such_file_or_directory)
某些供应商需要使用std::generic_category()它来工作.
在MinGW中std::error_code(ERROR_FILE_NOT_FOUND, std::system_category()).message()不会给你正确的信息.
对于一个真正的便携和强大的解决方案,我会建议既继承std::system_error和std::system_category对windows_error与windows_category和自己实现正确的功能.据报道,YMMV,VS2017正如人们所期望的那样工作.