diff --git a/runtime/src/main/java/com4j/Variant.java b/runtime/src/main/java/com4j/Variant.java index 343cede..f88eba0 100644 --- a/runtime/src/main/java/com4j/Variant.java +++ b/runtime/src/main/java/com4j/Variant.java @@ -356,14 +356,32 @@ public int comEnumValue() { } } + /** + * Determine the correct size of {@link Variant}. + * + * The size of the variant depends on whether this is + * a 32 or 64 bit system due to pointers in the structure + * definition. See https://docs.microsoft.com/en-gb/windows/desktop/api/oaidl/ns-oaidl-tagvariant + */ + private static int variantSize() { + /* Typical os.arch values: `x86_64`, `amd64` */ + String model = System.getProperty("os.arch"); + if (model.indexOf("64") != -1) { + return 24; + } + return 16; + } + + private static final int variantSize = variantSize(); + /** * Creates an empty {@link Variant}. */ public Variant() { - image = ByteBuffer.allocateDirect(16); + image = ByteBuffer.allocateDirect(variantSize); image.order(ByteOrder.LITTLE_ENDIAN); // The initial content of a buffer is, in general, undefined. See the documentation of java.nio.Buffer. - byte[] b = new byte[16]; // this initializes the array with zeros + byte[] b = new byte[variantSize]; // this initializes the array with zeros image.put(b); // this prints the zeros to the buffer to guarantee, that the buffer is initialized with zeros. image.position(0); }