Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 220297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:55:32+00:00 2026-05-11T18:55:32+00:00

I have a Java application that uses a C++ DLL via JNI. A few

  • 0

I have a Java application that uses a C++ DLL via JNI. A few of the DLL’s methods take string arguments, and some of them return objects that contain strings as well.

Currently the DLL does not support Unicode, so the string handling is rather easy:

  • Java calls String.getBytes() and passes the resulting array to the DLL, which simply treats the data as a char*.
  • DLL uses NewStringUTF() to create a jstring from a const char*.

I’m now in the process of modifying the DLL to support Unicode, switching to using the TCHAR type (which when UNICODE is defined uses windows’ WCHAR datatype). Modifying the DLL is going well, but I’m not sure how to modify the JNI portion of the code.

The only thing I can think of right now is this:

  • Java calls String.getBytes(String charsetName) and passes the resulting array to the DLL, which treats the data as a wchar_t*.
  • DLL no longer creates Strings, but instead passes jbyteArrays with the raw string data. Java uses the String(byte[] bytes, String charsetName) constructor to actually create the String.

The only problem with this method is that I’m not sure what charset name to use. WCHARs are 2-bytes long, so I’m pretty sure it’s UTF-16, but there are 3 posibilities on the java side. UTF-16, UTF-16BE, and UTF-16LE. I haven’t found any documentation that tells me what the byte order is, but I can probably figure it out from some quick testing.

Is there a better way? If possible I’d like to continue constructing the jstring objects within the DLL, as that way I won’t have to modify any of the usages of those methods. However, the NewString JNI method doesn’t take a charset identifier.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:55:32+00:00Added an answer on May 11, 2026 at 6:55 pm

    This answer suggests that the byte-ordering of WCHARS is not guaranteed…

    Since you are on Windows you could try WideCharToMultiByte to convert the WCHARs to UTF-8 and then use your existing JNI code.

    You will need to be careful using WideCharToMultiByte due to the possibility of buffer overruns in the lpMultiByteStr parameter. To get round this you should call the function twice, first with lpMultiByteStr set to NULL and cbMultiByte set to zero – this will return the length of the required lpMultiByteStr buffer without attempting to write to it. Once you have the length you can allocate a buffer of the required size and call the function again.

    Example code:

    int utf8_length;
    
    wchar_t* utf16 = ...;
    
    utf8_length = WideCharToMultiByte(
      CP_UTF8,           // Convert to UTF-8
      0,                 // No special character conversions required 
                         // (UTF-16 and UTF-8 support the same characters)
      utf16,             // UTF-16 string to convert
      -1,                // utf16 is NULL terminated (if not, use length)
      NULL,              // Determining correct output buffer size
      0,                 // Determining correct output buffer size
      NULL,              // Must be NULL for CP_UTF8
      NULL);             // Must be NULL for CP_UTF8
    
    if (utf8_length == 0) {
      // Error - call GetLastError for details
    }
    
    char* utf8 = ...; // Allocate space for UTF-8 string
    
    utf8_length = WideCharToMultiByte(
      CP_UTF8,           // Convert to UTF-8
      0,                 // No special character conversions required 
                         // (UTF-16 and UTF-8 support the same characters)
      utf16,             // UTF-16 string to convert
      -1,                // utf16 is NULL terminated (if not, use length)
      utf8,              // UTF-8 output buffer
      utf8_length,       // UTF-8 output buffer size
      NULL,              // Must be NULL for CP_UTF8
      NULL);             // Must be NULL for CP_UTF8
    
    if (utf8_length == 0) {
      // Error - call GetLastError for details
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I just played a bit with your code, is this… May 11, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer Forward declare one before the other. You can use references… May 11, 2026 at 8:53 pm
  • Editorial Team
    Editorial Team added an answer Here's a query which will produce the above results: SELECT… May 11, 2026 at 8:53 pm

Related Questions

I support a legacy Java application that uses flat files (plain text) for persistence.
I have an application that's a mix of Java and C++ on Solaris. The
I have been tasked with finding an open source DOM XML parser. The parser
I understand that the Windows API uses classes, relying to the WNDCLASS / WNDCLASSEX
I have a java application that uses some third party API. The third party

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.