Interactions / Communication between C ++ and a Java Program

I have an application written in Java and some native C ++ code with system hooks. These two must communicate with each other. I mean, the C ++ routine should send some data to Java. I would write it all in one language, if that was possible for me. What I'm doing right now is really stupid, but it works. I hide the C ++ program window and send it data to standard output, and then I read this output with standard Java input! Well, I know what JNI is, but I'm looking for something simpler for it (if it exists).

Can someone give me an idea on how to do this?

Any help would be greatly appreciated.

+3
source share
3

JNI 'easy', IPC (Inter process communication). , ++ Java-.

, , IPC, , , IPC.

, , , . "" "", , , 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();

java 'cpp.out' .

+2
+3

:

1) create two processes and use any suitable IPC;

2) compile the C ++ application into a dynamic library and export functions with a standard C-interface that must be called from any language.

+2
source

Source: https://habr.com/ru/post/1782953/


All Articles