home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD44023302000.psc / cMemory.cls next >
Encoding:
Visual Basic class definition  |  2000-03-25  |  3.4 KB  |  123 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. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
  17. Private Declare Function GlobalCompact Lib "kernel32" (ByVal dwMinFree As Long) As Long
  18. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
  19. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
  20. Private Declare Function GlobalReAlloc Lib "kernel32" (ByVal hMem As Long, ByVal dwBytes As Long, ByVal wFlags As Long) As Long
  21. Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
  22. Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
  23. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  24.     lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  25.  
  26. Public Enum EMemoryFlags
  27.     GMEM_DDESHARE = &H2000
  28.     GMEM_DISCARDABLE = &H100
  29.     GMEM_DISCARDED = &H4000
  30.     GMEM_INVALID_HANDLE = &H8000
  31.     GMEM_FIXED = &H0
  32.     GMEM_LOCKCOUNT = &HFF
  33.     GMEM_MODIFY = &H80
  34.     GMEM_MOVEABLE = &H2
  35.     GMEM_NODISCARD = &H20
  36.     GMEM_NOCOMPACT = &H10
  37.     GMEM_NOT_BANKED = &H1000
  38.     GMEM_LOWER = GMEM_NOT_BANKED
  39.     GMEM_NOTIFY = &H4000
  40.     GMEM_SHARE = &H2000
  41.     GMEM_VALID_FLAGS = &H7F72
  42.     GMEM_ZEROINIT = &H40
  43.     GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
  44. End Enum
  45.  
  46. Private m_hMem As Long
  47. Private m_lPtr As Long
  48.  
  49. Public Property Get Handle() As Long
  50.     Handle = m_hMem
  51. End Property
  52. Public Property Let Handle(ByVal hMem As Long)
  53.     If (m_hMem <> 0) Then
  54.         FreeMemory
  55.     End If
  56.     m_hMem = hMem
  57. End Property
  58. Public Property Get Pointer() As Long
  59.     If (m_hMem <> 0) Then
  60.         If (m_lPtr = 0) Then
  61.             LockMemory
  62.         End If
  63.         Pointer = m_lPtr
  64.     End If
  65. End Property
  66. Public Property Get Size() As Long
  67.     If (m_hMem <> 0) Then
  68.         Size = GlobalSize(m_hMem)
  69.     End If
  70. End Property
  71. Public Function AllocateMemory( _
  72.         ByVal lSize As Long, _
  73.         Optional ByVal dwFlags As Long = GPTR _
  74.     ) As Boolean
  75.     FreeMemory
  76.     m_hMem = GlobalAlloc(dwFlags, lSize)
  77.     If (m_hMem <> 0) Then
  78.         ' Success
  79.         AllocateMemory = True
  80.     Else
  81.         ' Failed
  82.     End If
  83. End Function
  84. Public Function LockMemory() As Boolean
  85.     If (m_hMem <> 0) Then
  86.         If (m_lPtr = 0) Then
  87.             m_lPtr = GlobalLock(m_hMem)
  88.             If (m_lPtr <> 0) Then
  89.                 ' Success
  90.                 LockMemory = True
  91.             Else
  92.                 ' Failed
  93.             End If
  94.         End If
  95.     End If
  96. End Function
  97. Public Sub UnlockMemory()
  98.     If (m_hMem <> 0) Then
  99.         If (m_lPtr <> 0) Then
  100.             GlobalUnlock m_hMem
  101.             m_lPtr = 0
  102.         End If
  103.     End If
  104. End Sub
  105. Public Sub FreeMemory()
  106.     If (m_hMem <> 0) Then
  107.         UnlockMemory
  108.         GlobalFree m_hMem
  109.     End If
  110.     m_hMem = 0
  111. End Sub
  112. Public Sub ReleaseDontFreeMemory()
  113.     ' For GMEM_DDESHARE operations...
  114.     UnlockMemory
  115.     m_hMem = 0
  116. End Sub
  117.  
  118. Private Sub Class_Terminate()
  119.     FreeMemory
  120. End Sub
  121.  
  122.  
  123.