Search code examples
javac++mappingjna

JNA wrong structure field values


I'm dancing around JNA adapter to "twain.h" - almost did it, but still have some problems with getting scanner capabilities. There is an original enity:

typedef unsigned short TW_UINT16, FAR *pTW_UINT16;
typedef unsigned long  TW_UINT32, FAR *pTW_UINT32;

typedef struct {
   TW_UINT16  ItemType;
   TW_UINT32  MinValue;     /* Starting value in the range.           */
   TW_UINT32  MaxValue;     /* Final value in the range.              */
   TW_UINT32  StepSize;     /* Increment from MinValue to MaxValue.   */
   TW_UINT32  DefaultValue; /* Power-up value.                        */
   TW_UINT32  CurrentValue; /* The value that is currently in effect. */
} TW_RANGE, FAR * pTW_RANGE;

This is my mapping (thnx JNAerator)

public static class TW_RANGE extends Structure {
        /** C type : TW_UINT16 */
        public short ItemType;
        /** C type : TW_UINT32 */
        public NativeLong MinValue;
        /** C type : TW_UINT32 */
        public NativeLong MaxValue;
        /** C type : TW_UINT32 */
        public NativeLong StepSize;
        /** C type : TW_UINT32 */
        public NativeLong DefaultValue;
        /** C type : TW_UINT32 */
        public NativeLong CurrentValue;
        public TW_RANGE() {
            super();
        }
        protected List<? > getFieldOrder() {
            return Arrays.asList("ItemType", "MinValue", "MaxValue", "StepSize", "DefaultValue", "CurrentValue");
        }
}

When I'm asking scanner for this entity, scanner sends me back filled object, but when I create it through

new TW_RANGE(pointer)

it returns me some wired stuff

TwaindsmLibrary$TW_RANGE(native@0x157c000c) (22 bytes) {
  short ItemType@0=870
  NativeLong MinValue@2=1572916
  NativeLong MaxValue@6=5500
  NativeLong StepSize@a=2097152
  NativeLong DefaultValue@e=5500
  NativeLong CurrentValue@12=2621440
}
  1. How can I interpret this values?
  2. Can I dump native structure and analyze it with some tool? Have seen Pointer.dump, but it requires the size of dumped value, how can i get size of structure under pointer?

Solution

  • I got it! After little debugging C++ code, I found that (TW_RANGE*)pointer makes the same mess as Java does. But (TW_RANGE**)pointer works like a charm. So

    new TW_RANGE(pointer.getPointer(0))
    

    [APPLAUSE] This solution lead me to random memory access errors. I should work with Twain native MemAllocate/Lock/Free functions which returns right values from given pointer.