JNI - connecting STDOUT to a JTextArea?
I am wondering if开发者_如何转开发 it is possible to connect STDOUT of a C/C++ dll to a JTextArea? The thing is, I am using 3rd party DLL's so I can't manually alter them. Basically I have my Java applet, and then my own DLL which Loads and manages the 3rd party DLL's, and it is the 3rd part DLL's which do stuff like printf(). It shows up in Eclipse console during debugging, but I want it in a JTextArea or something.
EDIT: To clarify, I am using std::cout in my win32 DLL.
Thanks
What is STDOUT? If it's the Posix file handle, there's not much you can do. You'd have to close it, then open a temporary file, write to that, and then read it in the Java part. If you're actually using std::cout for the output, it's possible to (temporarily) change the streambuf of it to use a stringbuf, then pass the generated string to Java.
You could, in your DLL, redirect stdout to a file or pipe. Then you could read from the file or pipe in Java and copy what you read to your JTextArea. Another option would be to move your DLL into its own process, which you would start from Java, then read from the process's stdout and write to your JTextArea.
SetStdHandle (link to MSDN) will allow you to redirect stdout.
精彩评论