4 messages in net.java.dev.jna.usersRe: [jna-users] struct containing poi...
FromSent OnAttachments
seb bratieresJul 17, 2007 7:28 am 
Timothy WallJul 17, 2007 9:19 am 
j impalaJul 17, 2007 10:24 am 
Timothy WallJul 17, 2007 11:26 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] struct containing pointer to struct arrayActions...
From:Timothy Wall (twal@dev.java.net)
Date:Jul 17, 2007 9:19:42 am
List:net.java.dev.jna.users

On Jul 17, 2007, at 10:28 AM, seb bratieres wrote:

Hi, I have trouble to correctly construct and pass a struct that contains a pointer to a struct array. Here is the problem case. I have tried two solutions which I could think of, none of which works.

Thank you Sebastien

* C types typedef struct VI_TagEntry_t{ TCHAR* name; VI_Variant value; // another struct } VI_TagEntry;

// tag list storage typedef struct VI_TagList_t{ unsigned long size; VI_TagEntry* list; } VI_TagList;

* JNA types public static class VI_TagEntry extends Structure { public Pointer name;

public VI_Variant value; // another Structure }

public static class VI_TagList extends Structure { public long size;

public Pointer list; // VI_TagEntry* list;

}

* JNA test code private Dll.VI_TagList createSampleVI_TagList() { Dll.VI_TagList tagList = new Dll.VI_TagList();

Structure[] tagEntryArray = (new Dll.VI_TagEntry()).toArray (2); tagEntryArray[0] = createSampleVI_TagEntry();

You're overwriting the first array element, so your array of structures is no longer contiguous in memory. When your C code moves to the second structure, it walks off the end of valid memory.

Change your "createSampleV1..." to "initializeSampleV1..." and pass the existing first array element.

Dll.VI_TagEntry intermediate = createSampleVI_TagEntry(); intermediate.value.data.longVal = 13; tagList.list = tagEntryArray[0].getPointer(); tagEntryArray[1] = intermediate;

tagList.size = 2;

tagList.write(); return tagList; }

public void testVI_TagList() { lib.testVI_TagList(createSampleVI_TagList()); }

* C test code _declspec(dllexport) void __cdecl testVI_TagEntry(VI_TagEntry* tagEntry) { printf("tagEntry.name=%S \n", tagEntry->name); printf("tagEntry.value.d.longVal=%i \n", tagEntry-

value.d.longVal);

}

_declspec(dllexport) void __cdecl testVI_TagList(VI_TagList* tagList) { printf("tagList->size=%i \n", tagList->size); testVI_TagEntry(tagList->list); }

* output tagList->size=2 # # An unexpected error has been detected by HotSpot Virtual Machine: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0af51f43, pid=3500, tid=4004 # # Java VM: Java HotSpot(TM) Client VM (1.5.0_11-b03 mixed mode) etc