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 JavaString
. If there is any possibility of it not beingconst
, you should pass a buffer instead (byte[]
,Memory
, or NIO Buffer), and then useNative.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.