0

Is it possible to access a pointer created in c++ in java? Like if I make a string, and make a pointer for the variable (giving the variable a memory place) in c++ is there some command in java that would let me take that pointer and view it? or would I have to output the string to a file, and then preform java file I/O.

JAW1025
  • 676
  • 2
  • 8
  • 17

2 Answers2

1

You would have to convert it into something java understands through JNI--JNI will have a method to convert your pointer to a string, but then Java will copy the memory and will create a regular Java string out of it--changing your memory after giving it to java will not change the Java string.

I don't think even JNI allows communications through direct memory access but I could be wrong, I haven't looked at it lately.

Bill K
  • 62,186
  • 18
  • 105
  • 157
0

You should be able to use JNI: http://java.sun.com/docs/books/jni/ You could also try using SWIG: http://www.swig.org/

But before you dive in you should evaluate if you really need to do that. Are you just trying to share data? You could use networking to do that. Pass a tcp message between two programs. Many options exist for sharing data.

anio
  • 8,903
  • 7
  • 35
  • 53
  • Adding network communications solely to share data between Java and C++ code can be unnecessarily complex. – Andy Thomas Nov 03 '11 at 20:13
  • @AndyThomas-Cramer I don't know exactly what the poster is trying to accomplish. I am just encouraging him to explore his options to find the best solution. – anio Nov 03 '11 at 20:14
  • Yes I'm only trying to exchange data between programs, but have only done this in c++ through file I/O. Should I just do that? have the data outputted, then just have it read in java? – JAW1025 Nov 04 '11 at 00:11
  • If all you want is to get the output of one program into another, then using file I/O is okay although clumsy. A little more advanced technique would be to read the data while its being written, see: http://stackoverflow.com/questions/5886202/java-read-a-logfile-live – anio Nov 04 '11 at 00:33