6 messages in net.java.dev.jna.usersRe: [jna-users] Reading a native array
FromSent OnAttachments
ThinnerDec 13, 2008 5:06 am 
Timothy WallDec 13, 2008 5:11 am 
ThinnerDec 13, 2008 5:15 am 
Timothy WallDec 13, 2008 5:32 am 
ThinnerDec 13, 2008 6:17 am 
Timothy WallDec 13, 2008 9:00 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] Reading a native arrayActions...
From:Timothy Wall (twal@dev.java.net)
Date:Dec 13, 2008 9:00:06 am
List:net.java.dev.jna.users

if the structure is declared with a nested array, you must use a primitive array. if it's declared as a pointer, you need to use a pointer.

you can dynamically allocate the byte[] on read if you overload the read() method like thus:

read() { this.length = getPointer().getInt(0); this.buf = new byte[length]; super.read(); }

but primitive arrays nested in a struct must always be initialized to *something* before you pass them to a function.

On Dec 13, 2008, at 9:17 AM, Thinner wrote:

I still got seg fault when calling Pointer.getByteArray(0, stru.length); It works if I change the type of data back to byte[] and initialize the byte array to the maximum length data will ever have (2404b). Got any ideas?

On Sat, Dec 13, 2008 at 2:32 PM, Timothy Wall <twal@dev.java.net> wrote: Pointer.getString(0, stru.length) if it's a string. Pointer.getByteArray(0, stru.length) if it's raw data.

getByteBuffer() pulls out a pointer from offset zero and creates a buffer from that, as opposed to converting the original pointer to a buffer, which is what you were expecting.

On Dec 13, 2008, at 8:16 AM, Thinner wrote:

Ok, I changed the type of data to Pointer and then called Pointer.getByteBuffer(0, length); and then tried to loop through the buffer but as soon as I call ByteBuffer.get() I get seg fault. Do I need to allocate something somewhere? :)

On Sat, Dec 13, 2008 at 2:12 PM, Timothy Wall <twal@dev.java.net> wrote: primitive arrays within a struct are inlined. you need a pointer type.

On Dec 13, 2008, at 8:07 AM, Thinner wrote:

Hello again everyone.

I'm trying to read a native char* from my Java app but I keep getting seg fault.

struct stru{ int length; char* data; }

struct stru *return_value some_function(){ }

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

int length; byte[] data;

public stru{ data = new byte[1]; } }

public static void main(String[] args){ myLibrary.INSTANCE }

Nor a spork, I suppose.