The
WIN32 API has been around for quite sometime and has a large number of
'calls' and
'mechanisms' that have not yet been

ported over to the
.NET world. There are also still quite a few valuable WIN32 libraries/exports that have yet to be
'upgraded'. When integration is needed with existing WIN32 applications or APIs from a .NET application the unmanaged WIN32 DLL exports can be accessed using
Platform Invoke (also referred to as PInvoke or P/Invoke). I am in the process of moving pieces of code to use the
.NET framework. One of the first pieces on my list was the About Box class that I use in my personal applications. This base About Box class I created and use through out most (if not all) of my personal applications. The setting of a few properties has the About Box up and running for a new application in moments. In this About Box class I use the
GlobalMemoryStatus function to display virtual and physical memory information. In order to still use this function in my new and 'improved' .NET About Box I needed to use
PInvoke.
C#using System.Runtime.InteropServices;
// define the structure
public struct _MEMORYSTATUS //http://msdn2.microsoft.com/en-us/library/aa366772.aspx
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern void GlobalMemoryStatus(out _MEMORYSTATUS memorystatus);
// for simplicity of demonstration
_MEMORYSTATUS memorystatus = new _MEMORYSTATUS();
GlobalMemoryStatus(out memorystatus);
ulong totalVirtualMemory = memorystatus.dwTotalVirtual;
ulong availablePhysicalMemory = memorystatus.dwAvailPhys;
ulong availableVirtualMemory = memorystatus.dwAvailVirtual;
ulong totalPhysicalMemory = memorystatus.dwTotalPhys; VB.NET
Imports System.Runtime.InteropServices
Public Structure _MEMORYSTATUS
Dim dwLength As Integer
Dim dwMemoryLoad As Integer
Dim dwTotalPhys As Integer
Dim dwAvailPhys As Integer
Dim dwTotalPageFile As Integer
Dim dwAvailPageFile As Integer
Dim dwTotalVirtual As Integer
Dim dwAvailVirtual As Integer
End Structure
'In VB.NET there are two ways to call this
'<DllImport("kernel32.dll")> Public Shared Sub _
' GlobalMemoryStatus1(ByVal memorystatus As _MEMORYSTATUS)
'End Sub
Declare Auto Sub GlobalMemoryStatus Lib "kernel32" (ByRef lpBuffer As _MEMORYSTATUS)
Dim memorystatus As New _MEMORYSTATUS
GlobalMemoryStatus(memorystatus)
Dim totalVirtualMemory As Integer = memorystatus.dwTotalVirtual
Dim availablePhysicalMemory As Integer = memorystatus.dwAvailPhys
Dim availableVirtualMemory As Integer = memorystatus.dwAvailVirtual
Dim totalPhysicalMemory As Integer = memorystatus.dwTotalPhys
Side Note: After having problems with
Windows Live Writer after I '
upgraded' to the
'new' Blogger I published this blog entry using
Google Docs & Spreadsheets. Curiosity got the best of me and I wanted to see how it worked.
Labels: .NET, C#, Code, VB.NET