home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / fileshrd.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  3KB  |  106 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE4_SEG
  14. #pragma code_seg(AFX_CORE4_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. ////////////////////////////////////////////////////////////////////////////
  25. // CSharedFile implementation
  26.  
  27. CSharedFile::CSharedFile(UINT nAllocFlags, UINT nGrowBytes)
  28.     : CMemFile(nGrowBytes)
  29. {
  30.     m_nAllocFlags = nAllocFlags;
  31.     m_hGlobalMemory = NULL;
  32.     m_bAllowGrow = TRUE;
  33. }
  34.  
  35. CSharedFile::~CSharedFile()
  36. {
  37.     if (m_lpBuffer)
  38.         Close();        // call appropriate Close/Free
  39.     ASSERT(m_lpBuffer == NULL);
  40. }
  41.  
  42. void CSharedFile::SetHandle(HGLOBAL hGlobalMemory, BOOL bAllowGrow)
  43. {
  44.     ASSERT(m_hGlobalMemory == NULL);        // do once only
  45.     ASSERT(m_lpBuffer == NULL);     // do once only
  46.     ASSERT(m_nPosition == 0);
  47.  
  48.     m_hGlobalMemory = hGlobalMemory;
  49.     m_lpBuffer = (BYTE*)::GlobalLock(m_hGlobalMemory);
  50.     m_nBufferSize = m_nFileSize = ::GlobalSize(m_hGlobalMemory);
  51.     m_bAllowGrow = bAllowGrow;
  52. }
  53.  
  54. BYTE* CSharedFile::Alloc(DWORD nBytes)
  55. {
  56.     ASSERT(m_hGlobalMemory == NULL);        // do once only
  57.     m_hGlobalMemory = ::GlobalAlloc(m_nAllocFlags, nBytes);
  58.     if (m_hGlobalMemory == NULL)
  59.         return NULL;
  60.     return (BYTE*)::GlobalLock(m_hGlobalMemory);
  61. }
  62.  
  63. BYTE* CSharedFile::Realloc(BYTE*, DWORD nBytes)
  64. {
  65.     if (!m_bAllowGrow)
  66.         return NULL;
  67.     ASSERT(m_hGlobalMemory != NULL);
  68.     ::GlobalUnlock(m_hGlobalMemory);
  69.     HGLOBAL hNew;
  70.     hNew = ::GlobalReAlloc(m_hGlobalMemory, nBytes, m_nAllocFlags);
  71.     if (hNew == NULL)
  72.         return NULL;
  73.     m_hGlobalMemory = hNew;
  74.     return (BYTE*)::GlobalLock(m_hGlobalMemory);
  75. }
  76.  
  77. void CSharedFile::Free(BYTE*)
  78. {
  79.     ASSERT(m_hGlobalMemory != NULL);
  80.     ::GlobalUnlock(m_hGlobalMemory);
  81.     ::GlobalFree(m_hGlobalMemory);
  82. }
  83.  
  84. HGLOBAL CSharedFile::Detach()
  85. {
  86.     HGLOBAL hMem;
  87.     ASSERT(m_hGlobalMemory != NULL);
  88.     hMem = m_hGlobalMemory;
  89.  
  90.     m_hGlobalMemory = NULL; // detach from global handle
  91.  
  92.     // re-initialize the CMemFile parts too
  93.     m_lpBuffer = NULL;
  94.     m_nBufferSize = 0;
  95.  
  96.     return hMem;
  97. }
  98.  
  99. #ifdef AFX_INIT_SEG
  100. #pragma code_seg(AFX_INIT_SEG)
  101. #endif
  102.  
  103. IMPLEMENT_DYNAMIC(CSharedFile, CMemFile)
  104.  
  105. ////////////////////////////////////////////////////////////////////////////
  106.