CMemoryFile

$Revision: 17 $

Description

This class makes memory mapped files easy to use. Memory mapped files are great when you have to access the contents of a file directly. Instead of declaring a big buffer (at least the size of the file) then reading the file into that buffer, mapping the file contents to a pointer is much faster.
Mapping a file to a pointer lets you dereference the pointer as needed and let the operating system handle the retrieval of the bytes from the file. Very cool.

Constructors

CMemoryFile()
CMemoryFile( DWORD                 allocation_granularity,
                   SECURITY_ATTRIBUTES * security_attributes = NULL,
                   SECURITY_DESCRIPTOR * security_descriptor = NULL )
Constructs the object. By specifying allocation_granularity you can avoid the overhead of calling GetSystemInfo() which the default constructor does. This is useful when you have to map thousands of files. Since the allocation granularity never changes, when keep calling GetSystemInfo()?

Data Members

DWORD Size
Holds the same value as GetLength(). This member is exposed for speed only. It is not used internally by the class.

Methods

void Close( void )
Closes the file.
BOOL Flush( void )
Synchronizes the contents of memory with the contents of the physical disk file.
BOOL FromHandle( HANDLE file_handle,
          UINT open_flags = CFile64::modeRead,
          ULONGLONG begining_at = 0,
          DWORD number_of_bytes_to_map,
          void * desired_address = NULL )
Pretty much the same as Open() except you pass in the handle to the file to use in mapping. This makes it much faster when you have to remap different sections of the same file. If you pass the handle to the file, you can skip the overhead that Open() must incur by going to the filesystem to actually open the file you're going to map. FromHandle() doesn't have that problem.
DWORD GetAttributes( void ) const
Retrieves the attributes used in the wrapped CreateFile() API. This value is used in the sixth parameter to CreateFile(). This method is called by the Open method. The default value is FILE_ATTRIBUTE_NORMAL.
ULONGLONG GetFileLength( void ) const
Returns the length of the Open()'d file. Yes, this returns a 64-bit length.
void GetFilename( CString& filename ) const
Retrieves the name of the file that is mapped. If filename is empty, no file has been opened.
DWORD GetLength( void ) const
Returns the number of bytes that were mapped.
void * GetPointer( void ) const
Returns the pointer to the mapped bytes. If this method returns NULL, it means there is no file mapped.
HANDLE GetTemplateFile( void ) const
Retrieves the handle to the file that will be used as a template in the Open method. This handle is used as the seventh parameter to the wrapped CreateFile() API.
BOOL Open( LPCTSTR filename,
          UINT open_flags = CFile64::modeRead,
          ULONGLONG begining_at = 0,
          DWORD number_of_bytes_to_map,
          void * desired_address = NULL )
This method is patterend after the Open() method in the MFC CFile64 class. In it's simplest form, you pass a name of a file and it will open that file in read only mode. This is great for parsing data files. You don't have to worry about the beginning_at value be a special value (like in the call to the MapViewOfFile() API). The other parameters have these meanings:
void SetAttributes( DWORD attributes )
Sets the attributes used in the wrapped CreateFile() API. This value is used in the sixth parameter to CreateFile().
void SetTemplateFile( HANDLE template_file_handle )
Sets the handle to the file that will be used as a template in the Open method. This handle is used as the seventh parameter to the wrapped CreateFile() API.

Example

#include <wfc.h>
#pragma hdrstop

void test_CMemoryFile( void )
{
   WFCTRACEINIT( TEXT( "test_CMemoryFile()" ) );

   CMemoryFile memory_mapped_file;

   // Set things up so the file that is mapped is automatically deleted

   memory_mapped_file.SetAttributes( FILE_FLAG_DELETE_ON_CLOSE );

   BYTE * pointer = NULL;

   if ( memory_mapped_file.Open( TEXT( "datafile.dat" ) ) == FALSE )
   {
      return;
   }

   pointer = (BYTE *) memory_mapped_file.GetPointer();

   _tprintf( TEXT( "The last byte of the file is %X\n" ),
             pointer[ memory_mapped_file.GetLength() - 1 ] );
}

API's Used


Copyright, 2000, Samuel R. Blackburn
$Workfile: CMemoryFile.cpp $
$Modtime: 6/26/01 10:47a $