[Mono-list] C# bindings and FILE* question

Antony Lesuisse al2000@udev.org
Mon, 30 Aug 2004 16:41:20 +0200


Jonathan Pryor <jonpryor@vt.edu> [040830]:
> On Mon, 2004-08-30 at 04:01, ?????????? ?????????? wrote:
> > Is it possible to pass a Stream object to the C code as FILE* pointer?
> 
> No.  They're completely different.  You could P/Invoke into libc for
> fopen(3), fclose(3), etc., and use IntPtr as a FILE*, if necessary.
> 
> > I'd like to wrap some native code that has lots of functions with FILE*-
> > args and I want to pass at least to kind of Stream from the class
> > library to them (the Memory Stream and the File Stream)
> 
> This isn't possible, even if you were in C.  FILE* doesn't support

Actually it is possible if you use the GNU libc (linux and cygwin):

for the generic way see section:

    12.21.3.2 Custom Stream Hook Functions
    http://www.delorie.com/gnu/docs/glibc/libc_232.html

and for simple buffers like MemoryStream:

    12.21.1 String Streams
    http://www.delorie.com/gnu/docs/glibc/libc_228.html

> polymorphism, so there's no way for you to handle the read/write
> requests, so that you could write to a memory buffer instead of a file. 
> That's just the limitations of the standard C library, and there's no
> way to change it (unless you change the standard to support such
> functionality, and wait for the implementations to match the
> standard...).
> 
> This is one of the reasons why C++ introduced iostreams (that, and to
> have type-safe I/O of class objects without needing extra casts).
> 
> Consequently, if you want to read data written to a FILE*, you have to
> use a temporary file first, and then read the file afterward.
> 
>  - Jon

-- 
Antony Lesuisse