2 messages in net.java.dev.jna.users[jna-users] platform independent mapp...
FromSent OnAttachments
sachin kumarNov 5, 2008 9:52 pm 
Timothy WallNov 6, 2008 5:30 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] platform independent mapping for wchar_tActions...
From:sachin kumar (sack@gmail.com)
Date:Nov 5, 2008 9:52:12 pm
List:net.java.dev.jna.users

Hi,

I am having issues in getting the correct string values from the mac os library. The string returned has lot of garbage characters at the end. I have created a library that returns the following structure in both Mac and windows.

#define MAX_SIZE_OF_NSP_NAME 128 typedef struct _CONNECTED_NSP_INFO { int structureSize; wchar_t NSPName[MAX_SIZE_OF_NSP_NAME]; wchar_t NSPRealm[MAX_SIZE_OF_NSP_NAME] ; int NSPid; } CONNECTED_NSP_INFO, *CONNECTED_NSP_INFO_P;

In java I have mapped the above structure with the following class public class ConnectedNspInfo extends Structure { private final int MAX_SIZE_OF_NSP_NAME = 128;

public int structureSize = 100; public char[] name = new char[MAX_SIZE_OF_NSP_NAME]; public char[] realm = new char[MAX_SIZE_OF_NSP_NAME]; public int nspId = 0;

public String getName() { return Native.toString(name); }

... ... }

Prototype: GetConnectedNSP (DEVICE_ID_P pDeviceId, CONNECTED_NSP_INFO_P pConnectedNSP);

Now as per jna documentation wchar_t maps to java char but char is 2 bytes and wchar_t is 4 bytes and that's causing the issue on mac os. In windows everything works fine as last two bytes of wchar_t gets truncated and the correct string value is returned but on Mac OS nothing gets truncated. So the memory is read for 128 *4 = 512 bytes and mapped to first string "name" which results in garbage characters at the end of string "name". And all other values after "name" field also get affected.

If I use different buffer sizes e.g 128 for windows and 64 for mac then I am getting the correct values (only for strings whose length < 64)

Is there any way to fix this issue?

Thanks Sachin