How can I extend the length of a memory-mapped file?

余生长醉 提交于 2019-12-07 09:01:06

问题


In Delphi 7, I open a file with CreateFileMapping then get a pointer by using MapViewOfFile.

How can I expand the memory and add some characters to the memory and have it saved to that file?

I have already opened the file with appropriate modes (fmOpenReadWrite, PAGE_READWRITE), and if I overwrite the characters, it gets saved to the file, but I need to add extra values in the middle of the file.


回答1:


If the file mapping is backed by an actual file and not a block of memory, then you can resize the file in one of two ways:

1) call CreateFileMapping() with a size that exceeds the current file size. The file will be resized to match the new mapping.

2) use SetFilePointer() and SetEndOfFile() to resize the file directly, then call CreateFileMapping() with the new size.

Both conditions are described in the documentation for CreateFileMapping().




回答2:


You cannot resize file mapping created with CreateFileMapping when it's already created. See earlier discussion on the topic: Windows: Resize shared memory .



来源:https://stackoverflow.com/questions/7752579/how-can-i-extend-the-length-of-a-memory-mapped-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!