如何将 ac 结构的指针转换为 jna 结构

发布于 2024-11-15 17:26:15 字数 2781 浏览 3 评论 0原文

我需要一些帮助,将 C 结构的指针转换为 jna 结构。我正在使用 jna 从 dll 接收回调函数,该函数有一个参数,它是指向 C 结构的指针,当我尝试将指针转换为 jna 结构时,我得到错误的结构值。

这是 C 结构:

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

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

这是 C 中的回调函数,带有指向 Point2 结构的指针(void *params):

void __stdcall PointCallback(void *params, int param_size)

因此,我在 java 中编写了这段代码来接收回调并获取原始结构:

// 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);
       }
}

回调实现:

//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'我们编写了一个 C 程序来访问 dll 并接收回调,它工作正常,它接收到正确的值。所以,问题是我的java代码。我尝试了很多替代方案但没有成功。

请,我将不胜感激任何帮助。

谢谢,

费尔南多。


编辑

我已经修改了代码并且它可以部分工作。

 //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.**
        }   
       }

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 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);
       }
}

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.**
        }   
       }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2024-11-22 17:26:15

jna 文档说构造函数 Structure(Pointer) 将结构分配到预分配的内存上。它不会自动为您分配值。我认为这不是您想要的。

将构造函数更改为

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

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

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;
}
弱骨蛰伏 2024-11-22 17:26:15

在回调的上下文中,JNA 将在进入时自动调用 Structure.read,在退出时自动调用 Structure.write,以获取任何 Structure 类型的参数。

如果您声明回调方法签名以使用适当子类的结构类型(示例中的“Point2”),则应自动复制到本机内存或从本机内存复制。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文