6 messages in net.java.dev.jna.usersRe: [jna-users] how to set a pointer
FromSent OnAttachments
Glick, Gene (GE Indust, Security)Sep 19, 2007 2:02 pm 
Timothy WallSep 19, 2007 2:55 pm 
Glick, Gene (GE Indust, Security)Sep 20, 2007 9:00 am 
Albert StrasheimSep 20, 2007 9:23 am 
Timothy WallSep 20, 2007 9:30 am 
Glick, Gene (GE Indust, Security)Sep 20, 2007 11:23 am 
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] how to set a pointerActions...
From:Timothy Wall (twal@dev.java.net)
Date:Sep 20, 2007 9:30:15 am
List:net.java.dev.jna.users

On Sep 20, 2007, at 12:01 PM, Glick, Gene (GE Indust, Security) wrote:

I'm confused. ByteBuffer is an abstract class. To use it means writing all its methods first, right? Seems like a lot of work.

Look at ByteBuffer.wrap (a static method). You don't have to implement anything; the class provides you with an instantiation. The JNA native layer can use Buffer interchangeably with native pointers.

Let me be a little more specific in what I'm trying to do:

1. In some java code, I have created a 'byte[] buf' of data. 2. In some c shared library (.dll) the method is looking for a passed parameter of 'uint8_t *buf' 3. The jna header file (not written by me, but could be modified if need be) is looking for type 'Pointer buf'.

in C it goes something like this: a) byte buf[10]; b) byte *ptrBuf; c) ptrBuf = &buf[0]; d) someMethod(uint8_t *ptrBuf); in Java, using the JNA it goes something like this: a) byte [] buf = new byte[10]; b) Pointer ptrBuf = new Pointer(); c) *** here's where I am lost *** d) someInstance.someMethod(Pointer ptrBuf);

So, how to get the Pointer to be set to &buf[0]?

Since your C method is expecting a pointer to uint8_t, you can safely declare the argument on the Java side as "byte[]". No conversion necessary. I'd recommend changing the Java interface definition to be more precise.

If your C program expects a pointer or array of a specific type, then you should use a Java array of the matching type in the interface method definition.

char* = byte[] short* = short[] int* = int[] etc.

If your C program expects a block of memory that might have different representations, then "Buffer" is more appropriate. Java provides "wrap" methods on ByteBuffer, ShortBuffer, IntBuffer, et al. which can wrap a java primitive array, and JNA will interpret the argument as a pointer to the data wrapped.

With primitive arrays, Buffers, Structures/Unions, PointerType, et al., there's little reason to use Pointer as an argument type except as a placeholder (usually where "void*" is used as an opaque handle).