6 messages in net.java.dev.jna.users[jna-users] Reading string from Union
FromSent OnAttachments
Johan LundMay 15, 2009 6:46 am 
Daniel KaufmannMay 17, 2009 7:22 am 
Johan LundMay 17, 2009 8:19 am 
Johan LundMay 17, 2009 8:22 am 
Daniel KaufmannMay 17, 2009 9:17 am 
Timothy WallMay 17, 2009 9:17 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:[jna-users] Reading string from UnionActions...
From:Johan Lund (joh@risken.se)
Date:May 15, 2009 6:46:43 am
List:net.java.dev.jna.users

Hi Timothy and everybody else who stumbles out there...

I started from zero as you told me Timothy, today everything works fine except from I sometime gets a Malloc error... I think that the problem arises when this Union varies in size, this Union (value_union) is stored inside a Structure (ADK_FIELD) which is stored within a big array of ADK_FIELD's inside an ADK_DATA structure...

I've seen some postings about this before and I belive that it's because I'm using a Union (with a String inside) which sometimes gets larger than it's share of memory. I've seen the advice about creating a dummy field which always is bigger than the othersand therfore you'll be safe...

But I'm a bit stupid and don't know how to actually do that....

My union is used inside a structure, I'vr only posted my union bellow:

public static class value_union extends com.sun.jna.Union { /// Allocate a new value_union struct on the heap public value_union() {} /// Cast data at given memory location (pointer + offset) as an existing value_union struct public value_union(com.sun.jna.Pointer pointer, int offset) { super(); useMemory(pointer, offset); read(); } /// Create an instance that shares its memory with another value_union instance public value_union(value_union struct) { this(struct.getPointer(), 0); } public static class ByReference extends value_union implements com.sun.jna.Structure.ByReference { /// Allocate a new value_union.ByRef struct on the heap public ByReference() {} /// Create an instance that shares its memory with another value_union instance public ByReference(value_union struct) { super(struct.getPointer(), 0); } } public static class ByValue extends value_union implements com.sun.jna.Structure.ByValue { /// Allocate a new value_union.ByVal struct on the heap public ByValue() {} /// Create an instance that shares its memory with another value_union instance public ByValue(value_union struct) { super(struct.getPointer(), 0); } } public double d; public String s; public int l; public int b; public int date; public ADK_DATA.ByReference data; }

My approach was to try to insert a public "byte[] memDummy" after the fields I actually use and initialize it within the default constructor like this memDummy=new byte[1024], but that doesn't work at all. How do you pull that byte[] trick of???

I always tend to get something like this:

Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) C [ntdll.dll+0x68915] C [ntdll.dll+0x68752] C [msvcrt.dll+0x9e04] C [jna753047577214618406.tmp+0x5ff8] J com.sun.jna.Memory.malloc(J)J J com.sun.jna.Memory.<init>(J)V J se.conditio.service.api.AdkWrapper.getAdkStr(Lse/conditio/service/api/adk/AdkLibrary$ADK_DATA;I)Ljava/lang/String; j se.conditio.risken.io.AdkImport.doIt(Ljava/util/HashMap;Ljava/util/Vector;)V+178 j se.conditio.risken.io.AdkImport.main([Ljava/lang/String;)V+2 v ~StubRoutines::call_stub

I've alse tried to replace the public String s; with public com.sun.jna.Pointer s; since I thought that a pointer would always be of the same size, but I got the same problem.

Please help me!!

/Johan