Windows API one-day training (81) formatmessage Function

Source: Internet
Author: User

Debugging is a common task during software development. Program Many problems may occur, but logical problems may also be unfamiliar with the API, or API calls may fail under certain conditions. So what are the causes of these errors? Generally, only the error code is obtained through the getlasterror function. Of course, you can use this error code to find out the cause of the error, but sometimes the customer does not have msdn, so you need to display the error message of calling the API function, or write it to log. In this case, you need to call the formatmessage function to display the detailed cause of the error code.

The formatmessage function is declared as follows:

Winbaseapi
DWORD
Winapi
Formatmessagea (
DWORD dwflags,
Lpcvoid lpsource,
DWORD dwmessageid,
DWORD dwageid,
Lpstr lpbuffer,
DWORD nsize,
Va_list * arguments
);
Winbaseapi
DWORD
Winapi
Formatmessagew (
DWORD dwflags,
Lpcvoid lpsource,
DWORD dwmessageid,
DWORD dwageid,
Lpwstr lpbuffer,
DWORD nsize,
Va_list * arguments
);
# Ifdef Unicode
# Define formatmessage formatmessagew
# Else
# Define formatmessage formatmessagea
# Endif //! Unicode

An example of calling a function is as follows:
#001 // system error message prompt.
#002 // Cai junsheng 2007/11/28 QQ: 9073204 Shenzhen
#003 void testerrorinfo (void)
#004 {
#005 // error.
#006 if (! Createdirectory (_ T ("C: \"), 0 ))
#007 {
#008 tchar szbuf [128];
#009 lpvoid lpmsgbuf;
#010 dword dw = getlasterror ();
#011
#012 formatmessage (
#013 format_message_allocate_buffer |
#014 format_message_from_system,
#015 null,
#016 DW,
#017 makelangid (lang_neutral, sublang_default ),
#018 (lptstr) & lpmsgbuf,
#019 0, null );
#020
#021 wsprintf (szbuf,
#022 _ T ("% s error message (error code = % d): % s "),
#023 _ T ("createdirectory"), DW, lpmsgbuf );
#024
#025 localfree (lpmsgbuf );
#026
#027 // output the prompt.
#028 outputdebugstring (szbuf );
#029}
#030
#031}

After the call, the following message is displayed:
Createdirectory error message (error code = 5): Access denied.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/caimouse/archive/2007/11/30/1909968.aspx

Related Article

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.