/* class MemoryWin32 for IE browsers uses J/Direct to access kernel32.dll functions to read currently available RAM memory. Requires compilation with MS jvc compiler. M. Gallant 09/17/2001 */ import java.awt.*; import com.ms.dll.DllLib; import com.ms.security.*; import com.ms.wfc.util.*; public class MemoryWin32 extends java.applet.Applet implements Runnable { Thread runner; long interval = 500 ; // pause time in milliseconds. private Label l1 = new Label("Total physical memory = -------------------------------------------- ") ; private Label l2 = new Label("Total available memory = -------------------------------------------- ") ;; protected MEMORYSTATUS mstatus; private int memorybar = 0; private int barwidth = 300 ; private int barheight = 20 ; public void init() { mstatus = new MEMORYSTATUS() ; add(l1) ; add(l2) ; } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { runner.stop(); runner = null; } } public void pause(long time) { try { Thread.sleep(time);} // wait for time milliseconds. catch (InterruptedException e) { } } public void run() { try { if (Class.forName("com.ms.security.PolicyEngine") != null) // get permissions PolicyEngine.assertPermission(PermissionID.SYSTEM); while (true) { availMemory() ; pause(interval) ; } } catch (Throwable cnfe) { } } private void availMemory() { GlobalMemoryStatus(mstatus); int totmem = mstatus.dwTotalPhys ; int availmem = mstatus.dwAvailPhys ; double fract = (int)(1000.0*availmem/totmem)/10.0 ; l1.setText("Total physical memory = " + Value.formatNumber(totmem/1024, 0, NumberFormat.GROUP_DIGITS) + " Kb") ; l2.setText("Total available memory = " + Value.formatNumber(availmem/1024, 0, NumberFormat.GROUP_DIGITS) + " Kb [" + fract + " %]") ; memorybar = (int) (1.0*barwidth*availmem/totmem) ; repaint() ; } public void update(Graphics g) { paint(g) ; } public void paint(Graphics g) { g.setColor(Color.black) ; g.drawRect(0,0,this.getSize().width-1, this.getSize().height-1) ; g.setColor(Color.white) ; g.fillRect(20, 80, barwidth, barheight) ; g.setColor(Color.red) ; g.fillRect(20,80, memorybar, barheight) ; g.setColor(Color.black) ; g.drawRect(20, 80, barwidth, barheight) ; } /** @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; }