Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OaIdlUtil#toPrimitiveArray fails if dimension bounds are not 0-based #785

Closed
matthiasblaesing opened this issue Apr 4, 2017 · 1 comment

Comments

@matthiasblaesing
Copy link
Member

OaIdlUtil#toPrimitive fails to correctly calculate the raw index positions if the dimension bounds are not zero based.

This manifests when querying ranges from excel, which return 1-based arrays.

Provide complete information about the problem

  1. Version of JNA and related jars: Reproduced with JNA 4.4.0
  2. Version and vendor of the java virtual machine: Oracle 1.8.0_121; Java HotSpot(TM) 64-Bit Server VM 25.121-b13
  3. Operating system: Windows 10
  4. System architecture (CPU type, bitness of the JVM): AMD64, 64bit VM
  5. Complete description of the problem: See above
  6. Steps to reproduce: Both of these converts should work, the second fails with an ArrayIndexOutOfBoundsException
        SAFEARRAY sa = SAFEARRAY.createSafeArray(new VARTYPE(VT_I4), 2, 2);
        Object[][] data = (Object[][]) OaIdlUtil.toPrimitiveArray(sa, false);
        for(Object[] row: data) {
            System.out.println(Arrays.toString(row));
        }
        sa.rgsabound[0].lLbound = new LONG(1);
        sa.rgsabound[1].lLbound = new LONG(1);
        sa.write();
        System.out.println(Arrays.toString((Object[]) OaIdlUtil.toPrimitiveArray(sa, false)));
@matthiasblaesing
Copy link
Member Author

A workaround (see adjustment of lLbound):

public class Main {

    public static void main(String[] args) {
        Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
        ObjectFactory of = new ObjectFactory();
        Application excel = null;
        try {
            excel = of.createObject(Application.class);
            Workbook wb = excel.getWorkbooks().Open("e:/temp/test.xlsx", VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING);
            _Worksheet ws = wb.getActiveSheet().queryInterface(_Worksheet.class);
            Range r = ws.getRange("A1:C1", VARIANT_MISSING);
            try(OaIdl.SAFEARRAY sa = (OaIdl.SAFEARRAY) r.getValue(VARIANT_MISSING)) {
                sa.rgsabound[0].lLbound = new LONG(0);
                sa.rgsabound[1].lLbound = new LONG(0);
                Object[][] obj = (Object[][]) OaIdlUtil.toPrimitiveArray(sa, false);
                for(Object[] o: obj) {
                    System.out.println(Arrays.toString(o));
                }
            }
        } finally {
            if (excel != null) {
                excel.Quit();
            }
            of.disposeAll();
            Ole32.INSTANCE.CoUninitialize();
        }

    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant