开发者

Get a FILE* handle to be passed to a C function for System.IO.File and default streams

开发者 https://www.devze.com 2023-02-19 05:38 出处:网络
I\'m using an old C library (built as a DLL) within my C# application. One of the functions in this library requires a FILE* (as defined开发者_StackOverflow社区 in ANSI C) to be passed. Is there any w

I'm using an old C library (built as a DLL) within my C# application. One of the functions in this library requires a FILE* (as defined开发者_StackOverflow社区 in ANSI C) to be passed. Is there any way I can get a FILE* handle, in C#, for a System.IO.File, stdout and stderr?

Or is there any way to workaround this problem, any idea, hint, etc...?

Thanks in advance.


I haven't tried this, but it might work...

System.IO.File.Create()/Open() will return a FileStream, from there you can use FileStream.SafeFileHandle and from there SafeFileHandle.DangerousGetHandle() will give you the native operating system HANDLE.

Then in unmanaged C, _open_osfhandle() will take your HANDLE and return an int file descripter. Pass this to _fdopen() and it will return a FILE*.

A long route, with plenty of opportunity for incompatibilities and other gotchas. But you might be lucky!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号