C DLL working on FILE * called from C # - stream transfer to and from

I have a problem calling a C DLL function from my C # code. The C DLL has a function that takes a file name ( const char * ), then opens the file, does some work on the content (reads through FILE * ) and writes the result to another file. I would like to optimize it, so the read / write operations on the disk are not performed, since I have a file that will be processed already in the memory stream in my C # application. I have the right to change both ends (C # application and C DLL). The C # application is based on .NET 2.0.

I was thinking of expanding the DLL so that it has a function that accepts and pops out an array of bytes so that I can easily PInvoke it. The output seems easy on the C side - instead of writing to FILE * , I could just store the sequential bytes into an array, but the input seems complicated. I don’t know how to work on the C side with the received byte array, to make it in the memory stream and work with it instead in the physical file stream from this point (I do not want to rewrite the entire DLL to read from the byte array instead of FILE * - I I want most of the internal components of the DLL to remain unchanged, just to wrap it up and configure the inputs / outputs). But actually I don’t know, maybe there is a better idea for this.

So the question is : how to turn an array of bytes in C into (FILE *) without writing it to disk and opening FILE * to read this file? Alternatively : how to transfer a memory stream from C # to PInvoke in a C DLL so that it can be easily recognized on the C side and works with FILE * (again, without a physical disk it writes / reads, only in memory)?

+4
source share
1 answer

Windows named pipes are processed through memory buffers and are compatible with most API files. To use the channel, use the \\. \ Pipe namespace. name is "\\. \ pipe \ foo". To create a channel, use the CreateNamedPipe Win32 API. Use fopen to open it on side C.

Another alternative would be a mailbox created using the CreateMailSlot API and working with the namespace "\\. \ Mailslot". The main difference is the operation at the datagram level, and not at the flows, as is the case with pipes.

+1
source

All Articles