4 messages in net.java.dev.jna.users[jna-users] Wrong Structure Values Pa...
FromSent OnAttachments
RWAD...@UP.COMJun 20, 2008 5:41 am 
Michael Brewer-DavisJun 20, 2008 10:59 am 
RWAD...@UP.COMJun 23, 2008 5:27 am 
Timothy WallJun 23, 2008 11:52 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] Wrong Structure Values PassedActions...
From:RWAD...@UP.COM (RWAD@UP.COM)
Date:Jun 20, 2008 5:41:43 am
List:net.java.dev.jna.users

I'm trying to pass an array of structures to a native function. Though my test program runs without crashing, the native function receives the wrong values.

The Java code:

________________________________________

import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Structure;

/** Provides access to a native library. */ public class Point extends Structure {

public int x; public int y;

public interface API extends Library { API INSTANCE = (API)Native.loadLibrary ("c:/upapps/efm/bin/efms-api.dll", API.class); void pointTest(Point[] points, int count); } public Point() {} public Point(int x, int y) { this.x = x; this.y = y; } public static void main(String[] unused) { Point[] points = new Point[2]; points[0] = new Point(1,2); points[1] = new Point(3,4); Point[] structures = (Point[])new Point().toArray(points); API.INSTANCE.pointTest(structures, structures.length); }}

________________________________________

The native C function the Java code is calling:

#include <stdio.h>

typedef struct { int x; int y; } POINT;

void pointTest(POINT points[], int count) { int i = 0; while (i++ < count) printf("Point %i: %i,%i\n", i, points[i].x, points[i].y); }

________________________________________

I expected to get:

Point 1: 1,2 Point 2: 3,4

But instead I get:

Point 1: 0,0 Point 2: 196610,34079172