3

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply ,yeah C++ function parameter is not 'const char*' its just 'char*' ,so as per your suggestion i tried to pass 'byte[]' and even 'Memory' also but its giving me the same error update: byte[] primitivebytearray=new byte[256]; int i=fooinstance.xxfunction(primitivebytearray); String str=new String(); Memory mem=new Memory(257); mem.setString(0,str); i=fooinstance.xxfunction(mem);
Either your native code is writing more than 256-257 characters to the incoming buffer, or it has issues elsewhere. Either instrument/log your native code execution, or attach a debugger to catch the crash at its source.
No its not writing more than 256 characters,and i tried to pass all other possible data types also into that function but its giving same error,. so what parameter should i send in java to make it compatible with char* in C++?isn't there any way to do it in JNA? And i am unable to attach the logged file as my company do not permit to access any sharing sites
# A fatal error has been detected by the Java Runtime Environment: # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x03d12366, pid=5432, tid=2252 JRE version: 7.0_04-b22 # Java VM: Java HotSpot(TM) Client VM (23.0-b21 mixed mode, sharing windows-x86 ) # Problematic frame: # C [WINUSB.DLL+0x2366] WinUsb_ControlTransfer+0x4a # # Failed to write core dump. Minidumps are not enabled by default on client versions of Windows #An error report file with more information is saved as: # C:\Documents and Settings\mml4kor\jni_dll\JnaSample\hs_err_pid5432.log #
# If you would like to submit a bug report, please visit: # bugreport.sun.com/bugreport/crash.jsp # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug. #
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.