Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improved fix for empty string array registry types.

  • Loading branch information...
commit bd5b7fb376b0bd909105754a2e3271d4b8b2e7a5 1 parent 05ed5c6
David A. Solin authored
View
12 contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java
@@ -608,7 +608,7 @@ public static String registryGetExpandableStringValue(HKEY root, String key, Str
String s = data.getString(offset, true);
offset += s.length() * Native.WCHAR_SIZE;
offset += Native.WCHAR_SIZE;
- if (s.length() == 0 && offset == stringData.size()) {
+ if (s.length() == 0 && offset == data.size()) {
// skip the final NULL
} else {
result.add(s);
@@ -1008,7 +1008,8 @@ public static void registrySetStringArray(HKEY hKey, String name, String[] arr)
size += s.length() * Native.WCHAR_SIZE;
size += Native.WCHAR_SIZE;
}
-
+ size += Native.WCHAR_SIZE;
+
int offset = 0;
Memory data = new Memory(size);
for(String s : arr) {
@@ -1016,10 +1017,13 @@ public static void registrySetStringArray(HKEY hKey, String name, String[] arr)
offset += s.length() * Native.WCHAR_SIZE;
offset += Native.WCHAR_SIZE;
}
-
+ for (int i=0; i < Native.WCHAR_SIZE; i++) {
+ data.setByte(offset++, (byte)0);
+ }
+
int rc = Advapi32.INSTANCE.RegSetValueEx(hKey, name, 0, WinNT.REG_MULTI_SZ,
data.getByteArray(0, size), size);
-
+
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
View
16 contrib/platform/test/com/sun/jna/platform/win32/Advapi32UtilTest.java
@@ -256,9 +256,13 @@ public void testRegistrySetGetStringArray() {
for(int i = 0; i < dataRead.length; i++) {
assertEquals(dataWritten[i], dataRead[i]);
}
- Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");
+ dataWritten = new String[0];
+ Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "EmptyMultiString", dataWritten);
+ dataRead = Advapi32Util.registryGetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "EmptyMultiString");
+ assertEquals(0, dataRead.length);
+ Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");
}
-
+
public void testRegistrySetGetBinaryValue() {
byte[] data = { 0x00, 0x01, 0x02 };
Advapi32Util.registryCreateKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");
@@ -294,9 +298,11 @@ public void testRegistryGetValues() {
byte[] dataWritten = { 0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF };
Advapi32Util.registrySetBinaryValue(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "DeadBeef", dataWritten);
String[] stringsWritten = { "Hello", "World", "Hello World", uu };
- Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "StringArray", stringsWritten);
+ Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "StringArray", stringsWritten);
+ String[] emptyArray = new String[0];
+ Advapi32Util.registrySetStringArray(WinReg.HKEY_CURRENT_USER, "Software\\JNA", "EmptyStringArray", emptyArray);
TreeMap<String, Object> values = Advapi32Util.registryGetValues(WinReg.HKEY_CURRENT_USER, "Software\\JNA");
- assertEquals(5, values.keySet().size());
+ assertEquals(6, values.keySet().size());
assertEquals("FourtyTwo" + uu, values.get("42" + uu));
assertEquals(42, values.get("FourtyTwo" + uu));
assertEquals("%TEMP%", values.get("ExpandableString"));
@@ -310,6 +316,8 @@ public void testRegistryGetValues() {
for(int i = 0; i < stringsWritten.length; i++) {
assertEquals(stringsWritten[i], stringsRead[i]);
}
+ stringsRead = (String[]) values.get("EmptyStringArray");
+ assertEquals(0, stringsRead.length);
Advapi32Util.registryDeleteKey(WinReg.HKEY_CURRENT_USER, "Software", "JNA");
}
Please sign in to comment.
Something went wrong with that request. Please try again.