如何在 Windows 上将 BYTE 数组映射为 FILE *

How to map BYTE array as FILE * on Windows(如何在 Windows 上将 BYTE 数组映射为 FILE *)

本文介绍了如何在 Windows 上将 BYTE 数组映射为 FILE *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了旧的、巨大的开源代码,它对存储在磁盘文件中的二进制数据执行一些计算,输出也保存为二进制文件.

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.

这个 API 迫使我在每次调用 magic() 时进行两次巨大的 I/O.

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

是否有可能在 Windows 上使用 C/C++ 机制将 BYTE 数组映射为 FILE*?

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

推荐答案

看来你需要fmemopen的功能:

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

它需要一个内存区域并返回一个文件描述符.不幸的是,这是一个 POSIX 函数,在 Windows 中没有等效函数.内存映射文件可能不是您想要的,因为它们采用现有文件并将其映射到内存区域,而不是像 fmemopen 那样相反.您唯一的选择是在 Windows 上使用 fmemopen 和 mingw(不知道您是否可以这样做)或推出您自己的 fopen、fwrite 等版本.

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.

这篇关于如何在 Windows 上将 BYTE 数组映射为 FILE *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Windows 上将 BYTE 数组映射为 FILE *

基础教程推荐