From 62c74f2d69c7f5bb54d564a41dbd0530d9ee7f88 Mon Sep 17 00:00:00 2001 From: clay_shooter Date: Wed, 24 Jul 2013 01:00:21 +0000 Subject: [PATCH] #41 Fix for SafeArray(String) constructor #111 m_pDispatch is not 0 if not attached Should really be mixing two fixes in the same commit but that's the way it goes --- docs/ReleaseNotes.html | 13 +++++++--- jni/Dispatch.cpp | 2 +- src/com/jacob/com/SafeArray.java | 24 +++++++++++++------ src/com/jacob/com/Variant.java | 2 +- .../SafeArrayStringConstructorTest.java | 24 +++++++++++++++++++ 5 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 unittest/com/jacob/test/safearray/SafeArrayStringConstructorTest.java diff --git a/docs/ReleaseNotes.html b/docs/ReleaseNotes.html index 3386cd0..97d08ed 100644 --- a/docs/ReleaseNotes.html +++ b/docs/ReleaseNotes.html @@ -23,8 +23,12 @@ (M1)Fix memory pointer that was 32 bit. Causes defects in 64 bit systems above 2GB - 115 (new system numbering) - (M3)#115 Release problem if you've got two threads with the same name + 115 (new numbers) + (M3)Release problem if you've got two threads with the same name + + + 111 (new numbers) + (M3)m_pDispatch is not 0 if not attached   @@ -33,7 +37,10 @@ Patches - + + 41 (new numbers) + (M3)Fix for SafeArray(String) constructor +     diff --git a/jni/Dispatch.cpp b/jni/Dispatch.cpp index 96f5ff0..aad1b75 100644 --- a/jni/Dispatch.cpp +++ b/jni/Dispatch.cpp @@ -251,7 +251,7 @@ JNIEXPORT void JNICALL Java_com_jacob_com_Dispatch_release IDispatch *disp = (IDispatch *)num; if (disp) { disp->Release(); - env->SetIntField(_this, jf, 0ll); + env->SetLongField(_this, jf, 0ll); } } diff --git a/src/com/jacob/com/SafeArray.java b/src/com/jacob/com/SafeArray.java index a3c1ae8..2f5a93b 100644 --- a/src/com/jacob/com/SafeArray.java +++ b/src/com/jacob/com/SafeArray.java @@ -90,19 +90,25 @@ public class SafeArray extends JacobObject { } /** - * convert a string to a VT_UI1 array + * Convert a string to a VT_UI1 array. * * @param s * source string */ public SafeArray(String s) { - char[] ca = s.toCharArray(); - init(Variant.VariantByte, new int[] { 0 }, new int[] { ca.length }); - fromCharArray(ca); + // https://sourceforge.net/p/jacob-project/patches/41/ + /* + * char[] ca = s.toCharArray(); init(Variant.VariantByte, new int[] { 0 + * }, new int[] { ca.length }); fromCharArray(ca); + */ + byte[] ba = s.getBytes(); + init(Variant.VariantByte, new int[] { 0 }, new int[] { ba.length }); + fromByteArray(ba); + } /** - * convert a VT_UI1 array to string + * Convert a VT_UI1 array to string. Is this broken for unicode? * * @return variant byte as a string */ @@ -110,8 +116,12 @@ public class SafeArray extends JacobObject { if (getvt() != Variant.VariantByte) { return null; } - char ja[] = toCharArray(); - return new String(ja); + // https://sourceforge.net/p/jacob-project/patches/41/ + /* + * char ja[] = toCharArray(); return new String(ja); + */ + byte ba[] = toByteArray(); + return new String(ba); } @Override diff --git a/src/com/jacob/com/Variant.java b/src/com/jacob/com/Variant.java index e8c4668..6d6280a 100644 --- a/src/com/jacob/com/Variant.java +++ b/src/com/jacob/com/Variant.java @@ -109,7 +109,7 @@ public class Variant extends JacobObject { // VT_I1 = 16 - /** variant's type is byte VT_UI1 */ + /** variant's type is byte VT_UI1 This is an UNSIGNED byte */ public static final short VariantByte = 17; // VT_UI2 = 18 diff --git a/unittest/com/jacob/test/safearray/SafeArrayStringConstructorTest.java b/unittest/com/jacob/test/safearray/SafeArrayStringConstructorTest.java new file mode 100644 index 0000000..293bf6e --- /dev/null +++ b/unittest/com/jacob/test/safearray/SafeArrayStringConstructorTest.java @@ -0,0 +1,24 @@ +package com.jacob.test.safearray; + +import com.jacob.com.SafeArray; +import com.jacob.test.BaseTestCase; + +/** + * Test case provided #41 Fix for SafeArray(String) constructor + * + * In the current release of Jacob, SafeArray.java contains a constructor which + * takes a string as a single argument. The documentation claims that this + * method converts a string to a VT_UI1 array. Using this method as written + * always causes a ComFailException, because it attempts to create a SafeArray + * from Java chars, which are 16-bit unsigned integers (which would be VT_UI2). + */ +public class SafeArrayStringConstructorTest extends BaseTestCase { + public void testStringConstructor() { + // The line below will throw ComFailException using jacob 1.17-M2 + // without the patch. + SafeArray safeArrayFromString = new SafeArray("This is a string."); + String convertBack = safeArrayFromString.asString(); + assertEquals("This is a string.", convertBack); + } + +}