On Windows, JNU_ThrowByNameWithLastError calls getLastErrorString, which in turn calls FormatMessageA, which first retrieves a localized error message from the OS, then converts it from Unicode to the current (Windows) code page. Later the message is converted back to Unicode using JNU_NewStringPlatform.
If the current code page cannot represent all characters in the Unicode message, some information will be lost. If JNU_NewStringPlatform uses a different encoding than Windows, the message may end up being completely different.
We should use FormatMessageW to retrieve the message. This method returns the message in Unicode, which can then be used in JNI NewString without any conversions.
There are other methods that also use getLastErrorString to retrieve error text; these include:
JNU_ThrowByNameWithLastError
JNU_ThrowIOExceptionWithLastError
JNU_ThrowByNameWithMessageAndLastError
NET_ThrowByNameWithLastError
and a few others.
If the current code page cannot represent all characters in the Unicode message, some information will be lost. If JNU_NewStringPlatform uses a different encoding than Windows, the message may end up being completely different.
We should use FormatMessageW to retrieve the message. This method returns the message in Unicode, which can then be used in JNI NewString without any conversions.
There are other methods that also use getLastErrorString to retrieve error text; these include:
JNU_ThrowByNameWithLastError
JNU_ThrowIOExceptionWithLastError
JNU_ThrowByNameWithMessageAndLastError
NET_ThrowByNameWithLastError
and a few others.