使用 JNA 分配字节数组

发布于 2024-10-06 19:04:19 字数 844 浏览 1 评论 0原文

我正在尝试开发一个函数来填充作为实际参数传递的字节数组。我正在遵循 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代码中?

谢谢!

I'm trying to develop a function to fill a byte array passed as an actual argument. I am following the example of the documentation of JNA but it is not working. The documentation says:

// 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 ());

My function in C is:

__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;
}

But when printing the array values are the results:
0 20 48 2

How to implement the function allocate_buffer properly?
Or is the problem is in Java code?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤感在游骋 2024-10-13 19:04:19

已解决:
正确的 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;

}

Resolved:
The correct C function is:

__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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文