⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmmemmappedfile.h

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 H
字号:
#ifndef VM_MEM_MAPPED_FILE_H_INCLUDED
#define VM_MEM_MAPPED_FILE_H_INCLUDED
/*****************************************************************************/
/*                           HEADER FILE                                     */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

   Description:  Memory Mapped File Header file

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
/*****************************************************************************/

//	Usage of this class: 
//  !! You MUST DERIVE a class from VMMemMappedFile. This is because VMMemMappedFile
//     has two pure virtual methods. Your derived class must implement these methods.
//     Since these methods are involved with data i/o from the memory mapped file,
//     this approach is necessary to allow your program to do whatever it takes to
//     move data in and out of the memory mapped file.
//
//  construction arguments: 
//  CMemMappedStrings( lpMemMapFileName, bCreate, dwSize );
//                     lpMemMapFileName = the name of the memory mapped file
//                     bCreate = pass true if you want the memory mapped file and
//                               support objects to be created in the kernel. One
//                               side or the other must construct using TRUE. No 
//                               harm will come if both sides choose to pass TRUE
//                               for this argument.
//                     dwSizeLow  = is passed as the low word in CreateFileMapping
#include "../../../stdafx.h"


#define BUFFER_LOCKED	  1
#define FATAL_ERROR		  -9999


class VMMemMappedFile
{
public:
  VMMemMappedFile( char* pchName, bool bCreate, DWORD dwSize);
  ~VMMemMappedFile( void );

  void  GetLastErrorText( char* pchBuffer, int iBufferSize );
  DWORD GetStatus( void ){ return( m_dwSysReturnCode ); };
      
  unsigned long Read( void*  pvYourBuffer, 
                      DWORD  dwYourBufferSize, 
                      DWORD  dwLowOffsetToStartReadAt );

  unsigned long  Write( void*  pvYourData, 
                        DWORD  dwDataSize, 
                        DWORD  dwLowOffsetToStartWriteAt );

  void* GetBufferFromMemMappedFile( DWORD dwOffset          = 0, 
                                    DWORD dwBufferSizeToGet = 0 );

  bool LockBuffer( void );
  void ReleaseBuffer( void );

  inline DWORD GetSize( void )       { return( m_dwSizeLow        ); };
  inline bool  CreatedMapping( void ){ return( m_bICreatedMapping ); };


protected:
  virtual long PostProcessReadData( void* pvMemMappedDataBuffer, void* pvYourData ) = 0;
  virtual long PreProcessWriteData( void* pvMemMappedDataBuffer, 
                                    void* pvToYourData,
                                    DWORD  dwYourBufferSize ) = 0;
private:
  bool  Create( void );
  bool  Open( void );
  bool  Lock( HANDLE hHandle, char* pchSemaphoreName );

  inline void UnlockWrites( void )
  { ::ReleaseSemaphore( m_hWriteSemaphore, 1L, NULL ); };

  inline void UnlockReads( void )
  { ::ReleaseSemaphore( m_hReadSemaphore, 1L, NULL ); };

  inline bool LockForWrites( void )
  { return ( Lock( m_hWriteSemaphore, m_achSemNameWrite ) ); };

  inline bool LockForReads( void )
  { return ( Lock( m_hReadSemaphore, m_achSemNameRead ) ); };

private:
  HANDLE  m_hFileMap;
  HANDLE  m_hFileMapReader;
  bool    m_bICreatedMapping;
  DWORD   m_dwSysReturnCode;
  char    m_achName[ 25 ];
  char    m_achSemNameRead[ 32 ];
  char    m_achSemNameWrite[ 32 ];
  DWORD   m_dwSizeHigh;
  DWORD   m_dwSizeLow;
  void*   m_pvView;
  HANDLE  m_hWriteSemaphore;
  HANDLE  m_hReadSemaphore;
	UINT    m_iFlags;
};



#endif



/*****************************************************************************/
/* Check-in history */
/*
*$Log:   $
*/
/*****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -