Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 8b1f986

Browse files
madewokherdrolfbjarne
authored andcommitted
Implement ComputerInfo Memory attributes on Windows. (#70)
1 parent 101fe89 commit 8b1f986

File tree

1 file changed

+53
-4
lines changed
  • vbruntime/Microsoft.VisualBasic/Microsoft.VisualBasic.Devices

1 file changed

+53
-4
lines changed

vbruntime/Microsoft.VisualBasic/Microsoft.VisualBasic.Devices/ComputerInfo.vb

+53-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727
'
2828
#If TARGET_JVM = False Then 'OSVersion Not Supported by Grasshopper
29+
Imports System.ComponentModel
2930
Imports System.Globalization
3031
Imports System.Diagnostics
32+
Imports System.Runtime.InteropServices
3133

3234
Namespace Microsoft.VisualBasic.Devices
3335
<DebuggerTypeProxy(GetType(ComputerInfo.ComputerInfoDebugView))> _
@@ -36,17 +38,56 @@ Namespace Microsoft.VisualBasic.Devices
3638
'Empty
3739
End Sub
3840

41+
Private Function IsOnWindows() As Boolean
42+
Return Environment.OSVersion.Platform <> PlatformID.Unix AndAlso Environment.OSVersion.Platform <> 128
43+
End Function
44+
45+
<StructLayout(LayoutKind.Sequential)>
46+
Private Structure MEMORYSTATUSEX
47+
Public dwLength As Integer
48+
Public dwMemoryLoad As Integer
49+
Public ullTotalPhys As ULong
50+
Public ullAvailPhys As ULong
51+
Public ullTotalPageFile As ULong
52+
Public ullAvailPageFile As ULong
53+
Public ullTotalVirtual As ULong
54+
Public ullAvailVirtual As ULong
55+
Public ullAvailExtendedVirtual As ULong
56+
End Structure
57+
58+
<DllImport("kernel32", CallingConvention:=CallingConvention.StdCall)>
59+
Private Shared Function GlobalMemoryStatusEx(ByRef buf As MEMORYSTATUSEX) As Boolean
60+
End Function
61+
62+
Private Function GetMemoryInfo() As MEMORYSTATUSEX
63+
Dim buf As New MEMORYSTATUSEX()
64+
buf.dwLength = Marshal.SizeOf(buf)
65+
If GlobalMemoryStatusEx(buf) Then
66+
Return buf
67+
Else
68+
Throw New Win32Exception()
69+
End If
70+
End Function
71+
3972
<CLSCompliant(False)> _
4073
Public ReadOnly Property AvailablePhysicalMemory() As ULong
4174
Get
42-
Throw New NotImplementedException()
75+
If IsOnWindows() Then
76+
Return GetMemoryInfo().ullAvailPhys
77+
Else
78+
Throw New NotImplementedException()
79+
End If
4380
End Get
4481
End Property
4582

4683
<CLSCompliant(False)> _
4784
Public ReadOnly Property AvailableVirtualMemory() As ULong
4885
Get
49-
Throw New NotImplementedException()
86+
If IsOnWindows() Then
87+
Return GetMemoryInfo().ullAvailVirtual
88+
Else
89+
Throw New NotImplementedException()
90+
End If
5091
End Get
5192
End Property
5293

@@ -77,14 +118,22 @@ Namespace Microsoft.VisualBasic.Devices
77118
<CLSCompliant(False)> _
78119
Public ReadOnly Property TotalPhysicalMemory() As ULong
79120
Get
80-
Throw New NotImplementedException()
121+
If IsOnWindows() Then
122+
Return GetMemoryInfo().ullTotalPhys
123+
Else
124+
Throw New NotImplementedException()
125+
End If
81126
End Get
82127
End Property
83128

84129
<CLSCompliant(False)> _
85130
Public ReadOnly Property TotalVirtualMemory() As ULong
86131
Get
87-
Throw New NotImplementedException()
132+
If IsOnWindows() Then
133+
Return GetMemoryInfo().ullTotalVirtual
134+
Else
135+
Throw New NotImplementedException()
136+
End If
88137
End Get
89138
End Property
90139

0 commit comments

Comments
 (0)