trip down Memory Lane

Started by spl, Today at 09:43:28 AM

Previous topic - Next topic

spl

Asked for a favor for a WB function to display available memory on a PC. After my initial 'So what?'... they explained reasons. I figured it would be a simple WMI query, but the code below returns 0 whether run in 32 or 64 bit.
;Winbatch 2025 - Available Memory
;it is WMI so should work with earlier versions
strComputer = "."
oWMI = GetObject( "winmgmts:\\" : strComputer : "\root\cimv2")
WQL = "SELECT * FROM  Win32_OperatingSystem" 
oP= oWMI.ExecQuery(WQL)
decimals(2)
ForEach p in oP
  total = p.TotalVisibleMemorySize
  free = p.FreePhysicalMemory
  available = (((total - free) / total) * 100)
  Message("Memory",available)
Next   
oP=0
oWMI=0
Exit

which only returns 0, no errors.

A little perplexed I just ran it as PS code through Std_Out() and get results. I am sure WB can probably do it, just not in a direct way.

;Std_out guess at available memory
;Stan Littlefield 9/2/2026
;========================================================================================================================
Gosub udfs
args = $"$Memory = Get-WmiObject -Class Win32_OperatingSystem
$total = $Memory.TotalVisibleMemorySize
$free = $Memory.FreePhysicalMemory
$available = [Math]::::Round(($total - $free) / $total * 100,2)
$available
$"

cmd="Powershell"
msg='Memory Check'
BoxOpen(msg,cmd:" ":args:@LF:"PLEASE WAIT...MAY TAKE SOME TIME")
TimeDelay(2)
vals = Get_stdout()
BoxShut()
Message("Available Memory %%",vals)
Exit
;======================================================================
:udfs
#DefineSubroutine Get_stdout()
ObjectClrOption("useany","System")
objInfo = ObjectClrNew("System.Diagnostics.ProcessStartInfo")
Output=""
timeOut = ObjectType("I2",5000)
objInfo.FileName = cmd
objInfo.RedirectStandardError = ObjectType("BOOL",@TRUE)
objInfo.RedirectStandardOutput = ObjectType("BOOL",@TRUE)
objInfo.UseShellExecute = ObjectType("BOOL",@FALSE)
objInfo.CreateNoWindow = ObjectType("BOOL",@TRUE)
objInfo.Arguments = args
oProcess = ObjectClrNew("System.Diagnostics.Process")
oProcess.StartInfo = objInfo
oProcess.Start()
oProcess.WaitForExit(timeout)
STDOUT = oProcess.StandardOutput.ReadToEnd()
STDERR = oProcess.StandardError.ReadToEnd()
Output = Output:STDOUT:@CRLF
;If STDERR<>""
;  Output = Output:"STDERR:":STDERR:@CRLF
;Endif
oProcess = 0
objInfo = 0

Return (Output)
#EndSubroutine
Return
;==========================================================
Stan - formerly stanl [ex-Pundit]

JTaylor

You need to force type for the division to show properly.

total = p.TotalVisibleMemorySize + 0.00
free = p.FreePhysicalMemory + 0.00

SMF spam blocked by CleanTalk