Rediger

Del via


CSharedFile Class

The CMemFile-derived class that supports shared memory files.

Syntax

class CSharedFile : public CMemFile

Members

Public Constructors

Name Description
CSharedFile::CSharedFile Constructs a CSharedFile object.

Public Methods

Name Description
CSharedFile::Detach Closes the shared memory file and returns the handle of its memory block.
CSharedFile::SetHandle Attaches the shared memory file to a memory block.

Remarks

Memory files behave like disk files. The difference is, a memory file is stored in RAM rather than on disk. A memory file is useful for fast temporary storage, or for transferring raw bytes or serialized objects between independent processes.

Shared memory files differ from other memory files in that memory for them is allocated with the GlobalAlloc Windows function. The CSharedFile class stores data in a globally allocated memory block (created using GlobalAlloc), and this memory block can be shared using DDE, the Clipboard, or other OLE/COM uniform data transfer operations, for example, using IDataObject.

GlobalAlloc returns an HGLOBAL handle rather than a pointer to memory, such as the pointer returned by malloc. The HGLOBAL handle is needed in certain applications. For example, to put data on the Clipboard you need an HGLOBAL handle.

CSharedFile doesn't use memory-mapped files, and the data can't be directly shared between processes.

CSharedFile objects can automatically allocate their own memory. Or, you can attach your own memory block to the CSharedFile object by calling CSharedFile::SetHandle. In either case, memory for growing the memory file automatically is allocated in nGrowBytes-sized increments if nGrowBytes isn't zero.

For more information, see the article Files in MFC and File Handling in the Run-Time Library Reference.

Inheritance Hierarchy

CObject

CFile

CMemFile

CSharedFile

Requirements

Header: afxadv.h

CSharedFile::CSharedFile

Constructs a CSharedFile object and allocates memory for it.

CSharedFile(
    UINT nAllocFlags = GMEM_DDESHARE | GMEM_MOVEABLE,
    UINT nGrowBytes = 4096);

Parameters

nAllocFlags
Flags indicating how memory is to be allocated. See GlobalAlloc for a list of valid flag values.

nGrowBytes
The memory allocation increment in bytes.

CSharedFile::Detach

Call this function to close the memory file and detach it from the memory block.

HGLOBAL Detach();

Return Value

The handle of the memory block that contains the contents of the memory file.

Remarks

You can reopen it by calling SetHandle, using the handle returned by Detach.

CSharedFile::SetHandle

Call this function to attach a block of global memory to the CSharedFile object.

void SetHandle(
    HGLOBAL hGlobalMemory,
    BOOL bAllowGrow = TRUE);

Parameters

hGlobalMemory
Handle to the global memory to be attached to the CSharedFile.

bAllowGrow
Specifies whether the memory block is allowed to grow.

Remarks

If bAllowGrow is nonzero, the size of the memory block is increased as necessary, for example, if you attempt to write more bytes to the file than the size of the memory block.

See also

CMemFile Class
Hierarchy Chart
CMemFile Class