Windows API: system error message string Retrieval

Source: Internet
Author: User
Tags format message

Are you constantly switching to the error lookup program to view the system error code returned by the API? That's too inefficient!

Let the system generate an error message string with the current default encoding:

//////////////////////////////////////// /////////////////////////////////////
//
// In
// DWORD dwerror error code. The default value (0 xffffffff) indicates that the last error code of this thread is obtained directly.
//
// Out
// The error message string generated by the cstring System
//
Static cstring geterrormsg (DWORD dwerror = 0 xffffffff)
{
Dwerror = (dwerror = 0 xffffffff )? Getlasterror (): dwerror;

// Format message
Lpvoid lpmsgbuf;
Formatmessage (
Format_message_allocate_buffer |
Format_message_from_system |
Format_message_ignore_inserts,
Null, // module to get message from (null = System)
Dwerror,
0, // default language: makelangid (lang_neutral, sublang_default)
(Lptstr) & lpmsgbuf,
0,
Null
);
// Process any inserts in lpmsgbuf.
//...
// Get the string.
Cstring strmsg (lpctstr) lpmsgbuf );
// Free the buffer.
Localfree (lpmsgbuf );

Strmsg. trimright (_ T ("/R/N "));
Return strmsg;
}

//////////////////////////////////////// /////////////////////////////////////
//
// In
// DWORD dwerror error code. The default value (0 xffffffff) indicates that the last error code of this thread is obtained directly.
//
// Out
// The error message string generated by the cstring System and the decimal value of the error code
//
Static cstring geterrormsgandcode (DWORD dwerror = 0 xffffffff)
{
Dwerror = (dwerror = 0 xffffffff )? Getlasterror (): dwerror;

Cstring strmsg;
Strmsg. Format (_ T ("% s (% d)"), geterrormsg (dwerror), dwerror );
Return strmsg;
}

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.