home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / mmfile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-12  |  1.9 KB  |  61 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : mmfile.cpp                                                          //
  10. //  Description: Memory-mapped file                                                  //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include "mmfile.h"
  19.  
  20. bool KMemoryMappedFile::Open(LPCTSTR szFileName)
  21. {
  22.     m_hFile = CreateFile(szFileName, GENERIC_READ, 
  23.         FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  24.  
  25.     if ( m_hFile == INVALID_HANDLE_VALUE )
  26.         return false;
  27.  
  28.     m_hMapping = CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  29.  
  30.     if ( m_hMapping )
  31.     {
  32.         m_View = (const unsigned char *) MapViewOfFile(m_hMapping, FILE_MAP_READ, 0, 0, 0);
  33.  
  34.         return m_View != NULL;
  35.     }
  36.     else
  37.         return false;
  38. }
  39.  
  40.  
  41. KMemoryMappedFile::~KMemoryMappedFile()
  42. {
  43.     if ( m_View )
  44.     {
  45.         UnmapViewOfFile(m_View);
  46.         m_View = NULL;
  47.     }
  48.  
  49.     if ( m_hMapping )
  50.     {
  51.         CloseHandle(m_hMapping);
  52.         m_hMapping = NULL;
  53.     }
  54.  
  55.     if ( m_hFile!=INVALID_HANDLE_VALUE )
  56.     {
  57.         CloseHandle(m_hFile);
  58.         m_hFile = INVALID_HANDLE_VALUE;
  59.     }
  60. }
  61.