Convert Windows Error code to error message

Source: Internet
Author: User
Document directory
  • Implementation

After calling an API function, if the function fails, you can call another API function "getlasterror" to return an error code. The following class can be defined based on the error code of windows, find the error message in text:

00001: #ifndef _YHB_SYSERROR_INCLUDED_
00002: #define _YHB_SYSERROR_INCLUDED_
00003:
00004: #include <stdexcept>
00005: #include <string>
00006:
00007: namespace yhb {
00008:
00009: class SysError
00010: : public std::runtime_error
00011: {
00012: public:
00013: SysError(int err=0)
00014: : runtime_error(makeMsgFromErrorNum(err))
00015: {}
00016: SysError(const char *customMsg, int err = 0)
00017: : runtime_error(makeMsgFromErrorNum(customMsg, err))
00018: {}
00019: static std::string makeMsgFromErrorNum(unsigned err = 0);
00020: static std::string makeMsgFromErrorNum(const char *customMsg, unsigned err = 0);
00021: };
00022:
00023: } // end of namespace
00024:
00025: #endif

Syserror is derived from STD: runtime_error. Therefore, it can be thrown and captured as a STD: exception.

If you do not want to use exceptions, you can use two static methods to return error messages.

Implementation
00001: # include "stdafx. H"
00002: # include "./syserror. H"
00003: # include <sstream>
00004: # include <lmerr. h>
00005:
00006: # define win32_lean_and_mean
00007: # include <windows. h>
00008:
00009: Using namespace STD;
00010:
00011: namespace yhb {
00012:
00013:/****************************************** ******************************
00014:Manage the module handles loaded with loadlibrary ()
00015:**************************************** ********************************/
00016: class module {
00017: public:
00018: explicit module (hmodule handle = 0)
00019: handle _ (handle)
00020 :{}
00021 :~ Module (){
00022: Free ();
00023 :}
00024: module & operator = (hmodule handle ){
00025: Free ();
00026: handle _ = handle;
00027: return * this;
00028 :}
00029: bool isvalid () const {return 0! = Handle _;}
00030: Operator hmodule () const {return handle _;}
00031: Private:
00032: module (const module &);
00033: module & operator = (const module &);
00034: void free (){
00035: If (0! = Handle _){
00036: freelibrary (handle _);
00037: handle _ = 0;
00038 :}
00039 :}
00040: hmodule handle _;
00041 :};
00042:
00043:/****************************************** ******************************
00044:Syserror
00045:**************************************** ********************************/
00046: String syserror: makemsgfromerrornum (unsigned err/*= 0*/){
00047: If (0 = ERR ){
00048: Err = getlasterror ();
00049: If (noerror = ERR)
00050: Return "no error! ";
00051 :}
00052: module;
00053: DWORD flag = format_message_ignore_inserts | format_message_from_system;
00054: If (ERR> = nerr_base & err <= max_nerr ){
00055: module = loadlibraryexa ("netmsg. dll", 0, load_library_as_datafile );
00056. If (module. isvalid ())
00057: Flag | = format_message_from_hmodule;
00058 :}
00059: Char Buf [1024];
00060: If (formatmessagea (flag, static_cast 00061: Err, makelangid (lang_neutral, sublang_default ),
00062: Buf, sizeof (BUF), 0 ))
00063 :{
00064: Return Buf;
00065:} else {
00066: ostringstream OS;
00067: OS <"Unknown error:" <err <"(0x" 00068: Return OS. STR ();
00069 :}
00070 :}
00071:
00072: String syserror: makemsgfromerrornum (const char * custommsg, unsigned err/*= 0*/){
00073: String result (custommsg );
00074: Result + = '';
00075: Result + = makemsgfromerrornum (ERR );
00076: return result;
00077 :}
00078:
00079 :}//End of namespace

A common method in C ++ is to encapsulate resources into a class and release the resource during the analysis. For example, the module above calls freelibrary during the analysis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.