B4J Question Shell problem

TheRealMatze

Active Member
Licensed User
Hello there,
i want to query some system informations and want to use wmi. It looks like there is no direct way to do this in b4j so i decide to use the shell and the wmic-command.
For example - when i want to get the cpu load i can call
B4X:
wmic cpu get loadpercentage
This works fine from the commandline, but when i call it from the shell it failed
B4X:
Dim shl As Shell
    shl.Initialize("shl", "wmic", Array("cpu get loadpercentage"))

I have tried multiple versions like
B4X:
Dim shl As Shell
    shl.Initialize("shl", "wmic", Array("cpu get loadpercentage"))
Returns: cpu get loadpercentage - Alias wurde nicht gefunden.

B4X:
shl.Initialize("shl", "wmic ", NULL)
Returns: org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "wmic cpu get loadpercentage" (in directory "."): CreateProcess error=2, Das System kann die angegebene Datei nicht finden)

all failed, but when i only call the wmic
B4X:
Dim shl As Shell
    shl.Initialize("shl", "wmic", NULL)
i got "wmic:root\cli>" - so wmic is found.

Can anybody help me?

Regards
Matze
 

PaulMeuris

Active Member
Licensed User
You could try this:
powershell to the rescue:
    Label1.Text = ""
    Dim shl As Shell
    Private arguments() As String = Array As String("wmic cpu get loadpercentage > output.txt")
    shl.Initialize("shl", "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe", arguments)
    shl.WorkingDirectory = "E:\_temp"
    shl.Run(10000) 'set a timeout of 10 seconds
    ' check the output
    Dim result As String = File.ReadString("E:\_temp","output.txt")
    Label1.Text = result
Use your own working directory for the output.txt file.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
shl.Initialize("shl", "wmic", Array("cpu", "get", "loadpercentage"))
Would that work?



1661235722035.png
 
Last edited:
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0

TheRealMatze

Active Member
Licensed User
Perfect!

B4X:
Sub Process_Globals
    Dim tmrCpuLoad As Timer
End Sub

Sub AppStart (Args() As String)
    Dim sys As JavaObject
    sys.InitializeStatic("java.lang.System")
    tmrCpuLoad.Initialize("tmrCpuLoad", 5000)
    tmrCpuLoad.Enabled = True
    StartMessageLoop
End Sub

Sub RunProcess
    Try
    Dim shl As Shell
    shl.Initialize("shl", "wmic", Array("cpu", "get", "loadpercentage"))
    shl.Run(-1)
    Wait For (shl) shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        Dim returnValue() As String=Regex.Split(Chr(13) & Chr(10),StdOut)
        Log(returnValue(1))
    Catch
        Log(0)
    End Try
    tmrCpuLoad.Enabled=True    're-enable timer
End Sub

Private Sub tmrCpuLoad_Tick
    tmrCpuLoad.Enabled=False
    RunProcess
End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Perfect!

B4X:
Sub Process_Globals
    Dim tmrCpuLoad As Timer
End Sub

Sub AppStart (Args() As String)
    Dim sys As JavaObject
    sys.InitializeStatic("java.lang.System")
    tmrCpuLoad.Initialize("tmrCpuLoad", 5000)
    tmrCpuLoad.Enabled = True
    StartMessageLoop
End Sub

Sub RunProcess
    Try
    Dim shl As Shell
    shl.Initialize("shl", "wmic", Array("cpu", "get", "loadpercentage"))
    shl.Run(-1)
    Wait For (shl) shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
        Dim returnValue() As String=Regex.Split(Chr(13) & Chr(10),StdOut)
        Log(returnValue(1))
    Catch
        Log(0)
    End Try
    tmrCpuLoad.Enabled=True    're-enable timer
End Sub

Private Sub tmrCpuLoad_Tick
    tmrCpuLoad.Enabled=False
    RunProcess
End Sub

It would be good/nice a "Like" (at least) to those help u like PaulMeuris and LucaMs and selecting their post as a Solution... sometimes a Like can change your day! ...i am not saying their lives :)
 
Upvote 0
Top