简体   繁体   English

没有使用JNA获得C ++ Struct

[英]Not getting C++ Struct using JNA

I have googled for a long time, and nothing helps. 我已经搜索了很长时间,没有任何帮助。

EDIT: This is the code after implementing technomage's advice. 编辑:这是实现技术建议后的代码。

Example struct C++ code: 示例struct C ++代码:

typedef struct _SOMESTRUCT{
    const char* String1;
    const char* String2;
} SOMESTRUCT, *LPSOMESTRUCT;

Example C++ function that "fills up" the struct with data: 示例C ++函数用数据“填充”结构:

int GetSomeStruct(_SOMESTRUCT somes);

Translation of struct to java: struct到java的翻译:

public class SomeStruct extends Structure{
    public String String1;
    public String String2;
    public class ByValue extends SomeStruct implements Structure.ByValue{}
    public SomeStruct(Pointer p){ //constructors of struct
        super(p);
        read();
    }
    public SomeStruct(){
         super();
         read();
    }
}

Translation of function to java method: 函数到java方法的翻译:

int GetSomeStruct(SomeStruct.ByValue structref);

How I execute the code from main Java app: 我如何从主要Java应用执行代码:

EnclosingClass.SomeStruct sstruct = enclosingInstance.new SomeStruct();
EnclosingClass.SomeStruc.ByValue sstructval = sstruct.new ByValue();
enclosingInstanceofClasswithTranslatedCfunctions.GetSomeStruct(sstructref);

Assumptions: 假设:

  • I have correctly executed Native.loadLibrary before attempting to use the function. 在尝试使用该函数之前,我已经正确执行了Native.loadLibrary。 (simpler functions that return ints work fine as does a callback function for receiving event notifications) (返回int的简单函数和用于接收事件通知的回调函数一样,都可以正常工作)
  • The java translations are both in separate files (as in the c code) nested in public classes that extend JNA library. Java转换都在嵌套在扩展JNA库的公共类中的单独文件中(如c代码中)。
  • The int that is returned by GetSomeStruct is zero if everything executes well on the c side. 如果一切都在c端执行良好,则GetSomeStruct返回的int为零。 I keep getting zero. 我不断得到零。

Your native declaration passes a struct by value. 您的本机声明按值传递结构。 The following declarations are equivalent: 以下声明是等效的:

int GetSomeStruct(struct _SOMESTRUCT somes);
int GetSomeStruct(SOMESTRUCT somes);

You need to define your Java mapping to explicitly accept an argument which implements the Structure.ByValue interface. 您需要定义Java映射以显式接受一个实现Structure.ByValue接口的参数。 JNA assumes struct* by default, so you need the explicit ByValue usage to indicate you want something different. JNA默认情况下采用struct* ,因此您需要显式的ByValue用法来表明您想要不同的东西。

public class MyStruct extends Structure {
    public class ByValue extends MyStruct implements Structure.ByValue { }
}

SOMESTRUCT is a typedef of struct _SOMESTRUCT , which is the most fundamental way of referring to that struct . SOMESTRUCT是一种typedefstruct _SOMESTRUCT ,这是指的是最根本的途径struct Both are type specifiers, and may be used interchangeably. 两者都是类型说明符,可以互换使用。 _SOMESTRUCT is often called the "tag" for the structure definition it is preceding. _SOMESTRUCT通常在其前面的结构定义中被称为“标签”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM