[Mono-list] Problem redirecting stdout

David Morán david.moran.anton at gmail.com
Sat Aug 15 19:07:01 EDT 2009


Solved, I need to flush stdlib buffer stream, this is a example code:

                int ofd;
                int[] p = new int[2];
                char* b = stackalloc char[256];

                // Duplicating output stdout
                ofd = Syscall.dup(1);
                // Creating a pipe
                Syscall.pipe(p);
                // Overwriting stdout with pipe output
                Syscall.dup2(p[1], 1);

                // Calling external code that prints something
                holamundo();

                // Flushing stdlib buffers
                Stdlib.fflush(Stdlib.stdout);
                // Reading pipe input descriptor
                Syscall.read(p[0], b, 256);
                // Restoring stdout
                Syscall.dup2(ofd, 1);


Thanks everybody


David Morán wrote:
> 
> First of all hello everybody, I'm new in this forum.
> 
> I'm a traditional C developer getting his first steps with mono and I need
> a little bit of help. I need something very simple but I'm starting to
> think is a impossible task with mono and C#, ok, let me explain the
> problem:
> 
> I'm working in a GNU/Linux enviroment and I have a external library
> (developed in C) which has a couple of function that return its values
> using stdout (yes, I know it, it's a crazy thing, but it works like it
> works). I need to parse its output but I don't know what is the method to
> do this... In C I'll make something like this:
> 
> 
> === CUT HERE ===
> pipe(p);
> if(fork() == 0) {
>    void (*f)(void);
>    void *h;
>    dup2(p[1], STDOUT_FILENO);
>    h = dlopen("libwhatever.so", RTLD_LAZY);
>    f = dlsym(h, "f");
>    f();
>    exit(0);
> }
> wait();
> read(p[0], buf, SIZE_BUF);
> === CUT HERE ===
> 
> In other words, I fork the process and then close stdout and send all
> output to a pipe which is read by the parent process, but due to mono
> doesn't have fork I don't know how redirect stdout stream to other file
> stream.
> 
> Any idea?
> 
> Thanks
> 

-- 
View this message in context: http://www.nabble.com/Problem-redirecting-stdout-tp24943420p24988926.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list