[英]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: 假设:
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
是一种typedef
的struct _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.