如何在Java程序和C ++程序之间交换数据 [英] How to interchange data between Java Program and C++ Program

查看:112
本文介绍了如何在Java程序和C ++程序之间交换数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用netbeans IDE构建了一个java程序,它能够连接各自的DLL;我想通过DLL连接和交换我的java程序(.jar)和VSC ++程序(.exe)之间的数据.JNI使用单个Dll以本机方式调用C / C ++函数,为了提高效率,我尝试连接和使用该DLL(JNI实现)在Java程序和C ++程序之间交换原始数据类型。



不幸的是,C ++程序无法获取Java程序已更改的数据值。示例 - 如果我在DLL中声明一个全局int变量,java程序可以捕获该int变量并可以更新它但是如果我运行我的C ++程序(exe)同时加载相同的DLL它无法接收在该声明中声明的那个int变量的更新值DLL。



因此,我需要一个解决方案,使用JNI在JAVA和C ++程序之间共享/交换至少原始数据及其各自的值(尽管日期转移通过套接字).JAVA到C / C ++和C / C ++到JAVA使用DLL。



请帮助我!谢谢

I constructed a java program using netbeans IDE that has ability to connect with a respective DLL; I want to connect and interchange data between my java program(.jar) and VSC++ program(.exe) through DLLs.JNI uses single Dll to invoke C/C++ function in native manner,In order to increase the efficiency I tried to connect and interchange primitive data types between Java program and C++ program using that DLL(JNI implemented).

Unfortunately C++ program cannot obtain data values that has been changed by Java Program.For example - If I declare a global int variable in DLL,java program can catch that int variable and can update it but If I run my C++ program(exe) loading same DLL simultaneously it cannot receive the updated value of that int variable declared in the DLL.

Therefore I need a solution to share/Interchange at least primitive data and their respective values between a JAVA and C++ Program using JNI (in spite of date transferring through Sockets).JAVA TO C/C++ & C/C++ to JAVA using DLLs.

PLEASE HELP ME!! THANK YOU

推荐答案

什么是同时加载相同的DLL?第一:如果你启动一个加载DLL的VC ++程序并启动一个加载相同DLL的Java程序,那么你创建了两个彼此无关的进程,所以你假设你需要某种类型的IPC来进行通信这些过程是正确的。套接字是一种非常好的通信,我大部分时间都在使用它。根据当前场景,您可能需要或可能不需要java程序中的jni和本机代码,但根据您提供的信息,我们无法帮助您。您在建筑中放置IPC(插座)通信的切口也可以作为辩论的基础,但同样,我们没有这方面的信息。



编辑:重新考虑一下:如果你只想分享一小段数据(比如int或几个字节/整数),那么你也可以通过使用共享内存+进程间互斥和一个或两个进程间事件来做到这一点如果您想要向一个或两个方向发送有关数据更改的信号。但是在这种情况下,你的java程序中需要一个小的jni dll。另一件事:jni通话可能非常昂贵。出于这个原因,我通常不会在任何一个方向(java-> c或c->; java)进行jni调用,只是为了读取/设置变量,如果我不需要。如果设置/读取变量不是那么性能关键,并且套接字通信是令人满意的,那么我会使用套接字。这个解决方案有两个优点:你在java程序中不需要jni(它有另一个间接的好处:它仍然是独立于平台的),你可以轻松地在不同的机器上运行程序,有时候会有一些好处!



如果您使用共享内存,那么您有两种选择:



1.您只为java编写一个jni dll程序。你不需要在C ++ exe中使用dll,尤其是不同的dll。当然,您仍然可以在jni dll和c ++程序中重用相同的C / C ++代码。必须以编程方式创建共享内存以及用于同步的mutext以及可选的一个或两个进程间事件。



2.一旦我通过标记创建了共享内存我(鼠标钩子)dll中的一个段(.bss)作为共享。这样,如果将此dll加载到单独的进程中,则所有这些加载的dll实例都会将该段视为共享。将段标记为共享只需要一些链接器魔法,并且知道段的名称可能会明确地将一些变量放到显式定义的段中(使用某些特定于平台的C ++编译器编译指示)。如果您对我之前描述的内容一无所知,我不推荐这种方法。
What is "loading same DLL simultaneously"? First: If you start a VC++ program that loads a DLL and you start a Java program that loads the same DLL then you have created two processes that have nothing to do with each other so your assumption that you require some kind of IPC to communicate between these processes is correct. Sockets is a very good communication, I'm using that most of the time. Depending on the current scenario you may or may not require jni and native code in your java program but we can not help you in that based on the information you provided. Also the cut in your architecture where you put the IPC (socket) communication could be a nice base for debate but again, we don't have the info for this.

Thinking it over again: If you want to share just a small piece of data (like int or a few bytes/ints) then you can also do that by using shared memory + an interprocess mutext and maybe one or two interprocess events if you want to signal about data change to one or both directions. In this case however you will need a small jni dll in your java program. Another thing: A jni call can be quite expensive. For this reason I usually don't make a jni call in either direction (java->c or c->java) just to read/set variables if I don't have to. If setting/reading the variable isn't so performance critical and a socket communication is satisfactory then I would go with sockets. This solution has 2 advantages: You don't need jni in the java program (that has another indirect benefit: it remains platform independent), and you can run the programs on separate machines easily that sometimes has some benefit!

If you use shared memory then you have two choices:

1. You write just a single jni dll for the java program. You don't need a dll in the C++ exe, especially not the same dll. Of course you can still reuse the same C/C++ code in the jni dll and the c++ program. The shared memory must be created programmatically along with the mutext for synchronization and optionally one or two inter-process events.

2. Once I've created shared memory by marking a segment (.bss) in my (mouse hook) dll as shared. This way if you load this dll into separate processes all of these loaded dll instances see that segment as shared. Marking the segment as shared requires only some linker magic and knowing the name of the segment maybe explicitly putting some variables to explicitly defined segments (with some platform specific C++ compiler pragmas). I don't recommend this approach if you don't understand anything in what I've described previously.


如果你非常小心地处理它,JNI接口工作正常。



一切都应该按价值转移,然后在本地副本上工作。不要使用Java中的C ++指针,反之亦然,或者一些奇怪的异常会伤害到你:mad:



这看起来很有趣:



JNI应用示例 [ ^ ]



使用Netbeans和Visual Studio调试JNI应用程序 [ ^ ]
The JNI interface works fine if you handle it very carefully.

Everything should be transfered by value and then work on a local copy. Do dont work with pointer from C++ in Java and vice versa or some strange exceptions will hurt you :mad:

That looks interesting for you:

Sample JNI Application[^]

Debugging a JNI Application using Netbeans and Visual Studio[^]


考虑使用通知模型。被调用者更改值,被调用者正在执行某些操作。喜欢通知其他模块。



事件驱动模型具有良好的性能和稳定性。
Consider using a notification model. The callee changes the value and the called is performing some actions. like notifying other modules.

That event driven model has a good performance and stability.


这篇关于如何在Java程序和C ++程序之间交换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆