|
|
1.1 ! root 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 documentation provided with the library. ! 8: // See these sources for detailed information regarding the ! 9: // Microsoft Foundation Classes product. ! 10: ! 11: #include "afxole.h" ! 12: #pragma hdrstop ! 13: ! 14: #ifdef AFX_OLE_SEG ! 15: #pragma code_seg(AFX_OLE_SEG) ! 16: #endif ! 17: ! 18: #ifdef _DEBUG ! 19: #undef THIS_FILE ! 20: static char BASED_CODE THIS_FILE[] = __FILE__; ! 21: #endif ! 22: ! 23: #define new DEBUG_NEW ! 24: ! 25: //////////////////////////////////////////////////////////////////////////// ! 26: // CSharedFile implementation ! 27: ! 28: IMPLEMENT_DYNAMIC(CSharedFile, CMemFile) ! 29: ! 30: CSharedFile::CSharedFile(UINT nAllocFlags, UINT nGrowBytes /* = 1024 */) ! 31: : CMemFile(nGrowBytes) ! 32: { ! 33: m_nAllocFlags = nAllocFlags; ! 34: m_hGlobalMemory = NULL; ! 35: } ! 36: ! 37: CSharedFile::~CSharedFile() ! 38: { ! 39: if (m_lpBuffer) ! 40: Close(); // call appropriate Close/Free ! 41: ASSERT(m_lpBuffer == NULL); ! 42: } ! 43: ! 44: void ! 45: CSharedFile::SetHandle(HANDLE hGlobalMemory) ! 46: { ! 47: ASSERT(m_hGlobalMemory == NULL); // do once only ! 48: ASSERT(m_lpBuffer == NULL); // do once only ! 49: ASSERT(m_nPosition == 0); ! 50: ! 51: m_hGlobalMemory = hGlobalMemory; ! 52: m_lpBuffer = (BYTE FAR*)::GlobalLock(m_hGlobalMemory); ! 53: m_nBufferSize = m_nFileSize = (UINT)::GlobalSize(m_hGlobalMemory); ! 54: } ! 55: ! 56: BYTE FAR* ! 57: CSharedFile::Alloc(UINT nBytes) ! 58: { ! 59: ASSERT(m_hGlobalMemory == NULL); // do once only ! 60: m_hGlobalMemory = ::GlobalAlloc(m_nAllocFlags, (DWORD) nBytes); ! 61: if (m_hGlobalMemory == NULL) ! 62: return NULL; ! 63: return (BYTE FAR*)::GlobalLock(m_hGlobalMemory); ! 64: } ! 65: ! 66: BYTE FAR* ! 67: CSharedFile::Realloc(BYTE FAR*, UINT nBytes) ! 68: { ! 69: ASSERT(m_hGlobalMemory != NULL); ! 70: ::GlobalUnlock(m_hGlobalMemory); ! 71: HANDLE hNew; ! 72: hNew = ::GlobalReAlloc(m_hGlobalMemory, (DWORD) nBytes, m_nAllocFlags); ! 73: if (hNew == NULL) ! 74: return NULL; ! 75: m_hGlobalMemory = hNew; ! 76: return (BYTE FAR*)::GlobalLock(m_hGlobalMemory); ! 77: } ! 78: ! 79: void ! 80: CSharedFile::Free(BYTE FAR*) ! 81: { ! 82: ASSERT(m_hGlobalMemory != NULL); ! 83: ::GlobalUnlock(m_hGlobalMemory); ! 84: ::GlobalFree(m_hGlobalMemory); ! 85: } ! 86: ! 87: HANDLE ! 88: CSharedFile::Detach() ! 89: { ! 90: HANDLE hMem; ! 91: ASSERT(m_hGlobalMemory != NULL); ! 92: hMem = m_hGlobalMemory; ! 93: ! 94: m_hGlobalMemory = NULL; // detach from global handle ! 95: ! 96: // re-initialize the CMemFile parts too ! 97: m_lpBuffer = NULL; ! 98: m_nBufferSize = 0; ! 99: ! 100: return hMem; ! 101: } ! 102: ! 103: ////////////////////////////////////////////////////////////////////////////
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.