Getting stdout using ctypes.

M

Mathias Lorente

Hello all.

I have a simple application (C++) that relies on shared libraries. It
works fine in console mode.
Lot of job is done into the shared library, so there is some calls to
'std::cout' to inform the user in it.

Now, I would like to wrap everything into a GUI, remove the application
and call directly everything from Python using ctypes. (I still need the
console application to launch it manually if needed).
I've made a simple library to test ctypes and everything works fine
except that I don't know how to get stout in order to redirect it
somewhere (dialog box or so).

I've looked for some help into the mailing list history and found
nothing useful (until now).
Do someone has any suggestion?

Mathias
 
C

Christian Heimes

Grant said:
If he's running Unix, he needs to replace the stdout file
descriptor (fd 1), with something that's connected to the
"write" end of a pipe. Then he needs to read the other
("read") end of the pipe in his application.

You do that using the dup2() system call:

It's dangerous to mix streams and descriptors. Traditionally freopen()
is used to redirect a standard stream to a file:

http://www.gnu.org/software/libc/manual/html_mono/libc.html#Standard-Streams

— Function: FILE * freopen (const char *filename, const char *opentype,
FILE *stream)

This function is like a combination of fclose and fopen. It first
closes the stream referred to by stream, ignoring any errors that are
detected in the process. (Because errors are ignored, you should not use
freopen on an output stream if you have actually done any output using
the stream.) Then the file named by filename is opened with mode
opentype as for fopen, and associated with the same stream object stream.

If the operation fails, a null pointer is returned; otherwise,
freopen returns stream.

freopen has traditionally been used to connect a standard stream
such as stdin with a file of your own choice. This is useful in programs
in which use of a standard stream for certain purposes is hard-coded. In
the GNU C library, you can simply close the standard streams and open
new ones with fopen. But other systems lack this ability, so using
freopen is more portable.
 
C

Christian Heimes

Grant said:
If he's running Unix, he needs to replace the stdout file
descriptor (fd 1), with something that's connected to the
"write" end of a pipe. Then he needs to read the other
("read") end of the pipe in his application.

You do that using the dup2() system call:

It's dangerous to mix streams and descriptors. Traditionally freopen()
is used to redirect a standard stream to a file:

http://www.gnu.org/software/libc/manual/html_mono/libc.html#Standard-Streams

— Function: FILE * freopen (const char *filename, const char *opentype,
FILE *stream)

This function is like a combination of fclose and fopen. It first
closes the stream referred to by stream, ignoring any errors that are
detected in the process. (Because errors are ignored, you should not use
freopen on an output stream if you have actually done any output using
the stream.) Then the file named by filename is opened with mode
opentype as for fopen, and associated with the same stream object stream.

If the operation fails, a null pointer is returned; otherwise,
freopen returns stream.

freopen has traditionally been used to connect a standard stream
such as stdin with a file of your own choice. This is useful in programs
in which use of a standard stream for certain purposes is hard-coded. In
the GNU C library, you can simply close the standard streams and open
new ones with fopen. But other systems lack this ability, so using
freopen is more portable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top