Win32RamUsage: Displays Ram Usage
M. Gallant 11/15/2000
The ability to create objects from classes in the Java API
can be used to extend the capability of Windows scripts.
Since the MS Java API is resident on any PC with IE4+ installed,
developers can leverage the capability contained therein.
The following script instantiates a Java object which contains
a method Win32RamUsage.availMemory()
to return basic RAM usage and availability. J/Direct is used from
Java to access the native win32 struct MEMORYSTATUS which contains
the RAM usage information.
To use the Java class, compile the Java source code using the MS
jvc compiler, and place the
resultant Win32RamUsage.class and MEMORYSTATUS.class
class files in the classpath (for example: C:\Windows\Java\Classes)
Win32RamUsage
'****************************************************************
' File: WinRamUsage.vbs (WSH for VBscript)
' Author: (c) M. Gallant 11/15/2000
'
' Displays Win32 Ram statistics
' Uses Java class "Win32RamUsage" which must be in classpath.
'****************************************************************
Option Explicit
Const ttext = "Win32 Ram Usage -- M. Gallant"
Dim WshShell, oJRam
DIM number, raminfo, again
set WshShell = WScript.CreateObject("WScript.Shell")
set oJRam = GetObject("java:Win32RamUsage") 'Instantiate Java object via moniker.
Do While True
raminfo = oJRam.availMemory()
again = WshShell.Popup(raminfo & vbCrLf & vbCrLf & "Continue?",0, _
ttext, vbInformation + vbYesNo)
If again = vbNo Then Exit Do
Loop
'----------------- End Script -------------------------------
Win32RamUsage.java (Java class)
/* class MemoryWin32 access native method to return RAM usage and available via J/Direct.
M. Gallant 08/10/2000 */
import java.awt.*;
import com.ms.dll.DllLib;
import com.ms.security.*;
public class Win32RamUsage{
protected MEMORYSTATUS mstatus;
public Win32RamUsage() {
mstatus = new MEMORYSTATUS() ;
}
public String availMemory() {
GlobalMemoryStatus(mstatus);
String inf = "Physical memory = " + Integer.toString(mstatus.dwTotalPhys) + "\r\n" ;
inf += "Available memory = " + Integer.toString(mstatus.dwAvailPhys) + "\r\n" ;
return inf ;
}
/** @dll.import("KERNEL32") */
static native void GlobalMemoryStatus(MEMORYSTATUS lptMemStat);
}
/** @dll.struct() */
class MEMORYSTATUS {
public int dwLength = DllLib.sizeOf(MEMORYSTATUS.class);
public int dwMemoryLoad;
public int dwTotalPhys;
public int dwAvailPhys;
public int dwTotalPageFile;
public int dwAvailPageFile;
public int dwTotalVirtual;
public int dwAvailVirtual;
}