Answer» I am making a application and I need a WAY to display the total memory in VB6. ONLINE, all the scripts i tried were hard. I am programming on NT4 and need a easy script. Can you help me??? Visual Basic 6.0, not .NETtry this-
project... Add Class Module
Name it, "CMemState" paste in the following:
Code: [Select]Option Explicit
'typedef struct _MEMORYSTATUSEX { ' DWORD dwLength; ' DWORD dwMemoryLoad; ' DWORDLONG ullTotalPhys; ' DWORDLONG ullAvailPhys; ' DWORDLONG ullTotalPageFile; ' DWORDLONG ullAvailPageFile; ' DWORDLONG ullTotalVirtual; ' DWORDLONG ullAvailVirtual; ' DWORDLONG ullAvailExtendedVirtual; '} MEMORYSTATUSEX, *LPMEMORYSTATUSEX; Private Type INT64 LoPart As Long HiPart As Long End Type Private Type MEMORYSTATUS dwLength As Long dwMemoryLoad As Long dwTotalPhys As Long dwAvailPhys As Long dwTotalPageFile As Long dwAvailPageFile As Long dwTotalVirtual As Long dwAvailVirtual As Long End Type
Private Type MEMORYSTATUSEX Length As Long MemoryLoad As Long ulTotalPhys As INT64 ulAvailPhys As INT64 ulTotalPageFile As INT64 ulAvailPageFile As INT64 ulTotalVirtual As INT64 ulAvailVirtual As INT64 UlAvailExtendedVirtual As INT64 End Type
Private Type MungeLong LongA As Long LongB As Long End Type Private Type MungeCurr CurrA As Currency End Type Private Declare Sub GlobalMemoryStatus Lib "kernel32.dll" (ByRef lpBuffer As MEMORYSTATUS) Private Declare Function GlobalMemoryStatusEx Lib "kernel32.dll" (ByRef lpBuffer As MEMORYSTATUSEX) As Long Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (ByRef Destination As Any, ByVal Length As Long) Private Mstatus As MEMORYSTATUS Private mStatusEx As MEMORYSTATUSEX Private mOldAPI As Boolean
Public Property Get TotalPhysical() As Double Refresh If mOldAPI Then TotalPhysical = Mstatus.dwTotalPhys Else TotalPhysical = LI2DBL(mStatusEx.ulTotalPhys.LoPart, mStatusEx.ulTotalPhys.HiPart) End If End Property Public Property Get AvailPhysical() As Double Refresh If mOldAPI Then AvailPhysical = Mstatus.dwAvailPhys Else AvailPhysical = LI2DBL(mStatusEx.ulAvailPhys.LoPart, mStatusEx.ulAvailPhys.HiPart) End If End Property Public Property Get TotalPageFile() As Double Refresh If mOldAPI Then TotalPageFile = Mstatus.dwTotalPageFile Else TotalPageFile = LI2DBL(mStatusEx.ulTotalPageFile.LoPart, mStatusEx.ulTotalPageFile.HiPart) End If End Property Public Property Get AvailPageFile() As Double Refresh If mOldAPI Then AvailPageFile = Mstatus.dwAvailPageFile Else AvailPageFile = LI2DBL(mStatusEx.ulAvailPageFile.LoPart, mStatusEx.ulAvailPageFile.HiPart) End If End Property
Public Property Get TotalVirtual() As Double Refresh If mOldAPI Then TotalVirtual = Mstatus.dwTotalVirtual Else TotalVirtual = LI2DBL(mStatusEx.ulTotalVirtual.LoPart, mStatusEx.ulTotalVirtual.HiPart) End If End Property Public Property Get AvailVirtual() As Double Refresh If mOldAPI Then AvailVirtual = Mstatus.dwAvailVirtual Else AvailVirtual = LI2DBL(mStatusEx.ulAvailVirtual.LoPart, mStatusEx.ulAvailVirtual.HiPart) End If End Property Public Property Get MemoryLoad() As Long Refresh If mOldAPI Then MemoryLoad = Mstatus.dwMemoryLoad Else MemoryLoad = mStatusEx.MemoryLoad End If End Property
Private Sub Refresh() On Error GoTo notSupported ZeroMemory mStatusEx, Len(mStatusEx) mStatusEx.Length = Len(mStatusEx) If GlobalMemoryStatusEx(mStatusEx) = 0 Then GoTo notSupported Else End If
EXIT Sub
notSupported: mOldAPI = True 'darn, we need to use the old API. ZeroMemory Mstatus, Len(Mstatus) GlobalMemoryStatus Mstatus End Sub Private Sub DBL2LI(ByVal Dbl As Double, ByRef LoPart As Long, ByRef HiPart As Long) Dim mungec As MungeCurr Dim mungeli As MungeLong mungec.CurrA = (Dbl / 10000#) LSet mungeli = mungec LoPart = mungeli.LongA HiPart = mungeli.LongB End Sub
Private Function LI2DBL(LoPart As Long, HiPart As Long) As Double Dim mungel As MungeLong Dim mungec As MungeCurr mungel.LongA = LoPart mungel.LongB = HiPart LSet mungec = mungel LI2DBL = mungec.CurrA * 10000#
End Function
Now, to use it:
Code: [Select]Dim memobj as CMemState,totalmem as Double Set memobj = new CMemState totalmem = memobj.TotalPhysical
You'll note also that there are other properties other then the totalphysical memory, such as the size of the pagefile and the total available memory and so forth.
Hope this helps.It worked WELL. I also used a script to TURN the bytes into megabytes and used a round-up script.yes, it returns bytes, of course; just a quick division by 1024 gives you kb and again by 1024 gives you MB.
|