How to increase file length with memory mapping?

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

How can I expand the memory and add some characters to the memory and save it in this file?

I already opened the file with the corresponding modes (fmOpenReadWrite, PAGE_READWRITE), and if I rewrote the characters, it will be saved to the file, but I need to add additional values ​​in the middle of the file.

+4
source share
2 answers

If file association is supported by the actual file, rather than the memory block, you can change the file size in one of two ways:

1) call CreateFileMapping() with a size larger than the current file size. The file will be modified 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() .

+8
source

You cannot resize the mapping of files created using CreateFileMapping when it is already created. See Previous topic discussion: Windows: changing shared memory .

+3
source

All Articles