Structure array size from another property

54 views
Skip to first unread message

Cyber Avater

unread,
Nov 29, 2023, 3:34:41 PM11/29/23
to Java Native Access
public class MyStruct extends Structure {
public int sz; // This field specifies the size of the array
// public int[] arr = new int[20]; // this works
public int[] arr; // But I want to initialize it later after reading sz


@Override
protected List<String> getFieldOrder() {
return Arrays.asList("b", "a");
}

public MyStruct(Pointer p) {
super(p);

read(); //will fail if I dont initialize an empty array

}
}


How do I achive this? is it even possible?

Cyber Avater

unread,
Nov 29, 2023, 3:39:24 PM11/29/23
to Java Native Access
I know I forget to update the getaFieldOrder fuction

Matthias Bläsing

unread,
Nov 29, 2023, 4:07:30 PM11/29/23
to jna-...@googlegroups.com
Hi,

you need to modify read to make this work. An example can be found
here:

https://github.com/java-native-access/jna/blob/365629530635440381f63939f7891456992fe91b/contrib/platform/src/com/sun/jna/platform/win32/Ddeml.java#L359-L443

In the example (MONHSZSTRUCT) the structure has a dynamic size. The
size is declared as the first parameter. For the read case, that field
is read first, then the structure updates its size info to the just
read info and runs the base read implementation with that.

Another example can be found here:

https://github.com/java-native-access/jna/blob/365629530635440381f63939f7891456992fe91b/contrib/platform/src/com/sun/jna/platform/win32/Winspool.java#L755-L807

I'm pretty sure, that the array allocation in 797 should be done
differently (using "new PRINTER_NOTIFY_INFO_DATA().toArray(count)"),
but it should transfer the idea:

When the Count indicates, that the array of PRINTER_NOTIFY_INFO_DATA
would be empty, the non-array fields are read directly and thus the
problem of reading an empty in JNA is worked around. If the array is
filled, the normal read implementation takes over.

Hint: The unittests and the mappings in the platform project can be a
good source for examples and possible options. They should be read with
a critical eye, but still...

HTH

Matthias
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jna-users/38cca395-fdf8-41ee-918d-b6f03ca159fcn%40googlegroups.com.

Cyber Avater

unread,
Nov 30, 2023, 12:41:59 AM11/30/23
to Java Native Access
Hi, thanks for your direction it was a great help.

But there is a problem, that is here https://github.com/java-native-access/jna/blob/365629530635440381f63939f7891456992fe91b/contrib/platform/src/com/sun/jna/platform/win32/Winspool.java#L798

if (count == 0) {
                Count = count;
                Version = (Integer) readField("Version");
                Flags = (Integer) readField("Flags");
            }

If the array size is 0 I've to manually set all fields, which is okay in this case but I have a very large class and I don't want to set all fields manually (If I don't I get Index 0 out of bounds for length 0), is there any workaround for this?

Cyber Avater

unread,
Nov 30, 2023, 1:02:02 AM11/30/23
to Java Native Access
I've tried Overriding the readField method but it seems it doesn't get called by super.read(), my Override only gets called when I call the function myself.

Matthias Bläsing

unread,
Nov 30, 2023, 2:23:28 PM11/30/23
to jna-...@googlegroups.com
Hi,

Am Mittwoch, dem 29.11.2023 um 22:02 -0800 schrieb Cyber Avater:
> I've tried Overriding the readField method but it seems it doesn't get called by super.read(), my Override only gets called when I call the function myself.

you are looking at the wrong method. The method called from
Structure#read is Structure#readField(StructField).

Greetings

Matthias
Message has been deleted
Message has been deleted

Cyber Avater

unread,
Dec 1, 2023, 12:11:50 AM12/1/23
to Java Native Access
Thanks for your help, couldn't do it without you.
This is the best I could come up with, (let me know if you have any recommendations)
https://pastebin.com/Q6dJUWcN

BTW, why do my messages get deleted?

Matthias Bläsing

unread,
Dec 1, 2023, 3:12:42 PM12/1/23
to jna-...@googlegroups.com
Hi,

Am Donnerstag, dem 30.11.2023 um 21:11 -0800 schrieb Cyber Avater:
> This is the best I could come up with, (let me know if you have any recommendations)
> https://pastebin.com/Q6dJUWcN

Looks like a good approach, that I might use as inspiration in the
future.

> BTW, why do my messages get deleted?

Only google can tell ;-)

Your posts were held in moderation. I cleared them from moderation and
they were sent via the mail distribution, but they don't show up in the
web interface. *irony* If google would work in the web area, I would
raise this as an issue, but given their low internet experience, it is
understandable, that even basic services often suck.*irony*

Greetings

Matthias

Cyber Avater

unread,
Dec 3, 2023, 12:09:18 PM12/3/23
to Java Native Access
Okay, thanks again and take care.
Reply all
Reply to author
Forward
0 new messages