简体   繁体   中英

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. 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. 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. I have a liberty to modify both ends (C# app and C DLL). C# app is .NET 2.0 based.

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. 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. 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). 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? 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)?

Named pipes in Windows are handled through memory buffers and are compatible with most file APIs. To operate a pipe use "\\\\.\\pipe" namespace, eg name like "\\\\.\\pipe\\foo". To create pipe use CreateNamedPipe Win32 API. Use fopen to open it on C side.

Another alternative could be mailslot created by CreateMailSlot API and operated though "\\\\.\\mailslot" namespace. The main difference is operating on datagrams level rather than streams as in case of pipes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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