1

PC にインストールされている物理メモリの量 (合計 RAM) を Visual Basic で取得しようとしています。問題は、"0 バイト" が返されることです。空き RAM の量、合計ページング、空きページ、および Windows の RAM のリソース モニターのような使用状況を示すグラフ。

私が間違っていることは何ですか?ありがとうございました。

これは私のコードです:

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

私はこの問題を解決しました。今、次のようなものを作ることができるかどうか疑問に思っています : http://s18.postimage.org/7zn5adst3/Memory.jpg

4

3 に答える 3

3

仕事を成し遂げるためのいくつかの簡単なシングルライナー:

合計物理メモリ

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

利用可能な物理メモリ

    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)

利用可能な仮想メモリ

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

また

API宣言をこれに変更.dllします(コードにないことに注意してください)

Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32.dll" (ByRef lpBuffer As MEMORYSTATUSEX)
于 2012-07-04T06:56:34.430 に答える
0

これが実例のペーストビンです

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)) 

構造定義をどこで入手したかはわかりませんが、MEMORYSTATUSEXPinvoke.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))
于 2012-07-04T06:22:53.203 に答える