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