java中调用c#代码,从C#调用我自己的Java代码

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值