Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top