Re: Array of a simple structure in JNA api

177 views
Skip to first unread message
Message has been deleted

Tobias Wolf

unread,
Jan 17, 2013, 11:19:25 AM1/17/13
to jna-...@googlegroups.com
My Memory dump look like that:
 

OaIdl$SAFEARRAYBOUND(auto-allocated@0xf41290 (16 bytes)) {

WinDef$ULONG cElements@0=10

WinDef$LONG lLbound@8=0

}

memory dump

[0a000000]

[00000000]

[00000000]

[00000000]

 

I changed the class like that to be absolutely conform to the MS api:

 public static class SAFEARRAYBOUND extends Structure {
  public static class ByReference extends SAFEARRAYBOUND implements
    Structure.ByReference {
  }

  public ULONG cElements;

  public LONG lLbound;

  public SAFEARRAYBOUND() {
  }

  public SAFEARRAYBOUND(Pointer pointer) {
   super(pointer);
   this.read();
  }

  public SAFEARRAYBOUND(int cElements, int lLbound) {
   this.cElements = new ULONG(cElements);
   this.lLbound = new LONG(lLbound);
   this.write();
  }

  @Override
  protected List getFieldOrder() {
   return Arrays.asList(new String[] { "cElements", "lLbound" });
  }
 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


On Wednesday, January 16, 2013 11:33:21 AM UTC+1, Tobias Wolf wrote:
How are arrays of structures handled by JNA api?

For example I have:

        SAFEARRAYBOUND[] rgsabound = new SAFEARRAYBOUND[1];
        rgsabound[0] = new SAFEARRAYBOUND(size, 0);

and when I take a look to the backing memory then I see that it is not being written to memory. Please explain why!
Which methods of the Structure class needs to be overwritten to solved that?


below is the code of the structure:

    public static class SAFEARRAYBOUND extends Structure {

        public static class ByReference extends SAFEARRAYBOUND implements
                Structure.ByReference {
        }

        public int cElements;

        public int lLbound;

        public SAFEARRAYBOUND() {
        }

        public SAFEARRAYBOUND(int cElements, int lLbound) {
            this.cElements = cElements;
            this.lLbound = lLbound;
        }

        public SAFEARRAYBOUND(Pointer pointer) {
            super(pointer);
        }

        @Override
        protected List getFieldOrder() {
            return Arrays.asList(new String[] { "cElements", "lLbound" });
        }
    }

Timothy Wall

unread,
Jan 17, 2013, 12:00:54 PM1/17/13
to jna-...@googlegroups.com
What are you trying to achieve? It's been a long time since I dealt with COM.

The only special handling of arrays of structures that JNA provides is with Structure.toArray(), which allocates a large block of memory to represent a contiguous array of native struct (which usually are indistinguishable from a simple "struct *").

Pointer[] and PointerType[] have temporary memory allocated to hold arrays of native pointers; use these when dealing with arrays of pointer to struct (usually represented by "struct**" in native code).

Timothy Wall

unread,
Jan 17, 2013, 12:04:10 PM1/17/13
to jna-...@googlegroups.com
Your type sizing seems off. The structure should only be 8 bytes (4 bytes for each field), unless for whatever reason you're running on something other than Windows.

LONG on windows is 32 bits on both 32- and 64-bit versions.

Tobias Wolf

unread,
Jan 17, 2013, 12:45:26 PM1/17/13
to jna-...@googlegroups.com
You`re joking?! I`m on Windows 8 32bit.

Timothy Wall

unread,
Jan 17, 2013, 1:32:17 PM1/17/13
to jna-...@googlegroups.com
hm... maybe that's part of the problem. maybe JNA is not detecting that windows 8 is windows --- what does the VM report for "os.name"?

Tobias Wolf

unread,
Jan 18, 2013, 4:04:18 AM1/18/13
to jna-...@googlegroups.com
ehhhh, no it was my fault, I made a wrong data type definition.
But I can check the jna os detetion mode, please give me a pointer in which class I`ve to look for. Is JNA overwriting the os.name property?

Timothy Wall

unread,
Jan 18, 2013, 7:35:50 AM1/18/13
to jna-...@googlegroups.com
Platform.isWindows()

JNA simply looks at os.name system property.

Tobias Wolf

unread,
Jan 18, 2013, 10:58:51 AM1/18/13
to jna-...@googlegroups.com
the property name.os gives "Windows 8", JNA works correct
Reply all
Reply to author
Forward
0 new messages