BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 Subscribe!

Sunday, January 28, 2007
PInvoke WIN32 and .NET

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: , , ,

posted by Brad Prendergast at 12:16:00 AM
Comments:
Links to this post:

Create a Link

Recent Posts
 ASP.NET upload a file HtmlInputFile Control
 PPCTL.DLL is damaged and could not be repaired.
 Google Mobile SMS
 Running a .NET Framework application from a networ...
 I am Green Lantern!
 More Hash - Part II
 More Hash - Part I
 Is it a Hash Brown?
 Virtualization is cool
 TShellExecuteInfo

 Subscribe!


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger