C++ code invoked through JNI. Having error inside the C++ code, I get the error number (GetLastError) and then I call windows' FormatMessage function which will return me the description of the error. So I input error no and I receive message like "File not found". That works like a charm. Now I'm passing the error's description to the java by throwing jni exception. That works also. BUT... displaying the exception's message in java I can't see my national characters (I see some quotations instead).
My question is how can I convert the message returned by the windows native FormatMessage function, so I will be able to put it to the jni exception, so java will display my national characters?
I tryed some functions from the well known code which converts jstrings to windows char and vice versa but it still doesn't work.
The code:
DWORD size = 1024;
DWORD error = GetLastError();
DWORD retval;
CHAR msg[size];
LPSTR pMessage = "%1!*.*s! %4 %5!*s!";
retval = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM ,
pMessage,
error,
0, // langid
(LPSTR)msg,
size,
NULL
);
if (retval == 0) {
sprintf(msg, "FormatMessage error: %d. Original error: %d", retval, error);
}
printf("Fm: %s\n", msg);
// OK - this prints in a console the message
// with my national characters
env->ThrowNew(exception, msg);
// FAIL - i pass the message to
// the exception but java can't display
// that with all my national characters,
// I see some other chars instead of my
// national ones
ThrowNew method takes char* as an argument so I need to convert char* to char* where the second one will be correctly displayed by Java (with my national characters). I would like to see solution which would convert it inside the CPP (and not in Java, so the Java code would be cleaner) but I will be happy for any solution.