4 messages in net.java.dev.jna.usersRe: [jna-users] Bad string.
FromSent OnAttachments
Johan LundMay 13, 2009 2:01 am 
Timothy WallMay 13, 2009 3:12 am 
Novatchkov HristoMay 14, 2009 10:05 am 
Johan LundMay 15, 2009 5:09 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] Bad string.Actions...
From:Johan Lund (joh@risken.se)
Date:May 15, 2009 5:09:05 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'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

Please help me!!

/Johan

On Wed, May 13, 2009 at 12:13 PM, Timothy Wall <twal@dev.java.net>wrote:

On May 13, 2009, at 5:01 AM, Johan Lund wrote:

Hi friends

I've managed to call som functions within a dll, I can readback the results but unfortunately I am not able to call one of the most important functions.

This is how the method looks like: __declspec(dllexport) ADK_ERROR __stdcall AdkOpenEx(CHAR* pszSystemPath, CHAR* pszFtgPath, long lLogOn);

My .h file includes wtypes.h which has this row: typedef char CHAR;

System.setProperty("jna.encoding", "ISO-8859-1"); System.setProperty("jna. library.path","C:\\TEST"); String prg="C:\\TEST\\Common files" String ftg="C:\\TEST\\Mitt företag" NativeLong lLogOn=new NativeLong(1); Map options = new HashMap(); options.put(Library.OPTION_FUNCTION_MAPPER,new com.sun.jna.win32.StdCallFunctionMapper()); Adk adk = (Adk) Native.loadLibrary("Adk", Adk.class,options);

My first approach to a stub was this: public ADK_ERROR AdkOpenEx(String pszSystemPath, String pszFtgPath, NativeLong lLogOn);

This is the correct mapping. The others are mostly equivalent (although byte[] might be useful for ensuring you know *exactly* which bytes are sent, since they are passed through without any translation.

The dll keeps returning an error which states that the path sent to the function is wrong, but it is not wrong. It is the correct path which I try to use but probably the way which I pass it into the function is wrong. Is none of my examples above correct?

Can it be something with encoding, the paths contains Swedish signs?

I'd suggest writing a very simple test which sends *exactly* the bytes you think are correct. Compare that to what Native.toByteArray() produces, and also see what your native code does with it. That will indicate whether the encoding is incorrect or whether your native code is behaving contrary to your expectations.