Having my own Java code I'm using C# to call some unmanaged code that call (via JNI) the java code. I'm using JNI since I need to ensure:
the ability that the Java code will run over real JVM and not over some .NET VM
the ability to attach to the VM for debugging (IKVM does'nt support it)
I need free solution
The current free solutions are not applicable (e.g. IKVM)
Anyway, my question is how can I manage strings passed between these layers in the best manner without leaks.
I'm doing something like:
[DllImport(@"MyDll.dll")]
public extern static void receive_message(string receDest, StringBuilder response);
This means I'm allocating the memory for the response in the managed code.
I want to avoid that since I don't know in advance the response length. How can I write a JNI appropriate method that will allocate the right buffer for the managed code without leaks. The JNI code should be thread safe.
Any suggestions?
Thanks,
Guy
解决方案
You may need JNI, but your requirements don't really indicate it.
The requirement to use a real JVM does not dictate the use of JNI. I'd suggest sharpening your requirements, or considering looser coupling. For example, socket comms, web services, a shared database, a shared file, or a queue.
If you really need Java and .NET to be run in the same process, with tight coupling, consider JNBridge.
They've solved the problem you are confronting.