C++ 之间的交互/通信和一个Java程序
我有一个用 Java 编写的应用程序和一些带有系统挂钩的本机 C++ 代码。这两个人必须互相沟通。我的意思是 C++ 子程序必须向 Java 子程序发送一些数据。如果可能的话,我会用一种语言写出整件事。我现在做的事情确实很愚蠢,但是有效。我隐藏了 C++ 程序的窗口并将其数据发送到它的标准输出,然后我使用 Java 的标准输入读取该输出! 好的,我知道 JNI 是什么,但我正在寻找更简单的东西(如果存在的话)。
谁能给我任何关于如何做到这一点的想法?
任何帮助将不胜感激。
I've got an app written in Java and some native C++ code with system hooks. These two have to communicate with each other. I mean the C++ subprogram must send some data to the Java one. I would've written the whole thing in one language if it was possible for me. What I'm doing now is really silly, but works. I'm hiding the C++ program's window and sending it's data to it's standard output and then I'm reading that output with Java's standard input!!!
Ok, I know what JNI is but I'm looking for something easier for this (if any exists).
Can anyone give me any idea on how to do this?
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
插座和插座Corba 是我想到的两种技术。
另外,请尝试 Google 的 Protocol Buffers 或 Apache Thrift.
Sockets & Corba are two techniques that come to mind.
Also, try Google's Protocol Buffers or Apache Thrift.
如果您觉得 JNI 不“简单”,那么您就需要 IPC(进程间通信)机制。因此,您可以从 C++ 进程与 Java 进程进行通信。
您对控制台重定向所做的事情是 IPC 的一种形式,本质上就是 IPC。
由于您发送的内容的性质尚不清楚,因此很难给您一个好的答案。但是,如果您有可以轻松序列化为简单协议的“简单”对象或“命令”,那么您可以使用诸如协议缓冲区之类的通信协议。
现在您的 java 文件可以打开“cpp.out”并像普通文件一样读取内容。
If you don't find JNI 'easy' then you are in need of an IPC (Inter process communication) mechanism. So from your C++ process you could communicate with your Java one.
What you are doing with your console redirection is a form of IPC, in essence that what IPC.
Since the nature of what you are sending isn't exactly clear its very hard to give you a good answer. But if you have 'simple' Objects or 'commands' that could be serialised easily into a simple protocol then you could use a communication protocol such as
protocol buffers
.Now your java file could open 'cpp.out' and read the contents like a normal file.
我想到了两种方法:
1)创建两个进程并使用任何合适的 IPC;
2)将C++应用程序编译成动态库并使用标准C接口导出函数,这些函数应该可以从任何语言调用。
Two approaches off the top of my head:
1) create two processes and use any suitable IPC;
2) compile C++ app into dynamic library and export functions with standard C interface, those should be callable from any language.