简体   繁体   English

从C#调用FILE *的C DLL - 传入和传出流

[英]C DLL working on FILE * called from C# - passing a stream in and out

I have an issue with calling a C DLL function from my C# code. 我有一个从我的C#代码调用C DLL函数的问题。 The C DLL has a function accepting a file name ( const char * ), then opens the file, performs some work on the content (reads through the FILE * ) and writes the outcome to another file. C DLL具有接受文件名( const char * )的const char * ,然后打开文件,对内容执行一些工作(通过FILE *读取)并将结果写入另一个文件。 I'd like to optimize it, so that the disk read/write operations are not performed, since I have the file to be processed already in a memory stream in my C# app. 我想优化它,以便不执行磁盘读/写操作,因为我已经在C#应用程序的内存流中处理了文件。 I have a liberty to modify both ends (C# app and C DLL). 我有自由修改两端(C#app和C DLL)。 C# app is .NET 2.0 based. C#app基于.NET 2.0。

I thought of extending the DLL so that it has a function that accepts and spits out a byte array, so that I could easily PInvoke it. 我想扩展DLL,以便它有一个接受并吐出一个字节数组的函数,这样我就可以轻松地对它进行PInvoke The output part seems easy on the C side - instead from writing to a FILE * I could just save the consecutive bytes to an array - but the input seems hard. 输出部分在C端看起来很容易 - 而不是写入FILE *我可以将连续的字节保存到数组 - 但输入似乎很难。 I don't know how to deal on the C side with the obtained byte array to make it into a memory stream and work on that instead on the physical file stream from this point (I don't want to rewrite the whole DLL to read from byte array instead of FILE * - I want most of the DLL internals to remain unchanged, just to wrap it and tweak ins/outs). 我不知道如何处理C端与获得的字节数组,使其成为一个内存流,而是从这一点开始在物理文件流上工作(我不想重写整个DLL来读取从字节数组而不是FILE * - 我希望大多数DLL内部保持不变,只是为了包装和调整输入/输出)。 But actually I don't know, maybe there is a better idea to do that. 但实际上我不知道,也许有更好的主意去做。

So the question is : how to turn a byte array in C into a (FILE *) without writing it actually to disk and opening a FILE * to read this file? 所以问题是 :如何将C中的字节数组转换为(FILE *)而不将其实际写入磁盘并打开FILE *来读取此文件? Alternatively : how to pass a memory stream from C# through PInvoke to a C DLL so that it could be easily recognized on the C side and worked with as a FILE * (again, without physical disk writes/reads, in-memory only)? 或者 :如何将内存流从C#通过PInvoke传递到C DLL,以便它可以在C端轻松识别并作为FILE * (同样,没有物理磁盘写入/读取,仅在内存中)?

Named pipes in Windows are handled through memory buffers and are compatible with most file APIs. Windows中的命名管道通过内存缓冲区处理,并与大多数文件API兼容。 To operate a pipe use "\\\\.\\pipe" namespace, eg name like "\\\\.\\pipe\\foo". 要操作管道,请使用“\\\\。\\ pipe”命名空间,例如名称“\\\\。\\ pipe \\ foo”。 To create pipe use CreateNamedPipe Win32 API. 要创建管道,请使用CreateNamedPipe Win32 API。 Use fopen to open it on C side. 使用fopen在C端打开它。

Another alternative could be mailslot created by CreateMailSlot API and operated though "\\\\.\\mailslot" namespace. 另一种替代方案可以是由CreateMailSlot API创建的邮件槽,并通过“\\\\。\\ mailslot”命名空间进行操作。 The main difference is operating on datagrams level rather than streams as in case of pipes. 主要区别在于数据报级别而不是流式管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM