Is it possible to check if error code has been retrived using GetLastError
GetLastError() just gets thread's last-error code value and that's all. So, I believe there is no way to know was it called once or a few times. Moreover, I don't really think you need to.
The best solution here is to create WinAPIException class derived from some base class (for example, std::exception ). In constructor WinAPIException should call GetLastError() before any other WinAPI call. It guarantees that the error value isn't change.
if( !CopyFile("c:\foobar.txt", "d:\foobar.txt",FALSE) )
throw WinAPIException("Houston, we have a WINAPI problem");
If you check result after non-WinAPI function call - just use other exception class (derived from the same base class):
if( std::cin.get() < 0 )
throw std::runtime_error("Houston, we have runtime problem");
Thus, you can use single try...catch statement to catch both exceptions:
try {
...
} catch( std::exception &e ) {
...
}
Win32 API: GetLastError() with FormatMessage() prints a junk
Your terminal is set to the wrong code page, the sequence ┬√яюыэхэю in code page 866 is Выполнено in code page 1251 (i.e. the raw bytes c2 fb ef ee eb ed e5 ed ee
), change your terminal code page by calling
chcp 1251
before you run your app and try again.
As to why your terminal code page does not match your system code page is anyone's guess, try resetting the cmd.exe settings for the local user by deleting the HKCU\Console registry key (make a registry backup first just in case).
Combining the value of GetLastError and a custom error message
According to Microsoft's documentation, the system error codes max out at 15999 (0x3E7F). This means you have the entire upper word to play with. You'll need to shorten your error codes to fit into 4 hex digits, then you can use some Windows macros to combine and split them:
return MAKELPARAM(GetLastError(), ERR_DB_NOT_OPEN);
int lasterror = LOWORD(result);
int code = HIWORD(result);
List of GetLastError() error codes for every winapi function
There is no such list. In fact, there cannot be one, because there are API calls, that aren't even in control of the entire set of error codes they can return (consider, for example, EnumWindows, where user-provided code sets the error code).
Some API calls provide a partial list of error codes they can return. On those cases it is part of the documented contract, and your code can be written to account for those error codes. Keep in mind, that those lists are usually never complete, so your code needs to be prepared to deal with other error codes as well.
In short, error handling needs to be implemented on a case-by-case basis. There are common patterns, but there is no single catch-all implementation.
Related Topics
Should I Compile With /Md or /Mt
Difference Between Atan and Atan2 in C++
Where to Put Default Parameter Value in C++
Efficient String Concatenation in C++
Creating All Possible K Combinations of N Items in C++
Rand() % 14 Only Generates the Values 6 or 13
Non-Blocking Worker - Interrupt File Copy
Splitting a C++ Std::String Using Tokens, E.G. ";"
How to Assign an Alias to a Function Name in C++
Why Is Default Constructor Called in Virtual Inheritance
Create Wcf Service For Unmanaged C++ Clients
Is Calling Destructor Manually Always a Sign of Bad Design
What Does a Colon Following a C++ Constructor Name Do
Why Are String Literals L-Value While All Other Literals Are R-Value