Private Declare Function apiGetProcessHeap& Lib "kernel32" Alias "GetProcessHeap" ()
Private Declare Function apiHeapAlloc& Lib "kernel32" Alias "HeapAlloc" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long)
Private Declare Function apiHeapCompact& Lib "kernel32" Alias "HeapCompact" (ByVal hHeap As Long, ByVal dwFlags As Long)
Private Declare Function apiHeapCreate& Lib "kernel32" Alias "HeapCreate" (ByVal flOptions As Long, ByVal dwInitialSize As Long, ByVal dwMaximumSize As Long)
Private Declare Function apiHeapDestroy& Lib "kernel32" Alias "HeapDestroy" (ByVal hHeap As Long)
Private Declare Function apiHeapFree& Lib "kernel32" Alias "HeapFree" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem As Long)
Private Declare Function apiHeapLock& Lib "kernel32" Alias "HeapLock" (ByVal hHeap As Long)
Private Declare Function apiHeapReAlloc& Lib "kernel32" Alias "HeapReAlloc" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem As Long, ByVal dwBytes As Long)
Private Declare Function apiHeapSize& Lib "kernel32" Alias "HeapSize" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem As Long)
Private Declare Function apiHeapUnlock& Lib "kernel32" Alias "HeapUnlock" (ByVal hHeap As Long)
Private Declare Function apiHeapValidate& Lib "kernel32" Alias "HeapValidate" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem As Long)
#End If 'WIN32
Public Sub NewHeap(initSize, MaxSize)
Dim ret&
If HeapSource% = 1 Then
ret& = apiHeapDestroy(iHeap)
If ret& = 0 Then RaiseMemoryError
End If
HeapSource = 1
ret& = apiHeapCreate&(0, initSize, MaxSize)
If ret& = 0 Then RaiseMemoryError
iHeap = ret&
End Sub
Public Property Let hHeap(vHeapHandle As Long)
Dim ret&
If apiHeapUnlock&(vHeapHandle) = 0 Then RaiseMemoryError
If HeapSource% = 1 Then
ret& = apiHeapDestroy(iHeap)
If ret& = 0 Then RaiseMemoryError
End If
HeapSource% = 1
End Property
Public Sub UseProcessHeap()
Dim ret&
If HeapSource% = 1 Then
ret& = apiHeapDestroy(iHeap)
If ret& = 0 Then RaiseMemoryError
End If
HeapSource% = 0
iHeap = apiGetProcessHeap()
End Sub
Private Sub RaiseMemoryError(Optional errval)
If IsMissing(errval) Then
RaiseMemoryError DWERR_APIRESULT
Else
RaiseError errval, "dwMemory"
End If
End Sub
Public Sub Alloc(memLength As Long)
Attribute Alloc.VB_HelpID = 2134
Attribute Alloc.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"
Dim ret&
If iMemAddr <> 0 Then
ret& = apiHeapFree(iHeap, 0, iMemAddr)
If ret& = 0 Then RaiseMemoryError
iMemAddr = 0
End If
If memLength < 1 Then RaiseMemoryError
iMemAddr = apiHeapAlloc(iHeap, 0, memLength)
If iMemAddr = 0 Then RaiseMemoryError
iMemSize = memLength
End Sub
Public Sub ReAlloc(memLength As Long)
Attribute ReAlloc.VB_HelpID = 2135
Attribute ReAlloc.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"