开发者

How to cast a pointer to a c struct to jna structure

开发者 https://www.devze.com 2023-03-13 09:38 出处:网络
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, w

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.

That is the C struct:

typedef struct
{
   int x;
   int y;
}Point;
Point *gpt;

typedef struct
{
   int x;
   int y;
   Point pt1;
}Point2;
Point2 *gpt2;

That is the callback function in C with a pointer (void *params) to Point2 sctruct:

void __stdcall PointCallback(void *params, int param_size)

So, I've made this code in java to receive the callback and get the original struct:

// Point.java    
package Callback.UsePointLib;
import com.sun.jna.Structure;

public class Poin开发者_运维技巧t 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);
       }
}

Callback implementation:

//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);        **<- I get zero here instead of four**
        System.out.println("pt2.y = "+pt2.y);        **<- I get zero here instead of five**
        System.out.println("pt2.pt1.x = "+pt2.pt1.x);**<- pt1 is null, throwing exception**
        System.out.println("pt2.pt1.y = "+pt2.pt1.y);**<- same as pt1.**
    }   
   }

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.

Please, I'd appreciate any help on that.

Thanks,

Fernando.


EDIT

I've modified the code and it works partially.

 //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();*   **<--Modification**
            System.out.println("pt2.x = "+pt2.x);        **<- I get the correct value (four)**
            System.out.println("pt2.y = "+pt2.y);        **<- I get the correct value (five)**
            System.out.println("pt2.pt1.x = "+pt2.pt1.x);**<- pt1 is still null, throwing exception**
            System.out.println("pt2.pt1.y = "+pt2.pt1.y);**<- same as pt1.**
        }   
       }


The jna docs say that the constructor Structure(Pointer) allocates a structure onto a preallocated memory. It wont automatically assign values for you.I don't think thats what you want.

Change the constructors to

public Point2(){
    super();
}
public Point2(Point1 p){
    super();
    pt1.x = p.x;
    pt1.y = p.y;

    this.x = something;
    this.y = something;
}


Within the context of a callback, JNA will automatically call Structure.read on entry and Structure.write on exit for any parameters of type Structure.

If you declare your callback method signature to use a Structure type of the appropriate subclass ("Point2" in your example), the copying to/from native memory should be automatic.

0

精彩评论

暂无评论...
验证码 换一张
取 消