Windows programming sometimes generates an error by calling a function, and calling the GetLastError () function can get an error code. If the error code is 0, there is no error, and if the error code is not 0, there is an error.
The error code does not make it easy for programmers or users to intuitively understand what is going wrong. Visual Studio 2015 (or previous version) provides an external tool for error lookup, and you can enter the error code to see what went wrong.
If you want to see the error message in your program code, you can write the following function:
#include <iostream>#include<Windows.h>using namespacestd;voidWinerr ();intMainintargcChar*argv[]) {System ("haha"); Winerr (); System ("Pause"); return 0;}voidWinerr () {Char* Win_msg =NULL; DWORD Code=GetLastError (); if(Code = =0) {cout<<"Error"<<code<<": No error!\n"; return; } Else { //Get error messageFormatMessage (Format_message_allocate_buffer | Format_message_from_system, NULL, Code, MAKELANGID (Lang_neutral, Sublang_default), (LPTSTR) &win_msg,0, NULL); if(Win_msg! =NULL) {cout<<"Error"<< Code <<":"<< win_msg <<Endl; LocalFree (WIN_MSG); } } //to make the function call does not affect the result of subsequent function calls to the GetLastError () functionSetLastError (code);}
Test results:
Windows API Programming----converting error codes to error description information