Structs by reference as argument, containing function pointer

683 views
Skip to first unread message

awu5ima

unread,
Jan 17, 2012, 4:52:01 AM1/17/12
to Java Native Access
Hello,

I have been unable to find documentation on this and cannot figure it
out by myself, so:


I am calling a native function which expects a certain kind of
structure's reference as argument. The function will fill the structre
with variable and function pointers. How does one define such a struct
using JNA and how to call the function pointers? Is this even
possible?

awu5ima

unread,
Jan 17, 2012, 4:55:12 AM1/17/12
to Java Native Access
Correction: The function wants a reference to struct pointer.

Timothy Wall

unread,
Jan 17, 2012, 5:58:28 AM1/17/12
to jna-...@googlegroups.com
It's much clearer if you simply provide the native declaration, or a suitable facsimile.

"reference to struct pointer" => not exactly common terminology in C, but "struct **" is my best guess
struct pointer => "struct *"

I'm guessing you want "struct *" and not "struct **".
struct * => Structure (when used as a function parameter) this is sufficient for the native call to be able to populate the struct

As for calling the function pointers, see the JNA doc sections on callbacks/function pointers.

Message has been deleted

Timothy Wall

unread,
Jan 17, 2012, 9:25:40 AM1/17/12
to jna-...@googlegroups.com
On Jan 17, 2012, at 9:05 AM, awu5ima wrote:

> Hi Timothy,
>
> Yes, sorry for the missing data, here you go:
>
> // native code
> struct {
> } struct_interface;

hm... how about "provide the native declaration of the function you are trying to map"?

awu5ima

unread,
Jan 17, 2012, 9:35:42 AM1/17/12
to Java Native Access
Hi Timothy,

Yes, sorry for the missing data. And I accidentally posted my reply
before typing it in completely, so here you go:

// native code
struct {
int (*uninit) (struct_interface *this, int param);
int attr;
} struct_interface

int populateInterface(struct_interface **intf);

// use it

struct_interface *ret_struct = null;
populateInterface(&ret_struct);

ret_struct->uninit(ret_struct, 0);

Timothy Wall

unread,
Jan 17, 2012, 9:44:38 AM1/17/12
to jna-...@googlegroups.com

On Jan 17, 2012, at 9:35 AM, awu5ima wrote:

> Hi Timothy,
>

> Yes, sorry for the missing data. And I accidentally posted my reply
> before typing it in completely, so here you go:
>
> // native code
> struct {
> int (*uninit) (struct_interface *this, int param);
> int attr;
> } struct_interface
>
> int populateInterface(struct_interface **intf);
>
> // use it
>
> struct_interface *ret_struct = null;
> populateInterface(&ret_struct);
>
> ret_struct->uninit(ret_struct, 0);

In this case, you need to obtain the address of the structure from the native code (use PointerByReference as the argument type, and PointerByReference.getValue() to get the pointer after the call). You can then use that address to initialize your custom Structure instance, e.g.

public class MyStructure extends Structure {
public MyCallbackType uninit;
public int attr;
// ...
public MyStructure(Pointer p) {
super(p);
read();
}
// ...

awu5ima

unread,
Jan 17, 2012, 11:11:25 AM1/17/12
to Java Native Access
Okay, so my implementation goes like this:

//JNA
class nativeLibrary {
...
// Native.register() etc.
public static native int populateInterface(PointerByReference p);
...
}

class struct_intf extends Structure {

public struct_intf(Pointer p) {
super(p);
read();
}
public interface uninitFunc extends Callback {
int invoke(Pointer p);
}

public uninitFunc uninit;
public int attrs;

}

/// Use it

PointerByReference ptr = new PointerByReference();
struct_intf intf;

nativeLibrary.populateInterface(ptr);
intf = new struct_intf(ptr);

intf.uninit.invoke(intf.getPointer());


The code compiles but running it throws the following exception:

Exception in thread "main" java.lang.Error: Exception reading field
'Release' in class struct_intf: java.lang.IllegalAccessException:
Class com.sun.jna.Structure can not access a member of class
struct_intf with modifiers "public"

awu5ima

unread,
Jan 17, 2012, 12:45:02 PM1/17/12
to Java Native Access
Ah, nevermind. That had nothing to do with JNA but with my class file
declarations and I got the code to work now.

Much obliged, Timothy.
Reply all
Reply to author
Forward
0 new messages