java char 数组 作为形参,JNA:char数组作为函数参数

Using JNA, I am loading a dll written in C++ and calling function present within that C++ function:

int xxfunction(Char* ptr){...}

Here I need to send a char array so that function will assign value to it. Basically I need to pass char array by reference.

According to the JNA documentation, C++ char* equivalent in Java is String, so I created a String object and passed it to the function like shown below:

Java function declaration:

interface foo extends Library

{

....//loading dll and other work

int xxfunction(String chararray);//function declaration

}

Java function call:

public static void main(String args[])

{

String str="abcd";

int i=fooinstance.xxfunction(str);//function call

}

but when I executed this code it is giving me:

A fatal error has been detected by the Java Runtime Environment:

Failed to write core dump. Minidumps are not enabled by default on

client versions of Windows

The crash happened outside of the Java Virtual Machine in native code. See problematic frame for where to report the bug.

So is it the correct way to pass String as an argument where function expects char pointer?

C++ char equivalent in java is byte, so do I need to pass byte array as a parameter?

I can't even pass Pointer object from JNA to function because it gives me IllegalargumentException.

解决方案

Only const char* should ever be mapped to a Java String. If there is any possibility of it not being const, you should pass a buffer instead (byte[], Memory, or NIO Buffer), and then use Native.toString() on the "returned" value.

As a matter of style, you should always provide the callee with the length of the provided buffer so that it has enough information available to avoid overwriting the buffer.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值