Structured exception codes are defined through NTSTATUS numbers. Although someone from MS suggests here (the article has been moved to here) using FormatMessage() to convert NTSTATUS numbers to strings, I would not do this. Flag FORMAT_MESSAGE_FROM_SYSTEM
is used to convert result of GetLastError() into a string, so it makes no sense here. Using flag FORMAT_MESSAGE_FROM_HMODULE
along with ntdll.dll
will lead to incorrect results for some codes. E.g., for EXCEPTION_ACCESS_VIOLATION
you will get The instruction at 0x
, which is not very informative :) .
When you look at the strings that are stored in ntdll.dll
it becomes obvious that many of them are supposed to be used with the printf() function, not with the FormatMessage(). For example, the string for EXCEPTION_ACCESS_VIOLATION
is:
The instruction at 0x%08lx referenced memory at 0x%08lx. The memory could not be %s.
%0
is treated by FormatMessage() as the escape sequence meaning message terminator, not an insert. Inserts are %1 to %99. That's why flag FORMAT_MESSAGE_IGNORE_INSERTS
does not make any difference.
You might want to load the string from ntdll.dll
and pass it to vprintf() but you will need to prepare arguments exactly as the string specifies (e.g. for EXCEPTION_ACCESS_VIOLATION
it's unsigned long
, unsigned long
, char*
). And this approach has major drawback: any change in the number, order or size of arguments in ntdll.dll
may break your code.
So it's safer and easier to hard code the strings into your own code. I find it dangerous to use strings prepared by someone else without coordination with me :) and moreover for other function. This is just one more possibility for malfunction.
FormatMessage
should do the trick. – Avisavitaminosis