4 messages in net.java.dev.jna.usersRe: [jna-users] Mapping two-dimension...
FromSent OnAttachments
Albert StrasheimAug 29, 2007 1:41 pm 
Timothy WallAug 29, 2007 3:30 pm 
Albert StrasheimSep 12, 2007 2:14 pm 
Albert StrasheimSep 12, 2007 2:47 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Mapping two-dimensional arraysActions...
From:Albert Strasheim (full@gmail.com)
Date:Sep 12, 2007 2:14:26 pm
List:net.java.dev.jna.users

Hello all

Following the StringArrays, example I created the following class:

class LongArrays extends Memory { private final List<Memory> buffers;

public LongArrays(final long[][] arrs) { super(arrs.length * Pointer.SIZE); this.buffers = new ArrayList<Memory>(arrs.length); for (int i = 0; i < arrs.length; i++) { Memory buf = new Memory(arrs[i].length * LONG_BYTES); buf.getByteBuffer(0, buf.getSize()).asLongBuffer().put(arrs[i]); buffers.add(buf); setPointer(i * Pointer.SIZE, buf); } } }

and I load my library as follows:

Map<String, TypeMapper> options = new HashMap<String, TypeMapper>(); DefaultTypeMapper mapper = new DefaultTypeMapper(); mapper.addToNativeConverter(long[][].class, new ToNativeConverter() { public Class<LongArrays> nativeType() { return LongArrays.class; } public Object toNative(final Object obj, final ToNativeContext context) { return new LongArrays((long[][]) obj); } }); options.put(Library.OPTION_TYPE_MAPPER, mapper); MyLib lib = (MyLib) Native.loadLibrary("mylib", MyLib.class, options);

I then call my mapped function that takes a long[][] argument.

From my debugging I can see that the type mapper kicks in and does the conversion.

I can also see this parameter is handled by the (*env)->IsInstanceOf(env, arg, classPointer) case in dispatch() in dispatch.c.

If I print the native address here, I can see that it corresponds to the address of my LongArrays instance.

However, when I arrive in the native function I'm calling, this argument has become null, so it would seem the libffi might be misbehaving. At this point, I don't really know what went wrong, since I can't properly debug the JNA code using Visual Studio, since GCC doesn't generate symbols that this debugger understands.

Any thoughts? I might try to build JNA's native code using Visual Studio so that I can use the debugger all the way through...

Regards,

Albert

----- Original Message ----- From: "Timothy Wall" <twal@dev.java.net> To: <use@jna.dev.java.net> Sent: Thursday, August 30, 2007 12:31 AM Subject: Re: [jna-users] Mapping two-dimensional arrays

There isn't any direct mapping, since there isn't a canonical two- dimensional data layout in C.

You'll have to set up the data manually, or create a class to handle the layout. See StringArray in Function.java; it maintains an array of char*, which might be similar to what you're doing.

Figure out what your data should look like on the Java side, then do the conversion to a Pointer type.

If you want your defined interface to take a two-dimensional java array, e.g.

void foo(int[][] data)

then you'll need to define a type mapper which converts the java type int[][] into a type the invocation layer can handle (namely, Pointer).

On Aug 29, 2007, at 4:42 PM, Albert Strasheim wrote:

Hello all

I'm trying to map a function that takes a two-dimensional array parameter, like:

void foo(int* bar[]);

or equivalently:

void foo(int** bar);

I took a quick look at the JNA unit tests, but I couldn't find an obvious example of how to map this kind of parameter.

Any help would be appreciated.

Cheers,