#41 Fix for SafeArray(String) constructor

1.18 Accepted
closed-accepted
nobody
5
2015-06-22
2012-09-07
Joe Polak
No

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).

The following

Discussion

  • Joe Polak

    Joe Polak - 2012-09-07

    Patch to SafeArray.java

     
    Attachments
  • Joe Polak

    Joe Polak - 2012-09-07

    Oops, I hit "attach" a moment too soon. I'm just going to finish the details here.

    The attached patch fixes the bug by getting a byte array of the string and then creating a SafeArray from that.

    I did not null terminate the array in Java, because that would be unnecessary for what I wanted to use this constructor for. So be sure to null-terminate on the COM server side (like, for instance, if you wanted to populate a LPCSTR with data from this array). Or you could add an additional constructor with a boolean to do that. I dunno.

     
  • clay_shooter

    clay_shooter - 2012-10-20

    Do you have a test program that demonstrates this problem?
    Should the null termination be fixed before integrating this into Jacob?

     
  • Joe Polak

    Joe Polak - 2012-10-22

    Java program that demonstrates bug.

     
    Attachments
  • Joe Polak

    Joe Polak - 2012-10-22

    SafeArrayTest.java demonstrates the bug. Without the patch, it throws a ComFailExeception, saying "safearray cannot be assigned from char"

    I looked around more and remembered that SafeArrays aren't supposed to be null terminated; their boundaries are given in their rgsabound array. So you can ignore that bit about null terminating.

     
  • clay_shooter

    clay_shooter - 2013-07-24

    It looks like asString is broken also. I'm looking at this fix for the inverse of the method you fixed
    /
    * Convert a VT_UI1 array to string.
    *
    * @return variant byte as a string

    /
    public String asString() {
    if (getvt() != Variant.VariantByte) {
    return null;
    }
    // https://sourceforge.net/p/jacob-project/patches/41/
    /

    * char ja[] = toCharArray(); return new String(ja);
    */
    byte ba[] = toByteArray();
    return new String(ba);
    }

     
  • clay_shooter

    clay_shooter - 2013-07-24
    • status: open --> pending
    • Group: --> 1.17 Accepted
     
  • clay_shooter

    clay_shooter - 2013-07-24

    How the heck did we get this far with this defect?

     
  • clay_shooter

    clay_shooter - 2013-09-07
    • status: pending --> closed-accepted
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks