Skip to content

What is the CsWin32 way to handle API failures and access error information? #1092

Answered by AArnott
netcorefan1 asked this question in Q&A
Discussion options

You must be logged in to vote

CsWin32 generates APIs that follow the same Win32 error reporting standard that you might have in manually written P/Invokes. You should refer to the documentation of the individual win32 API to learn how to get error information. Some APIs call SetLastError while others do not. You should only call GetLastWin32Error directly after a call to a win32 API that is documented as calling SetLastError and should only do so when it says you should (e.g. when you get a null result).
The reason for this is if you call an API that does not call SetLastError (either because it never does, or only does in error cases), then by calling GetLastWin32Error you'll be getting the code from some prior call …

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@netcorefan1
Comment options

Answer selected by netcorefan1
Comment options

You must be logged in to vote
1 reply
@AArnott
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants