2012-07-04 52 views
1

我想通過Visual Basic獲取我的PC上安裝的物理內存(總RAM)的數量。問題是我得到的返回「0字節」。此外,我還會嘗試喲得到使用的百分比,可用RAM的數量,總分頁,免費頁面和一個圖形顯示的使用情況,如資源監視器的RAM,在Windows.The問題是,我不能得到正確數量的可用RAM的第一與其他人一起前進。Visual Basic物理內存

我在做什麼錯了? 謝謝。

這是我的代碼:

Option Strict On 
Option Explicit On 
Imports System.Math 
Imports System.Management 
Imports System.Runtime.InteropServices 
Public Class Form1 
    Inherits System.Windows.Forms.Form 
#Region " API " 
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _ 
    Private Structure MEMORYSTATUSEX 
     Dim dwLength As Integer 
     Dim dwMemoryLoad As Integer 
     Dim ullTotalPhys As ULong 
    End Structure 
    Private memoryInfo As MEMORYSTATUSEX 
    Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32" (ByRef lpBuffer As MEMORYSTATUSEX) 
#End Region 


#Region " Variables " 

    Private mullTotalRAM As ULong 

#End Region 

#Region " Form Events " 
    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load 
     ' set title 
     Me.Text = My.Application.Info.Title & " " & My.Application.Info.Version.Major.ToString & "." & _ 
      My.Application.Info.Version.Minor.ToString 

     Application.DoEvents() 
     GetMemoryInfo() 
     Timer1.Enabled = True 
    End Sub 

#End Region 

#Region " Information Gathering and Display " 

    Private Sub GetMemoryInfo() 

     System.Windows.Forms.Application.DoEvents() 

     ' set size of structure (required by this api call) 
     memoryInfo.dwLength = Marshal.SizeOf(memoryInfo) 
     GlobalMemoryStatusEx(memoryInfo) 

     mullTotalRAM = memoryInfo.ullTotalPhys 

     txtRAM.Text = FormatBytes(mullTotalRAM) 

    End Sub 

#End Region 

#Region " Update Timer " 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) 

     GetMemoryInfo() 

     Application.DoEvents() 

    End Sub 

#End Region 


#Region " Formatting Routines " 

    Private Function FormatBytes(ByVal ullBytes As ULong) As String 
     Dim dblTemp As Double 

     Try 
      Select Case ullBytes 
       Case Is >= 1073741824 'GB 
        dblTemp = CDbl(ullBytes/1073741824) 
        Return FormatNumber(dblTemp, 2) & " GB" 
       Case 1048576 To 1073741823 
        dblTemp = CDbl(ullBytes/1048576) 'MB 
        Return FormatNumber(dblTemp, 0) & " MB" 
       Case 1024 To 1048575 
        dblTemp = CDbl(ullBytes/1024) 'KB 
        Return FormatNumber(dblTemp, 0) & " KB" 
       Case 0 To 1023 
        dblTemp = ullBytes ' bytes 
        Return FormatNumber(dblTemp, 0) & " bytes" 
       Case Else 
        Return "" 
      End Select 
     Catch 
      Return "" 
     End Try 

    End Function 


#End Region 


    Private Sub ramaTotalRAM_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ramaTotalRAM.Enter 

    End Sub 

    Private Sub txtRAM_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRAM.TextChanged 

    End Sub 
End Class 

我已經解決了這個problem.Now我想知道是否有可能讓這樣的事情: http://s18.postimage.org/7zn5adst3/Memory.jpg。怎麼能我也很it.thank你許多。

+0

您運行的是什麼操作系統? –

+0

這不是VB,但也許VB.Net。 – Bob77

+0

@Bob Riemersma:我使用Visual Studio 2008,這是一個應用程序的.vb(「Form1.vb的」這個名字吧)。 –

回答

2

一些簡單的單套來完成這項工作:

TotalPhysicalMemory

MsgBox(String.Format("TotalPhysicalMemory: {0} MBytes", System.Math.Round(My.Computer.Info.TotalPhysicalMemory/(1024 * 1024)), 2).ToString) 

AvailablePhysicalMemory

MsgBox(String.Format("AvailablePhysicalMemory: {0} MBytes", System.Math.Round(My.Computer.Info.AvailablePhysicalMemory/(1024 * 1024)), 2).ToString) 

TotalVirtualMemory

MsgBox(String.Format("TotalVirtualMemory: {0} MBytes", System.Math.Round(My.Computer.Info.TotalVirtualMemory/(1024 * 1024)), 2).ToString) 

AvailableVirtualMemory

MsgBox(String.Format("AvailableVirtualMemory: {0} MBytes", System.Math.Round(My.Computer.Info.AvailableVirtualMemory/(1024 * 1024)), 2).ToString) 

DECL更改API這個(通知,.dll在你的代碼失蹤)

Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32.dll" (ByRef lpBuffer As MEMORYSTATUSEX) 
+0

通過更改API聲明「kernel32.dll」它仍然不會工作。上面的代碼,是的,它的工作原理,非常感謝你。我也嘗試了一個更早。我仍然不明白爲什麼我的方法贏得' t工作,因爲在我用作模型的程序中,它的工作原理就像是一個魅力。我仍然從最後一個case獲得0個字節。如果我刪除了case 0到1023的那部分,它在運行時不會返回任何內容箱子是空的,我仍然無法弄清楚。非常感謝。 –

0

這裏是工作示例的pastebin

我能得到它的工作通過使用GlobalMemoryStatusEx這個定義和下面MEMORYSTATUSEX

Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32" (<[In](), Out()> lpBuffer As MEMORYSTATUSEX) 

,並宣佈它像

Private memoryInfo As MEMORYSTATUSEX = New MEMORYSTATUSEX 

和註釋掉

'memoryInfo.dwLength = CUInt(Marshal.SizeOf(memoryInfo)) 

我不知道你在哪裏得到你的結構定義MEMORYSTATUSEX,但根據Pinvoke.net它應該是。

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _ 
Public Class MEMORYSTATUSEX 

    ''' <summary> 
    ''' Initializes a new instance of the <see cref="T:MEMORYSTATUSEX" /> class. 
    ''' </summary> 
    Public Sub New() 
    Me.dwLength = CType(Marshal.SizeOf(GetType(MEMORYSTATUSEX)), UInt32) 
    End Sub 
    ' Fields 
    ''' <summary> 
    ''' Size of the structure, in bytes. You must set this member before calling GlobalMemoryStatusEx. 
    ''' </summary> 
    Public dwLength As UInt32 
    ''' <summary> 
    ''' 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). 
    ''' </summary> 
    Public dwMemoryLoad As UInt32 
    ''' <summary> 
    ''' Total size of physical memory, in bytes. 
    ''' </summary> 
    Public ullTotalPhys As UInt64 
    ''' <summary> 
    ''' Size of physical memory available, in bytes. 
    ''' </summary> 
    Public ullAvailPhys As UInt64 
    ''' <summary> 
    ''' Size of the committed memory limit, in bytes. This is physical memory plus the size of the page file, minus a small overhead. 
    ''' </summary> 
    Public ullTotalPageFile As UInt64 
    ''' <summary> 
    ''' Size of available memory to commit, in bytes. The limit is ullTotalPageFile. 
    ''' </summary> 
    Public ullAvailPageFile As UInt64 
    ''' <summary> 
    ''' Total size of the user mode portion of the virtual address space of the calling process, in bytes. 
    ''' </summary> 
    Public ullTotalVirtual As UInt64 
    ''' <summary> 
    ''' Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes. 
    ''' </summary> 
    Public ullAvailVirtual As UInt64 
    ''' <summary> 
    ''' Size of unreserved and uncommitted memory in the extended portion of the virtual address space of the calling process, in bytes. 
    ''' </summary> 
    Public ullAvailExtendedVirtual As UInt64 
End Class 

您也可以看看Microsoft.VisualBasic.Devices.ComputerInfo

Dim info As Microsoft.VisualBasic.Devices.ComputerInfo = New Microsoft.VisualBasic.Devices.ComputerInfo 
Debug.Print(CStr(info.TotalPhysicalMemory)) 
Debug.Print(CStr(info.TotalVirtualMemory)) 
Debug.Print(CStr(info.AvailablePhysicalMemory)) 
+0

我有一個類似的應用程序,它使用MEMORYSTATUSEX,它的工作原理,它顯示我適量的RAM,但在我的情況下,它不會工作。 –

+0

我剛剛完成了顯示可用RAM的部分,結果仍爲「0字節」。在我在網上找到的類似應用程序中,它顯示了正確的數量。是關於顯示輸出的對話框的內容。被設置爲只讀:true,accept返回:true,修飾符:public。 –

+0

@TeoRoxy添加了一些額外的信息。我無法讓您的代碼使用您的結構工作,我使用了我發佈的那個代碼,甚至必須更改GlobalMemoryStatusEX聲明。所以上面的工作,我不知道你爲什麼不工作。 –

0
Friend Sub ReleaseMemory() 
     Try 
      GC.Collect() 
      GC.WaitForPendingFinalizers() 
      If Environment.OSVersion.Platform = PlatformID.Win32NT Then 
       SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1) 
      End If 
     Catch ex As Exception 
      LogError(ex.ToString()) 
     End Try 
    End Sub