Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

convert windows message to java (convert FormatMessage message)

843829Sep 19 2008 — edited Sep 22 2008
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 20 2008
Added on Sep 19 2008
6 comments
349 views