home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD1070210162000.psc / cMemory.cls < prev    next >
Encoding:
Visual Basic class definition  |  2000-09-10  |  3.6 KB  |  129 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cMemory"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. ' ==========================================================================
  16. ' Class:    cMemory
  17. ' Filename: cMemory.cls
  18. ' Author:   Steve McMahon
  19. ' Date:     24 May 1998
  20. '
  21. ' A class for manipulating API memory blocks.
  22. ' ==========================================================================
  23.  
  24. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  25. Private Declare Function GlobalCompact Lib "kernel32" (ByVal dwMinFree As Long) As Long
  26. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  27. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
  28. Private Declare Function GlobalReAlloc Lib "kernel32" (ByVal hMem As Long, ByVal dwBytes As Long, ByVal wFlags As Long) As Long
  29. Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
  30. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  31. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  32.     lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  33.  
  34. Public Enum EMemoryFlags
  35.     GMEM_DDESHARE = &H2000
  36.     GMEM_DISCARDABLE = &H100
  37.     GMEM_DISCARDED = &H4000
  38.     GMEM_INVALID_HANDLE = &H8000
  39.     GMEM_FIXED = &H0
  40.     GMEM_LOCKCOUNT = &HFF
  41.     GMEM_MODIFY = &H80
  42.     GMEM_MOVEABLE = &H2
  43.     GMEM_NODISCARD = &H20
  44.     GMEM_NOCOMPACT = &H10
  45.     GMEM_NOT_BANKED = &H1000
  46.     GMEM_LOWER = GMEM_NOT_BANKED
  47.     GMEM_NOTIFY = &H4000
  48.     GMEM_SHARE = &H2000
  49.     GMEM_VALID_FLAGS = &H7F72
  50.     GMEM_ZEROINIT = &H40
  51.     GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
  52. End Enum
  53.  
  54. Private m_hMem As Long
  55. Private m_lPtr As Long
  56.  
  57. Public Property Get Handle() As Long
  58.     Handle = m_hMem
  59. End Property
  60. Public Property Let Handle(ByVal hMem As Long)
  61.     If (m_hMem <> 0) Then
  62.         FreeMemory
  63.     End If
  64.     m_hMem = hMem
  65. End Property
  66. Public Property Get Pointer() As Long
  67.     If (m_hMem <> 0) Then
  68.         If (m_lPtr = 0) Then
  69.             LockMemory
  70.         End If
  71.         Pointer = m_lPtr
  72.     End If
  73. End Property
  74. Public Property Get Size() As Long
  75.     If (m_hMem <> 0) Then
  76.         Size = GlobalSize(m_hMem)
  77.     End If
  78. End Property
  79. Public Function AllocateMemory( _
  80.     ByVal lSize As Long, _
  81.     Optional ByVal dwFlags As Long = GPTR _
  82.     ) As Boolean
  83.     FreeMemory
  84.     m_hMem = GlobalAlloc(dwFlags, lSize)
  85.     If (m_hMem <> 0) Then
  86.         ' Success
  87.         AllocateMemory = True
  88.       Else
  89.         ' Failed
  90.     End If
  91. End Function
  92. Public Function LockMemory() As Boolean
  93.     If (m_hMem <> 0) Then
  94.         If (m_lPtr = 0) Then
  95.             m_lPtr = GlobalLock(m_hMem)
  96.             If (m_lPtr <> 0) Then
  97.                 ' Success
  98.                 LockMemory = True
  99.               Else
  100.                 ' Failed
  101.             End If
  102.         End If
  103.     End If
  104. End Function
  105. Public Sub UnlockMemory()
  106.     If (m_hMem <> 0) Then
  107.         If (m_lPtr <> 0) Then
  108.             GlobalUnlock m_hMem
  109.             m_lPtr = 0
  110.         End If
  111.     End If
  112. End Sub
  113. Public Sub FreeMemory()
  114.     If (m_hMem <> 0) Then
  115.         UnlockMemory
  116.         GlobalFree m_hMem
  117.     End If
  118.     m_hMem = 0
  119. End Sub
  120. Public Sub ReleaseDontFreeMemory()
  121. ' For GMEM_DDESHARE operations...
  122.     UnlockMemory
  123.     m_hMem = 0
  124. End Sub
  125.  
  126. Private Sub Class_Terminate()
  127.     FreeMemory
  128. End Sub
  129.