home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / OLEFILE.CP_ / OLEFILE.CP
Encoding:
Text File  |  1993-02-08  |  2.5 KB  |  103 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 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 Microsoft 
  7. // QuickHelp and/or WinHelp 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_AUX_SEG
  14. #pragma code_seg(AFX_AUX_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. ////////////////////////////////////////////////////////////////////////////
  25. // CSharedFile implementation
  26.  
  27. IMPLEMENT_DYNAMIC(CSharedFile, CMemFile)
  28.  
  29. CSharedFile::CSharedFile(UINT nAllocFlags, UINT nGrowBytes /* = 1024 */)
  30.     : CMemFile(nGrowBytes)
  31. {
  32.     m_nAllocFlags = nAllocFlags;
  33.     m_hGlobalMemory = NULL;
  34. }
  35.  
  36. CSharedFile::~CSharedFile()
  37. {
  38.     if (m_lpBuffer)
  39.         Close();        // call appropriate Close/Free
  40.     ASSERT(m_lpBuffer == NULL);
  41. }
  42.  
  43. void
  44. CSharedFile::SetHandle(HGLOBAL hGlobalMemory)
  45. {
  46.     ASSERT(m_hGlobalMemory == NULL);        // do once only
  47.     ASSERT(m_lpBuffer == NULL);     // do once only
  48.     ASSERT(m_nPosition == 0);   
  49.  
  50.     m_hGlobalMemory = hGlobalMemory;
  51.     m_lpBuffer = (BYTE FAR*)::GlobalLock(m_hGlobalMemory);
  52.     m_nBufferSize = m_nFileSize = (UINT)::GlobalSize(m_hGlobalMemory);
  53. }
  54.  
  55. BYTE FAR* 
  56. CSharedFile::Alloc(UINT nBytes)
  57. {
  58.     ASSERT(m_hGlobalMemory == NULL);        // do once only
  59.     m_hGlobalMemory = ::GlobalAlloc(m_nAllocFlags, (DWORD) nBytes);
  60.     if (m_hGlobalMemory == NULL)
  61.         return NULL;
  62.     return (BYTE FAR*)::GlobalLock(m_hGlobalMemory);
  63. }
  64.  
  65. BYTE FAR* 
  66. CSharedFile::Realloc(BYTE FAR*, UINT nBytes)
  67. {
  68.     ASSERT(m_hGlobalMemory != NULL);
  69.     ::GlobalUnlock(m_hGlobalMemory);
  70.     HGLOBAL hNew;
  71.     hNew = ::GlobalReAlloc(m_hGlobalMemory, (DWORD) nBytes, m_nAllocFlags);
  72.     if (hNew == NULL)
  73.         return NULL;
  74.     m_hGlobalMemory = hNew;
  75.     return (BYTE FAR*)::GlobalLock(m_hGlobalMemory);
  76. }
  77.  
  78. void
  79. CSharedFile::Free(BYTE FAR*)
  80. {
  81.     ASSERT(m_hGlobalMemory != NULL);
  82.     ::GlobalUnlock(m_hGlobalMemory);
  83.     ::GlobalFree(m_hGlobalMemory);
  84. }
  85.  
  86. HGLOBAL
  87. CSharedFile::Detach()
  88. {
  89.     HGLOBAL hMem;
  90.     ASSERT(m_hGlobalMemory != NULL);
  91.     hMem = m_hGlobalMemory;
  92.  
  93.     m_hGlobalMemory = NULL; // detach from global handle
  94.  
  95.     // re-initialize the CMemFile parts too
  96.     m_lpBuffer = NULL;
  97.     m_nBufferSize = 0;
  98.  
  99.     return hMem;
  100. }
  101.  
  102. ////////////////////////////////////////////////////////////////////////////
  103.