简体   繁体   English

JNA结构参考

[英]JNA Structure ByReference

below the method and the unit test for that method. 下方的方法以及该方法的单元测试。 The problem is that I'm not able to return the value of result from the Load method. 问题是我无法从Load方法返回结果的值。 the unit test below fails! 以下单元测试失败!

I thought that by default JNA's object were ByRef by default so I tried instantiating and passing LoadResults "without" .ByReference ... 我认为默认情况下,JNA的对象默认为ByRef,因此我尝试实例化并“无”传递LoadResults。ByReference...

where is my mistake? 我的错误在哪里?

@Test
public void testLoad () {
   MY_Processor proc = new MY_Processor();
   // LoadResults result = new LoadResults ();
   LoadResults.ByReference result = new LoadResults.ByReference();
   ByteByReference [] pathToFile = new ByteByReference[256];
   // fill pathToFile out ... 

   try {
      proc.Load (pathToFile, result);
      assertEquals(0, result.errorCode);
      assertEquals(1, result.elaborationTime);
      assertEquals(2, result.coreItem);
   } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
}



public Integer Load ( ByteByReference[] pathToFile, 
                          LoadResults.ByReference result ) throws Exception {

   // here result is correctly filled out !
   LoadResults result = null;
   result = native.getResult (numCore);
}

added the native code. 添加了本机代码。

UPDATE UPDATE

// header
typedef struct
{
   int     errorCode;
   int     elaborationTime;
   int     coreItem;
} LoadResults;

//[in]  path
//[out] result
int Load (char path[MY_BUFFER_DEFINE], LoadResults* result);
// implementation ...

LoadResults* getResult (int numCore)
{
   // some check ... 

   LoadResults *localResult = new LoadResults();
   // fill out ... 

   return localResult;
}

there is a "free" method exposed by the native code but I didn't show in order to keep the focus on my problem :-) 本机代码公开了一个“免费”方法,但是为了保持对我的问题的关注,我没有显示它:-)

/UPDATE / UPDATE

thanks! 谢谢!

O. O.

The problem is that you're passing a Structure as a parameter, then reassigning that parameter within the function. 问题是您要传递一个Structure作为参数,然后在函数中重新分配该参数。 That will have no effect whatsoever on the argument. 这对论点毫无影响。

The pattern you need to follow is this: 您需要遵循的模式是这样的:

Pointer p = mylib.getResult()
MyStructure m = new MyStructure(p);
// ....
mylib.free(p);

I'd recommend you pass in a native string ( const char* ) as your path rather than a fixed-size buffer of native char . 我建议您传入一个本地字符串( const char* )作为路径,而不是一个固定大小的本地char缓冲区。

UPDATE UPDATE

If you want to copy a result into the argument, then you'll need to copy the structure's contents, eg 如果要将结果复制到参数中,则需要复制结构的内容,例如

public int Load(LoadResults arg) throws Exception {
    // Effectively copy memory from result into arg
    LoadResults result = native.getResult(numCore);
    if (alternative_1) {
        // Copy result's native memory into arg's memory, then sync Java fields
        result.useMemory(arg.getPointer());
        result.write();
        arg.read();
    }
    else {
        // Sync result's native memory with arg's Java fields
        Pointer p = arg.getPointer();
        arg.useMemory(result.getPointer());
        arg.read();
        arg.useMemory(p);
    }
}

Just solved ... 刚解决...

1) I don't need to use LoadResults.ByReference . 1)我不需要使用LoadResults.ByReference

2) the problem was that into the Load method I updated the reference passed in input with another one: 2)问题是在Load方法中,我用另一个输入更新了输入中传递的引用:

public Integer Load ( ByteByReference[] pathToFile, LoadResults result ) throws Exception     
{
   // that's the problem!!!! storing the value into another object with another "address"
   // and not the original "results".
   // result = native.getResult (numCore);

   // solved with this:
   native.getResult (numCore, result);
}

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

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