Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

c++ - How to map BYTE array as FILE * on Windows

I found old, huge open source code which performs some computations on binary data stored in file on disk, output is also saved as binary file.

There is one root method which I would like to use, simplified signature:

int magic(FILE* input, FILE* output);

The problem is that I store input data in process memory and I would like to have output also as memory in process. Code is so big that I'm not able to rewrite it in resonable time.

This API forces me to make two huge I/O on every call to magic().

Is there any possibility to map BYTE array as FILE* on Windows using C/C++ mechanisms?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems that you need the functionality of fmemopen:

http://man7.org/linux/man-pages/man3/fmemopen.3.html

Which takes a memory region and returns a file descriptor. Unfortunately, this is a POSIX function that does not have an equivalent in Windows. Memory mapped files are probably not what you want, as they take an existing file and map it to a memory region, not the other way around as with fmemopen. The only options you have are to either use fmemopen with mingw on Windows (don't know if you can do this) or roll out your own versions of fopen, fwrite and so on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...