C++ 之间的交互/通信和一个Java程序

发布于 2024-10-09 21:04:58 字数 261 浏览 0 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尘曦 2024-10-16 21:04:58

插座和插座Corba 是我想到的两种技术。

另外,请尝试 Google 的 Protocol BuffersApache Thrift.

Sockets & Corba are two techniques that come to mind.

Also, try Google's Protocol Buffers or Apache Thrift.

绻影浮沉 2024-10-16 21:04:58

如果您觉得 JNI 不“简单”,那么您就需要 IPC(进程间通信)机制。因此,您可以从 C++ 进程与 Java 进程进行通信。

您对控制台重定向所做的事情是 IPC 的一种形式,本质上就是 IPC。

由于您发送的内容的性质尚不清楚,因此很难给您一个好的答案。但是,如果您有可以轻松序列化为简单协议的“简单”对象或“命令”,那么您可以使用诸如协议缓冲区之类的通信协议。

#include <iostream>
#include <boost/interprocess/file_mapping.hpp>

// Create an IPC enabled file
const int FileSize = 1000;

std::filebuf fbuf;
fbuf.open("cpp.out", std::ios_base::in | std::ios_base::out 
                          | std::ios_base::trunc | std::ios_base::binary); 
// Set the size
fbuf.pubseekoff(FileSize-1, std::ios_base::beg);
fbuf.sputc(0);

// use boost IPC to use the file as a memory mapped region
namespace ipc = boost::interprocess;
ipc::file_mapping out("cpp.out", ipc::read_write);

// Map the whole file with read-write permissions in this process
ipc::mapped_region region(out, ipc::read_write);

// Get the address of the mapped region
void * addr       = region.get_address();
std::size_t size  = region.get_size();

// Write to the memory 0x01
std::memset(addr, 0x01, size);

out.flush();

现在您的 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.

#include <iostream>
#include <boost/interprocess/file_mapping.hpp>

// Create an IPC enabled file
const int FileSize = 1000;

std::filebuf fbuf;
fbuf.open("cpp.out", std::ios_base::in | std::ios_base::out 
                          | std::ios_base::trunc | std::ios_base::binary); 
// Set the size
fbuf.pubseekoff(FileSize-1, std::ios_base::beg);
fbuf.sputc(0);

// use boost IPC to use the file as a memory mapped region
namespace ipc = boost::interprocess;
ipc::file_mapping out("cpp.out", ipc::read_write);

// Map the whole file with read-write permissions in this process
ipc::mapped_region region(out, ipc::read_write);

// Get the address of the mapped region
void * addr       = region.get_address();
std::size_t size  = region.get_size();

// Write to the memory 0x01
std::memset(addr, 0x01, size);

out.flush();

Now your java file could open 'cpp.out' and read the contents like a normal file.

软糯酥胸 2024-10-16 21:04:58

我想到了两种方法:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文