Create FILE* from String
I'm using a library where they used paths to define input and output files where data gets read from/written to.
App1
writes the input file, App2
reads that input file and creates an output file. Then, App1
reads the output and everyone is happy.
Now, we get the requirement that for the communication between App1
and App2
, no files shall be created anymore. Therefore, App1
shall pass a buffer to App2
(a simple char*
) along with a pre-allocated output buffer where App2
shall write the results to.
Unfortunately, throughout the library that is used in App2
, a FILE*
is used to read from/write to.
So, here's the question: can I achieve – it without rewriting the library used by App2 – (or with minor changes) that I can use the buffers passed from App1
to App2
for information exchange? IMHO this would require that I could create a FILE*
from a buffer without having that FILE*
on the disk.
I read that for .NET I could use the System.IO.StringReader
class, but si开发者_JAVA百科nce I'm using plain C++ and no .NET, I cannot use that stuff.
Extra Bonus:
The caller (i.e.App1
) is managed code (C# application), the callee (i.e. App2
) is a plain C++ DLL (i.e. App1
calls a function in that DLL).
Edit:
I'd favor answers where the data exchange could happen synchronously in a way that the caller passes buffers to the callee, the callee reads from the input buffer and writes to the output buffer, then the callee terminates and the caller could read the content of the buffers.For IPC you can use pipes, it is easy to make FILE*
from winapi HANDLE
created for pipe.
This code will show how:
HANDLE read_hnd;
HANDLE write_hnd;
//make sa (SECURITY_ATTRIBUTES)
if(!CreatePipe(&read_hnd, &write_hnd, &sa, NULL))
{
//error
}
int fd = _open_osfhandle((intptr_t)read_hnd, _O_RDONLY|_O_TEXT);
FILE *pipe_read_file = _fdopen(fd, "rt");
For IPC you should use named pipes, but idea the same.
精彩评论