【问题标题】:Allocation of byte array with JNA使用 JNA 分配字节数组
【发布时间】:2010-12-11 02:07:48
【问题描述】:

我正在尝试开发一个函数来填充作为实际参数传递的字节数组。我正在遵循 JNA 文档的示例,但它不起作用。文档说:

// Original C declaration allocate_buffer <br>
void (char ** bufp, int * lenp);

// Equivalent JNA mapping
void allocate_buffer (PointerByReference bufp, IntByReference lenp);

// Usage
PointerByReference PointerByReference pref = new ();
IntByReference IntByReference iref = new ();
lib.allocate_buffer (pref, iref);
Pref.getValue Pointer p = ();
byte [] buffer = p.getByteArray (0, iref.getValue ());

我在 C 中的函数是:

__declspec (dllexport) void allocate_buffer (char ** bufp, int * lenp)
{
    char array [4];

    array [0] = 0;
    array [2] = 1;
    array [3] = 2;
    array [4] = 3;

    * bufp = array;
    * lenp = 4;
}

但是当打印数组值时,结果是: 0 20 48 2

如何正确实现函数allocate_buffer? 还是问题出在 Java 代码中?

谢谢!

【问题讨论】:

    标签: arrays pointers char jna


    【解决方案1】:

    已解决: 正确的 C 函数是:

    __declspec(dllexport) void allocate_buffer(unsigned char *bufp, int lenp)

    {

        unsigned char *array = (unsigned char *)malloc(4*sizeof(unsigned char));
    
        array[0] = 0;
        array[1] = 1;
        array[2] = 2;
        array[3] = 3;
    
        *bufp = array;
        *lenp = 4;
    

    }

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 2012-05-28
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多