我正忙于一个项目,在这个项目中,我必须对专有的C库进行本机调用。我遇到了JNA,它似乎经过了许多成功项目的尝试和测试。
我在将结构(或指向的指针)传递给回调函数时遇到问题。我以前尝试过很多不同的场景,基本上,任何需要内存分配的结构成员,比如String(char*),在我检索它时都是null
我试图用下面的例子来说明这个问题:
C代码:
typedef struct {
int number;
char *string;
} TEST_STRUCT;
typedef union {
int number;
TEST_STRUCT test_struct;
} TEST_UNION;
typedef void (*TEST_CB)(TEST_UNION*);
void test(TEST_CB test_cb)
{
TEST_STRUCT *test_struct = malloc(sizeof *test_struct);
test_struct->number = 5;
test_struct->string = "Hello";
TEST_UNION *test_union = malloc(sizeof *test_union);
test_union->number = 10;
test_union->test_struct = *test_struct;
test_cb(test_union);
free(test_struct);
free(test_union);
}
Java代码:
public interface TestLib extends Library
{
class TestStruct extends Structure
{
public int number;
public String string;
public TestStruct() {
super();
}
protected List<? > getFieldOrder() {
return Arrays.asList("number", "string");
}
public TestStruct(int number, String string) {
super();
this.number = number;
this.string = string;
}
public TestStruct(Pointer peer) {
super(peer);
}
public static class ByReference extends MBTStatus implements Structure.ByReference {}
public static class ByValue extends MBTStatus implements Structure.ByValue {}
}
class TestUnion extends Union {
public int number;
public TestStruct testStruct;
public TestUnion() {
super();
}
public TestUnion(int number, TestStruct testStruct) {
super();
this.number = number;
this.testStruct = testStruct;
}
public TestUnion(Pointer pointer) {
super(pointer);
}
public static class ByReference extends TestUnion implements com.sun.jna.Structure.ByReference {}
public static class ByValue extends TestUnion implements com.sun.jna.Structure.ByValue {}
}
interface TestCallback extends Callback
{
public void callback(TestUnion testUnion);
}
void test(TestCallback testCallback);
}
主要的Java类:
public class TestMain
{
static
{
System.loadLibrary("test");
}
public static void main (String [] args)
{
TestLib.INSTANCE.test(
new TestLib.TestCallback()
{
public void callback(TestLib.TestUnion testUnion)
{
System.out.println(testUnion.testStruct.string == null ? "The string value is null" : "The string value is: " + testUnion.testStruct.string);
}
}
);
}
}
字符串值为空:
The string value is null
当谈到JNA时,我是一个完全的傻瓜,所以我有很多东西要学。我不确定结构的映射是否正确,这可能是null值的原因。
任何帮助都将不胜感激!
编辑:我让这个问题更有意思:
因此,回调函数的参数是一个并集,而不是一个结构。该结构现在是联合的一部分。当我这样做时,结构字符串变量的值似乎也是null。
我自己刚刚找到了更新问题的答案。这个例子ultimatley展示了如何做到这一点。由于联合只占用其最大成员的内存,因此其类型必须设置为该成员。然后必须调用Union.read()函数来读取"选定"变量。具体操作如下:
testUnion.setType(TestLib.TestStruct.class);
testUnion.read();
然后可以访问testStruct变量。正确的回调函数是:
public void callback(TestLib.TestUnion testUnion)
{
testUnion.setType(TestLib.TestStruct.class);
testUnion.read();
System.out.println(testUnion.testStruct.string == null ? "The string value is null" : "The string value is: " + testUnion.testStruct.string);
}
当您实现Union
的基于Pointer
的构造函数时,在调用super
后调用read并重写read()
,使其始终执行正确的操作(例如),这可能会很有用
class MyStructure1 {
public int type;
public int intField;
}
class MyStructure2 {
public int type;
public float floatField;
}
class MyUnion extends Union {
public int type;
public MyStructure1 s1;
public MyStructure2 s2;
public MyUnion(Pointer p) {
super(p);
read();
}
protected void read() {
int type = getPointer().getInt(0);
switch(type) {
case 0: setType(MyStruct1); break;
case 1: setType(MyStruct2); break;
}
super.read();
}
}
如果没有设置联合的类型,JNA通常会尝试自动填充尽可能多的数据,避免任何指针字段(如字符串),如果它们包含无效数据,可能会导致内存故障。