CreateFileMapping 以 "Not enough disk space"错误结束

标签 c windows api mapping

我在理解如何使用 API 函数 CreateFileMapping(...) 时遇到问题。

我一直在尝试映射一个小文件,然后反转它的内容。它只有大约 1 Kb,但我一直收到“内存不足”错误。

我已通过调用 CreateFile 打开了文件,并使用 GetFileSize 获取了它的大小。

然后我调用:

CreateFileMapping(fileHandle, 
                  NULL, 
                  PAGE_READWRITE | SEC_RESERVE, 
                  fileSize + 1, 
                  fileSize + 1, 
                  NULL);

我怀疑问题是将 fileSize + 1 作为 dwFileOffsetHighdwFileOffsetLow 传递,但我很难理解应该做什么我改为传递给它。

非常感谢任何提示!

最佳答案

dwFileOffsetHighdwFileOffsetLow 是两个 32 位值,它们组合形成一个 64 位值。此函数以这种方式实现,因为它早于编译器对 64 位值的广泛支持。

我觉得你的误区是认为high和low就是上下限。

在您的情况下,您的值(假设 fileSize 约为 1KB)远不及需要 64 位,因此您应该为 dwFileOffsetLow 通过 fileSize+1 > 和 0 用于 dwFileOffsetHigh

但是,如果您尝试映射整个文件,您可以简单地为两个参数传递 0

来自documentation :

dwMaximumSizeHigh [in]

The high-order DWORD of the maximum size of the file mapping object.

dwMaximumSizeLow [in]

The low-order DWORD of the maximum size of the file mapping object.

If this parameter and dwMaximumSizeHigh are 0 (zero), the maximum size of the file mapping object is equal to the current size of the file that hFile identifies.

An attempt to map a file with a length of 0 (zero) fails with an error code of ERROR_FILE_INVALID. Applications should test for files with a length of 0 (zero) and reject those files.

关于CreateFileMapping 以 "Not enough disk space"错误结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7799914/

相关文章:

c - 我该如何禁用关闭按钮?

c - 如何在 C 中通过结构变量对数组**进行排序?

windows - VBScript 中对象的破坏顺序是什么?

c - 使用 c 访问 %appdata%

java - 简单 Java API - 错误 : Unrecognized field, 未标记为可忽略

c++ - 如何使用 GRUB 查询 BIOS?

c - 指针和排序

windows - 用空文件复制目录文件结构

c++ - 如何检查 win32 exe 文件是否已经在运行? (使用 win32 API 代码)

api - 如何使用 API 调用编辑组合框下拉项