共用方式為


CMemFile::Attach

Call this function to attach a block of memory to CMemFile.

void Attach(
   BYTE* lpBuffer,
   UINT nBufferSize,
   UINT nGrowBytes = 0 
);

Parameters

  • lpBuffer
    Pointer to the buffer to be attached to CMemFile.

  • nBufferSize
    An integer that specifies the size of the buffer in bytes.

  • nGrowBytes
    The memory allocation increment in bytes.

Remarks

This causes CMemFile to use the block of memory as the memory file.

If nGrowBytes is 0, CMemFile will set the file length to nBufferSize. This means that the data in the memory block before it was attached to CMemFile will be used as the file. Memory files created in this manner cannot be grown.

Since the file cannot be grown, be careful not to cause CMemFile to attempt to grow the file. For example, don't call the CMemFile overrides of CFile:Write to write past the end or don't call CFile:SetLength with a length longer than nBufferSize.

If nGrowBytes is greater than 0, CMemFile will ignore the contents of the memory block you've attached. You'll have to write the contents of the memory file from scratch using the CMemFile override of CFile::Write. If you attempt to write past the end of the file or grow the file by calling the CMemFile override of CFile::SetLength, CMemFile will grow the memory allocation in increments of nGrowBytes. Growing the memory allocation will fail if the memory block you pass to Attach wasn't allocated with a method compatible with Alloc. To be compatible with the default implementation of Alloc, you must allocate the memory with the run-time library function malloc or calloc.

Requirements

Header: afx.h

See Also

Concepts

CMemFile Class

CMemFile Members

Hierarchy Chart

CMemFile::CMemFile

CMemFile::Detach

CMemFile::Alloc

CFile::Write

CFile::SetLength