Windows error messages

459 views Asked by At

I am developing a program using the HIDAPI for USB communications and it appears that the API function hid_error() returns the last error message direct from windows, not a message generated by the API itself. Where can I find a list of all possible error messages so I can react more precisely to an error?

2

There are 2 answers

18
László Papp On BEST ANSWER

I believe you are looking for the following two functions below in addition to the regular error codes on Windows, the documentation of which seems to be the first result on Google.

The first function will allow you to check the error code against the system error codes, and based on your desire (although you have not shared why you need any customization), you can either have a custom error message displayed, or you can just use the second function for keeping that when that is feasible. Surely, you could also keep the hid_error output since that is equivalent.

1) Getting the error code itself.

GetLastError function

Retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.

2) Getting the string out of the error code.

FormatMessage function

Formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.

In a Qt application for instance, this would be even a more natural way of handling the issue for your customization:

QString QtWin::errorStringFromHresult(HRESULT hresult)

Returns the code name of the hresult error id specified (usually the name of the WinAPI macro) or an empty string if the message is unknown.

2
Preston On

You might have a look at the System Error Codes.