Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cast a pointer to a c struct to jna structure
    text
    copied!<p>I would like some help in casting a pointer to a C struct to a jna strucuture. I am using jna to receive a callback function from a dll, the function has a parameter that is a pointer to a C struct, when a I try to cast the pointer to a jna structure I get wrong structure values. </p> <p>That is the C struct:</p> <pre><code>typedef struct { int x; int y; }Point; Point *gpt; typedef struct { int x; int y; Point pt1; }Point2; Point2 *gpt2; </code></pre> <p>That is the callback function in C with a pointer (void *params) to Point2 sctruct:</p> <pre><code>void __stdcall PointCallback(void *params, int param_size) </code></pre> <p>So, I've made this code in java to receive the callback and get the original struct:</p> <pre><code>// Point.java package Callback.UsePointLib; import com.sun.jna.Structure; public class Point extends Structure { public static class ByValue extends Point implements Structure.ByValue {} public int x; public int y; } //Point2.java package Callback.UsePointLib; import com.sun.jna.Pointer; import com.sun.jna.Structure; public class Point2 extends Structure { public int x; public int y; Point pt1; public Point2(Pointer p){ super(p); } } </code></pre> <p>Callback implementation:</p> <pre><code>//UsePointLib.java public interface IFuncCallback extends StdCallCallback{ void callback(Pointer Params, int ParamSize); } public class FuncCallback implements IFuncCallback{ @Override public void callback(Pointer Params, int ParamSize) { Point2 pt2; // = new Point2(); pt2 = new Point2(Params); System.out.println("pt2.x = "+pt2.x); **&lt;- I get zero here instead of four** System.out.println("pt2.y = "+pt2.y); **&lt;- I get zero here instead of five** System.out.println("pt2.pt1.x = "+pt2.pt1.x);**&lt;- pt1 is null, throwing exception** System.out.println("pt2.pt1.y = "+pt2.pt1.y);**&lt;- same as pt1.** } } </code></pre> <p>I've made a C program to access the dll and receive the callback and it works ok, it receives the correct values. So, the problem is my java code. I've tried many alternatives with no success. </p> <p>Please, I'd appreciate any help on that.</p> <p>Thanks,</p> <p>Fernando. </p> <hr> <p><strong>EDIT</strong></p> <p>I've modified the code and it works partially.</p> <pre><code> //UsePointLib.java public interface IFuncCallback extends StdCallCallback{ void callback(Pointer Params, int ParamSize); } public class FuncCallback implements IFuncCallback{ @Override public void callback(Pointer Params, int ParamSize) { Point2 pt2; // = new Point2(); pt2 = new Point2(Params); *pt2.read();* **&lt;--Modification** System.out.println("pt2.x = "+pt2.x); **&lt;- I get the correct value (four)** System.out.println("pt2.y = "+pt2.y); **&lt;- I get the correct value (five)** System.out.println("pt2.pt1.x = "+pt2.pt1.x);**&lt;- pt1 is still null, throwing exception** System.out.println("pt2.pt1.y = "+pt2.pt1.y);**&lt;- same as pt1.** } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload