

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
2 messages in net.java.dev.jna.usersRe: [jna-users] platform independent ...| From | Sent On | Attachments |
|---|---|---|
| sachin kumar | Nov 5, 2008 9:52 pm | |
| Timothy Wall | Nov 6, 2008 5:30 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] platform independent mapping for wchar_t | Actions... |
|---|---|---|
| From: | Timothy Wall (twal...@dev.java.net) | |
| Date: | Nov 6, 2008 5:30:34 am | |
| List: | net.java.dev.jna.users | |
On Nov 6, 2008, at 12:52 AM, sachin kumar wrote:
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.
If the string is correct but has garbage at the end then the issue is that the string is not null-terminated. If there were a size mismatch then your Java characters would be interspersed with null or garbage characters.
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); }
I don't know how structureSize is being used, but 100 is less than even the size of your "name" field.
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.
Where jchar and wchar_t sizes do not match, a temporary buffer of the size of the target array is used, and elements written one by one, after which the temporary buffer is copied to the target array. On OSX, 4 bytes per char are allocated for the structure's native backing memory. For the structure you've given, you should find that Structure.size() returns 1032 (2*512 + 2*4).
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.
Compare the results of MyStructure.size() with sizeof(mystruct) in native code. If JNA were not allocating enough memory for "name" as you suggest, then part of the "name" field would be written to subsequent fields.
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?
Check that *your* library is properly handling the difference in size. How are you populating the "name" field (and others)?
As a test, return a statically-initialized version of your structure with known values, then see where the fields do or do not align properly.







