如何获取托管 System.IO.FileStream 的底层 stdio FILE*?
我正在为 C/C++ 库编写一个 .NET 适配器,其中方法“bar”采用常规 stdio FILE*。 是否可以构建一个接口,以便托管代码用户可以传递托管(文件)流? 也就是说,无需创建中间缓冲区和代码来在之间传输数据。 另外,假设 bar() 只读会让事情变得更好吗?
// native code
void bar(FILE*);
// interface for managed code
void foo(System::IO::FileStream^ file)
{
FILE* stdio_handle = ???;
bar(stdio_handle);
}
I'm writing a .NET adaptor for a C/C++ library where a method "bar" takes a regular stdio FILE*. Is it possible to build an interface so that managed code user can pass a managed (File)Stream? That is without creating an intermediary buffer and code to pipe the data between. Also does the assumption that bar() reads only make things any better?
// native code
void bar(FILE*);
// interface for managed code
void foo(System::IO::FileStream^ file)
{
FILE* stdio_handle = ???;
bar(stdio_handle);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
System.IO.FileStream.Handle
它不一定是
stdio
句柄。 这是一个 Windows 句柄。 我不认为FileStream
是基于stdio
构建的,因此没有stdio
句柄。正如 Marc 在 MSDN 链接中指出和提到的那样,您可能需要考虑使用
SafeFileHandle
属性(如果您使用的是 .NET 2.0+)而不是Handle
(现在是被认为已过时)。 不过,旧版本中只有Handle
可用。System.IO.FileStream.Handle
It's not necessarily
stdio
handle. It's a Windows handle. I don't thinkFileStream
is built uponstdio
to have astdio
handle.As Marc pointed out and mentioned in the MSDN link, you might want to consider using
SafeFileHandle
property (if you are on .NET 2.0+) instead ofHandle
(which is now considered obsolete). OnlyHandle
is available in older versions, though.不,无法将流转换为文件描述符 (
FILE
*)。No, it's not possible to convert a stream to a file descriptor (
FILE
*).如果你必须有一个 stdio 句柄,你总是可以首先使用 fopen 打开文件。 这描述了导出c stdlib文件的包装器函数,然后他使用互操作来处理它们。
If you have to have a stdio handle, you could always use fopen to open the file in the first place. This describes a wrapper to export the c stdlib file functions and then he uses interop to work with them.