Private Type SYSTEM_INFO
dwOemID As Long
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOrfProcessors As Long
dwProcessorType As Long
dwAllocationGranularity As Long
dwReserved As Long
End Type
Private Type MEMORY_BASIC_INFORMATION
BaseAddress As Long
AllocationBase As Long
AllocationProtect As Long
RegionSize As Long
State As Long
Protect As Long
lType As Long
End Type
Private Declare Sub GetSystemInfo Lib "Kernel32" (lpSystemInfo As SYSTEM_INFO)
Private Declare Function VirtualQuery Lib "Kernel32" (ByVal lpAddress As Long, lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Long) As Long
Public Function GetMemoryUsedByProcess() As Long
' Purpose: Returns the number of bytes used by current process.
' Returns -1 if function fails
Dim lngLenOfMBI As Long ' Size of MBI type
Dim lngMemoryPointer As Long ' Memory pointer
Dim lngPrivateBytes As Long ' Total bytes used so far
Dim lngReturnValue As Long ' API return code
Dim MBI As MEMORY_BASIC_INFORMATION ' Memory block status
Dim SI As SYSTEM_INFO ' Used to find out address range used my this process
' Assume failure
GetMemoryUsedByProcess = -1
' Get number of bytes in MEMORY_BASIC_INFORMATION structure
lngLenOfMBI = Len(MBI)
' Find the address range used by this process
Call GetSystemInfo(SI)
lngMemoryPointer = SI.lpMinimumApplicationAddress
Do While lngMemoryPointer < SI.lpMaximumApplicationAddress
MBI.RegionSize = 0
lngReturnValue = VirtualQuery(lngMemoryPointer, MBI, lngLenOfMBI)
If lngReturnValue = lngLenOfMBI Then
If ((MBI.lType = MEM_PRIVATE) And (MBI.State = MEM_COMMIT)) Then
' This block is in use by this process
lngPrivateBytes = lngPrivateBytes + MBI.RegionSize
End If
On Error GoTo Finished
' the only time an error can occur on the next line is an overflow
' error. We have got as far as we can anyway so we must have finished.
' advance lngMemoryPointer to the next address range.
lngMemoryPointer = MBI.BaseAddress + MBI.RegionSize
On Error GoTo 0 ' switch off error trapping
Else
' Function failed - abort loop
Exit Function
End If
Loop
Finished:
GetMemoryUsedByProcess = lngPrivateBytes
End Function