CreateFileMapping 以“磁盘空间不足"结尾错误 [英] CreateFileMapping ends with "Not enough disk space" error

查看:50
本文介绍了CreateFileMapping 以“磁盘空间不足"结尾错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

然后我打电话:

CreateFileMapping(fileHandle,空值,PAGE_READWRITE |SEC_RESERVE,文件大小 + 1,文件大小 + 1,空值);

我怀疑问题在于将 fileSize + 1 作为 dwFileOffsetHighdwFileOffsetLow 传递,但我很难理解应该怎么做我改为传递给它.

非常感谢任何提示!

解决方案

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

我认为您的误解是认为上限和下限是上限和下限.

在您的情况下,您的值(假设 fileSize 约为 1KB)远不及需要 64 位,因此您应该为 dwFileOffsetLow 传递 fileSize+1> 和 0 表示 dwFileOffsetHigh.

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

来自文档:><块引用>

dwMaximumSizeHigh [in]

最大尺寸的高位 DWORD文件映射对象.

dwMaximumSizeLow [in]

最大尺寸的低位 DWORD文件映射对象.

如果这个参数和dwMaximumSizeHigh都为0(零),则最大尺寸文件映射对象的大小等于文件的当前大小hFile 标识.

尝试映射长度为 0(零)的文件失败并显示错误ERROR_FILE_INVALID 的代码.应用程序应该测试具有长度为 0(零)并拒绝这些文件.

I have a problem understanding how to use the API function CreateFileMapping(...).

I've been trying to map a small file and then reverse it's content. It has only about 1 Kb, but I keep getting the "Not enough memory" error.

I have opened the file by calling CreateFile and got it's size with GetFileSize.

Then I call:

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

I suspect that the problem is with passing the fileSize + 1 as dwFileOffsetHighand dwFileOffsetLow, but I have a hard time understanding what should I pass to it instead.

Any hints are greatly appreciated!

解决方案

dwFileOffsetHigh and dwFileOffsetLow are two 32 bit values that are combined to form a single 64 bit value. This function was implemented this way because it pre-dates widespread compiler support for 64 bit values.

I think your misunderstanding is in believing that the high and low mean upper and lower limits.

In your case your value (assuming fileSize is around 1KB) is nowhere near requiring 64 bits so you should pass fileSize+1 for dwFileOffsetLow and 0 for dwFileOffsetHigh.

However, if you are attempting to map the entire file you can simply pass 0 for both parameters.

From the 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 以“磁盘空间不足"结尾错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆