description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: CSharedFile Class |
CSharedFile Class |
11/04/2016 |
|
|
5d000422-9ede-4318-a8c9-f7412b674f39 |
The CMemFile-derived class that supports shared memory files.
class CSharedFile : public CMemFile
Name | Description |
---|---|
CSharedFile::CSharedFile | Constructs a CSharedFile object. |
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. |
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.
CSharedFile
Header: afxadv.h
Constructs a CSharedFile
object and allocates memory for it.
CSharedFile(
UINT nAllocFlags = GMEM_DDESHARE | GMEM_MOVEABLE,
UINT nGrowBytes = 4096);
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.
Call this function to close the memory file and detach it from the memory block.
HGLOBAL Detach();
The handle of the memory block that contains the contents of the memory file.
You can reopen it by calling SetHandle, using the handle returned by Detach.
Call this function to attach a block of global memory to the CSharedFile
object.
void SetHandle(
HGLOBAL hGlobalMemory,
BOOL bAllowGrow = TRUE);
hGlobalMemory
Handle to the global memory to be attached to the CSharedFile
.
bAllowGrow
Specifies whether the memory block is allowed to grow.
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.