1

I used JNA to call some C++ methods from Java. Everything works fine. This methods contain "cout" to print some things. My question is if it is possible to redirect these prints from C++ methods to a JFrame in Java. In Java methods it is quite easy but I don't know if it possible for methods called through JNA. Ok I know it is a quite strange question but I would like to know if it is possible. Thanks in advance.

Spyros
  • 682
  • 7
  • 18
  • 37
  • 1
    You'll need to redirect the `cout` stream to where Java can read it. You might use a circular buffer with producer/consumer pointers, a callback to Java with the most recent data, or any other method common to inter-process communication. It'll *probably* be easier to redirect the stream in native code to test, but there's no reason you couldn't translate that native code into JNA calls. – technomage Apr 03 '14 at 11:09
  • 1
    See http://stackoverflow.com/questions/584868/rerouting-stdin-and-stdout-from-c for one method of redirecting `stdout`. If you dump to a file, you can have Java read from the file. JNA is perfectly capable of calling `freopen`. – technomage Apr 03 '14 at 11:11
  • 1
    You may need to play around to find the actual value of `stdout`; look at your system's `` file for its definition. On OSX, it's a global symbol called `__stdout` of type `FILE*`. On other systems it's the address of an element within a global array of `FILE` structs. – technomage Apr 03 '14 at 11:25
  • Ok I see. Actually I was thinking the same you mention in comment no2. For me at least the thing when you use JNA is to have something common between C/C++ and Java. So Files are probably the most easy way to implement this. By the way thanks a lot for your suggestions and if you want post one as an answer and I will accept it. :) – Spyros Apr 10 '14 at 08:54

1 Answers1

1

You'll need to redirect the cout stream to where Java can read it. You might use a circular buffer with producer/consumer pointers, a callback to Java with the most recent data, or any other method common to inter-process communication. It'll probably be easier to redirect the stream in native code to test, but there's no reason you couldn't translate that native code into JNA calls.

See Rerouting stdin and stdout from C for one method of redirecting stdout. If you dump to a file, you can have Java read from the file. JNA is perfectly capable of calling freemen.

You may need to look around to find the actual value of stdout; look at your system's <stdio.h> file for its definition. On OSX, it's a global symbol called __stdout of type FILE*. On other systems it's the address of an element within a global array of FILE structs.

Community
  • 1
  • 1
technomage
  • 9,861
  • 2
  • 26
  • 40
  • Did anyone try this method, I used the `freopen` with JNA redirect it to a file, to get the stdout, I find `__acrt_iob_func` is used in windows and that's captured by JNA as well. However it doesn't work, printf still print everything to the screen. – worldterminator Dec 01 '21 at 06:35