4 messages in net.java.dev.jna.usersstruct vs &struct vs struct *
FromSent OnAttachments
bb...@cox.netJul 16, 2007 9:21 am 
Timothy WallJul 16, 2007 11:54 am 
bb...@cox.netJul 17, 2007 4:45 am 
bb...@cox.netJul 17, 2007 4:53 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:struct vs &struct vs struct *Actions...
From:bb...@cox.net (bb@cox.net)
Date:Jul 16, 2007 9:21:22 am
List:net.java.dev.jna.users

What is the difference between passing a C struct (used as input-only
parameter), an address of a C struct (still used as an input-only parameter),
and a pointer to struct, partially filled-in, but which will be filled in by the called function?

When the following C is translated into Java+JNA, what does it look like? -----------------C version--------------------------------- struct A { int a1; } struct B { int b1; } struct D { int d1; int d2; } struct C { int n; struct D* ds; } extern int f(struct A a, struct B *b, struct C *c);

int foo(int len){ struct A a; a.a1 = 1; struct B b; b.b1 = 2; struct D *d; // "d" set later within f(). struct C *c = malloc(sizeof(struct C)); c->n = len; c->ds = malloc(len*sizeof(struct D)); int rv = f(a,&b,c); if (rv != 0) return rv; d = c->ds[1]; return d.d2; } -----------------Java+JNA version--------------------------------- class A extends Structure { public int a1; } class B extends Structure { public int b1; }

class D extends Structure { public int d1; public int d2; }

class C extends Structure { public int n; public D[] ds; }

public interface L extends Library { L INSTANCE = (L)Native.loadLIbrary("L.dll",L.class) int f(A a, B[] b, C[] c); }

int foo(int len) { A a = new A(); a.a1 = 1;

B[] b = new B[1]; b[0].b1 = 2;

C[] c = new C[1]; c[0].ds = new D[len]; c[0].n = len;

int rv = Lib.INSTANCE.f(a, b, c); if (rv != 0) return rv; D d = c[0].ds[1]; return d.d2; }