Advertisement
Mark_Hall

Memory

Jul 4th, 2012
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.39 KB | None | 0 0
  1. Option Strict On
  2. Option Explicit On
  3. Imports System.Math
  4. Imports System.Management
  5. Imports System.Runtime.InteropServices
  6.  
  7. Public Class Form1
  8. #Region " API "
  9.     Private memoryInfo As MEMORYSTATUSEX = New MEMORYSTATUSEX
  10.     Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32" (<[In](), Out()> lpBuffer As MEMORYSTATUSEX)
  11. #End Region
  12.  
  13. #Region " Variables "
  14.  
  15.     Private mullTotalRAM As ULong
  16.  
  17. #End Region
  18.  
  19. #Region " Form Events "
  20.     Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
  21.         ' set title          
  22.         Me.Text = My.Application.Info.Title & " " & My.Application.Info.Version.Major.ToString & "." & _
  23.             My.Application.Info.Version.Minor.ToString
  24.  
  25.         Application.DoEvents()
  26.         GetMemoryInfo()
  27.         Timer1.Enabled = True
  28.     End Sub
  29.  
  30. #End Region
  31.  
  32. #Region " Information Gathering and Display "
  33.  
  34.     Private Sub GetMemoryInfo()
  35.  
  36.         System.Windows.Forms.Application.DoEvents()
  37.  
  38.         GlobalMemoryStatusEx(memoryInfo)
  39.  
  40.         mullTotalRAM = memoryInfo.ullTotalPhys
  41.  
  42.         txtRAM.Text = FormatBytes(mullTotalRAM)
  43.  
  44.     End Sub
  45.  
  46. #End Region
  47.  
  48. #Region " Update Timer "
  49.  
  50.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  51.  
  52.         GetMemoryInfo()
  53.  
  54.         Application.DoEvents()
  55.  
  56.     End Sub
  57.  
  58. #End Region
  59.  
  60.  
  61. #Region " Formatting Routines "
  62.  
  63.     Private Function FormatBytes(ByVal ullBytes As ULong) As String
  64.         Dim dblTemp As Double
  65.  
  66.         Try
  67.             Select Case ullBytes
  68.                 Case Is >= 1073741824 'GB
  69.                     dblTemp = CDbl(ullBytes / 1073741824)
  70.                     Return FormatNumber(dblTemp, 2) & " GB"
  71.                 Case 1048576 To 1073741823
  72.                     dblTemp = CDbl(ullBytes / 1048576) 'MB
  73.                     Return FormatNumber(dblTemp, 0) & " MB"
  74.                 Case 1024 To 1048575
  75.                     dblTemp = CDbl(ullBytes / 1024) 'KB
  76.                     Return FormatNumber(dblTemp, 0) & " KB"
  77.                 Case 0 To 1023
  78.                     dblTemp = ullBytes ' bytes
  79.                     Return FormatNumber(dblTemp, 0) & " bytes"
  80.                 Case Else
  81.                     Return ""
  82.             End Select
  83.         Catch
  84.             Return ""
  85.         End Try
  86.  
  87.     End Function
  88.  
  89.  
  90. #End Region
  91.  
  92. End Class
  93. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
  94. Public Class MEMORYSTATUSEX
  95.  
  96.     ''' <summary>
  97.     ''' Initializes a new instance of the <see cref="T:MEMORYSTATUSEX" /> class.
  98.     ''' </summary>
  99.     Public Sub New()
  100.         Me.dwLength = CType(Marshal.SizeOf(GetType(MEMORYSTATUSEX)), UInt32)
  101.     End Sub
  102.     ' Fields
  103.     ''' <summary>
  104.     ''' Size of the structure, in bytes. You must set this member before calling GlobalMemoryStatusEx.
  105.     ''' </summary>
  106.     Public dwLength As UInt32
  107.     ''' <summary>
  108.     ''' Number between 0 and 100 that specifies the approximate percentage of physical memory that is in use (0 indicates no memory use and 100 indicates full memory use).
  109.     ''' </summary>
  110.     Public dwMemoryLoad As UInt32
  111.     ''' <summary>
  112.     ''' Total size of physical memory, in bytes.
  113.     ''' </summary>
  114.     Public ullTotalPhys As UInt64
  115.     ''' <summary>
  116.     ''' Size of physical memory available, in bytes.
  117.     ''' </summary>
  118.     Public ullAvailPhys As UInt64
  119.     ''' <summary>
  120.     ''' Size of the committed memory limit, in bytes. This is physical memory plus the size of the page file, minus a small overhead.
  121.     ''' </summary>
  122.     Public ullTotalPageFile As UInt64
  123.     ''' <summary>
  124.     ''' Size of available memory to commit, in bytes. The limit is ullTotalPageFile.
  125.     ''' </summary>
  126.     Public ullAvailPageFile As UInt64
  127.     ''' <summary>
  128.     ''' Total size of the user mode portion of the virtual address space of the calling process, in bytes.
  129.     ''' </summary>
  130.     Public ullTotalVirtual As UInt64
  131.     ''' <summary>
  132.     ''' Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.
  133.     ''' </summary>
  134.     Public ullAvailVirtual As UInt64
  135.     ''' <summary>
  136.     ''' Size of unreserved and uncommitted memory in the extended portion of the virtual address space of the calling process, in bytes.
  137.     ''' </summary>
  138.     Public ullAvailExtendedVirtual As UInt64
  139. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement