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