How do I get the base STDIO FILE * managed by System.IO.FileStream?

I am writing a .NET adapter for a C / C ++ library where the "bar" method takes a standard stdio FILE *. Is it possible to build an interface so that a managed (File) Stream can be passed by a user of managed code? It is not about creating an intermediate buffer and code to exchange data between them. Also does the assumption that bar () is reading only makes things better?

// native code
void bar(FILE*);

// interface for managed code
void foo(System::IO::FileStream^ file)
{
    FILE* stdio_handle = ???;

    bar(stdio_handle);
}

      

0


source to share


3 answers


System.IO.FileStream.Handle

Not necessarily a stdio

descriptor. This is a Windows descriptor. I don't think it is FileStream

built on stdio

to have a handle stdio

.



As Mark pointed out and mentioned in the MSDN link, you may need to use the property SafeFileHandle

(if you are using .NET 2.0+) instead of Handle

(which is now deprecated). However, only Handle

available in older versions.

+5


source


Is it possible to build an interface so that a user of managed code can submit a managed (file) stream?



No, it is not possible to convert a stream to a file descriptor ( FILE

*).

0


source


If you must have a stdio descriptor, you can always use fopen to open the file first. This describes a wrapper for exporting functions of a stdlib file, and then it uses interop to work with them.

0


source







All Articles