1

In my Application i am reading registry to get all TimeZone names. It is working fine with native english OS machine.

But for chinese native os it is showing "????????".

I am using WinRegistry.java, the common file available to read registry in java.

Below is the method that reads bytes from registry, but that bytes contains junk characters only.

private static String readString(Preferences root, int hkey, String key, String value)
    throws IllegalArgumentException, IllegalAccessException,
    InvocationTargetException 
  {
    int[] handles = (int[]) regOpenKey.invoke(root, new Object[] {
        new Integer(hkey), toCstr(key), new Integer(KEY_READ) });
    if (handles[1] != REG_SUCCESS) {
      return null; 
    }
    byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[] {
        new Integer(handles[0]), toCstr(value) });
    regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
    return (valb != null ? new String(valb).trim() : null);
  }

In valb[] byte array i am getting the junk characters, so whatever encoding i use to convert that byte array to string, i am getting junk characters only. Can any one suggest me, what changes in this method will make it work fine??

JayRaj
  • 21
  • 1
  • 7

1 Answers1

1

I assume you mean the hack in this answer that uses a private, undocumented, implementation-specific API.

This code uses the default encoding to turn the returned bytes into chars:

return (valb != null ? new String(valb).trim() : null);

On Windows, the default encoding will likely be a legacy encoding - an "ANSI" code page.

You will need to figure out the encoding of the data and provide it explicitly in the String(byte[],Charset) constructor or switch to a documented API - e.g. using RegQueryValueExW with JNA.

As Octopus pointed out in the comments, it is also easy to turn characters into junk by using System.out as it uses lossy legacy encodings too.

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • But i have checked the bytes that i am getting in valb[]. And that bytes it self contains "??????". So whatever encoding we use it will not help.. – JayRaj Apr 04 '14 at 09:11
  • Ah, OK. When Java's Preferences API stores Unicode values in the Windows registry it does its own Unicode escaping - it was never intended to be a general registry reader type. WinRegistry.java's hack abuses the internals of this API. This might explain why the array data is corrupt under some locales. I would look at using native code. – McDowell Apr 04 '14 at 12:28