SystemInfoViewer [Win Only]

Share your advanced PureBasic knowledge/code with the community.
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

SystemInfoViewer [Win Only]

Post by Axolotl »

I like the RunProgram() function and I use it a lot.
Here comes an (two) example(s) with the commandline command SystemInfo:

Code: Select all

Procedure DebugSystemInfo()  
  Protected NP, result, stdout.s, name.s, value.s, p1    

  NP = RunProgram("cmd.exe", "/C systeminfo", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide) 
  If NP
    Debug "SystemInfo Results " 
    While ProgramRunning(NP) 
      If AvailableProgramOutput(NP) 
        stdout = ReadProgramString(NP)    ; : Debug "OUT: '" + stdout + "'" 
        If stdout ; is valid 
          If Asc(stdout) = ' ' 
            name  = ""
            value = Mid(stdout, 28)       ; move the value to the left 
          Else  
            p1 = FindString(stdout, ":", 1) 
            If p1 
              name  = Left(stdout, p1 - 1)      
              value = Trim(Mid(stdout, p1 + 1))   
            EndIf 
          EndIf 
          Debug "  " + LSet(name, 26) + " = " + value 
        EndIf 
      EndIf ; AvailableProgramOutput 
    Wend 
    Debug "Done." 
    Debug "" 
    CloseProgram(NP)                            ; Close the connection to the program
  EndIf 
EndProcedure 

DebugSystemInfo() 
If you want this as a complete tool, you should try this code:

Code: Select all

;/=====================================================================================================================
;|
;|  FILE  : SystemInfoViewer.pb 
;|
;|  
;|
;| License    : Free, unrestricted, no warranty whatsoever - Use at your own risk 
;|
;|            : Copyright (c) by Axolotl - All Rights reserved. 
;|
;\=====================================================================================================================

EnableExplicit 

; ---------------------------------------------------------------------------------------------------------------------

#ProgramName      = "SystemInfoViewer" 
#ProgramVersion   = "0." + #PB_Editor_BuildCount + "." + #PB_Editor_CompileCount 

#ProgramCaption     = "System Info Viewer " 
#ProgramMainCaption = #ProgramCaption + " " + #ProgramVersion 

#ProgramPreferencesName = #ProgramName + ".prefs"   ; not used by now 

#ProgramMainWindowFlags = #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget

CompilerIf Not #PB_Compiler_Thread 
  CompilerWarning "Enable thread safe option!"
CompilerEndIf

; ---------------------------------------------------------------------------------------------------------------------

Enumeration EWindow 1 ; my standard usage 
  #WND_Main 
EndEnumeration 

Enumeration EGadget 1 
  #GDT_lblInfo 
  #GDT_btnCancel 
  #GDT_lstResult 
EndEnumeration 

Enumeration EFont 1 
  #FNT_Monospace 
EndEnumeration 

Enumeration ECustomEvent #PB_Event_FirstCustomValue 
  #EVT_ThreadFinished   ; fired when thread has finished. 
EndEnumeration 

; ---------------------------------------------------------------------------------------------------------------------

Structure TSystemInfoData 
  Thread.i              ; <- The Thread ... used in main program with e.g. WaitThread() 
  TerminateNow.i        ; <- "Signal" from Main Program 
  ; 
  List OutputLines.s()   ; <- the output => Name + LF + Value 
EndStructure 

; ---------------------------------------------------------------------------------------------------------------------
;
; Global System Info ..... 
;
Global SystemInfo.TSystemInfoData 

; ---------------------------------------------------------------------------------------------------------------------

Procedure ConfirmBox(Message.s)  ; BOOL 
  Protected result, flags  

  flags = #PB_MessageRequester_Info | #PB_MessageRequester_YesNo 
  If MessageRequester(#ProgramCaption, Message, flags) = #PB_MessageRequester_Yes 
    result = #True 
  EndIf 
  ProcedureReturn result 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure ReadSystemInfo_WorkerThread(*SID.TSystemInfoData) 
  Protected NP, result, stdout.s, name.s, value.s, p1  

  ClearList(*SID\OutputLines())   ; start with an empty list 

  NP = RunProgram("cmd.exe", "/C systeminfo", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide) 
  If NP 
    While ProgramRunning(NP) And *SID\TerminateNow = #False 
      If AvailableProgramOutput(NP) 
        stdout = ReadProgramString(NP)    ; : Debug "OUT: '" + stdout + "'" 
        If stdout ; is valid 
          If Asc(stdout) = ' ' 
            name  = ""
            value = Mid(stdout, 28)       ; move the value to the left 
          Else  
            p1 = FindString(stdout, ":", 1) 
            If p1 
              name  = Left(stdout, p1 - 1)      
              value = Trim(Mid(stdout, p1 + 1))   
            EndIf 
          EndIf 
          AddElement(*SID\OutputLines()) 
          *SID\OutputLines() = name + #LF$ + value 
        EndIf 
      EndIf ; AvailableProgramOutput 
    Wend 

    If *SID\TerminateNow <> #False 
      KillProgram(NP)  
    EndIf 

    CloseProgram(NP)                            ; Close the connection to the program
  EndIf 
  PostEvent(#EVT_ThreadFinished) 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure MainWindowOnSizeEvent() 
  Protected ww, wh 

  ww = WindowWidth(#WND_Main)
  wh = WindowHeight(#WND_Main) 

  ResizeGadget(#GDT_lblInfo,   #PB_Ignore, #PB_Ignore, ww-96,      #PB_Ignore) 
  ResizeGadget(#GDT_lstResult, #PB_Ignore, #PB_Ignore, ww,         wh) 
  ResizeGadget(#GDT_btnCancel, ww-80,      #PB_Ignore, #PB_Ignore, #PB_Ignore) 

  ; adjust column 1 
  ww - GetGadgetItemAttribute(#GDT_lstResult, 0, #PB_ListIcon_ColumnWidth, 0) ; get width of column 0 
  SetGadgetItemAttribute(#GDT_lstResult, 0, #PB_ListIcon_ColumnWidth, ww-28, 1) ; set width of column 1 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure CreateMainWindow() 

  If OpenWindow(#WND_Main, 0, 0, 640, 400, #ProgramMainCaption, #ProgramMainWindowFlags) 
    TextGadget(#GDT_lblInfo, 8, 22, 240, 80, "Please wait while processing ..... "+#LF$+#LF$+"Thanks for trying it out.") 
    ButtonGadget(#GDT_btnCancel, 560, 20, 76, 24, "Cancel") 

    ListIconGadget(#GDT_lstResult, 0, 0, 640, 400, "Name", 160, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
      AddGadgetColumn(#GDT_lstResult, 1, "Value", 640-160-28) 

    LoadFont(#FNT_Monospace, "Consolas", 8) 
    SetGadgetFont(#GDT_lstResult, FontID(#FNT_Monospace)) 

    HideGadget(#GDT_lstResult, 1) 

    BindEvent(#PB_Event_SizeWindow, @MainWindowOnSizeEvent(), #WND_Main) 
  EndIf 
  ProcedureReturn IsWindow(#WND_Main) 
EndProcedure 

; ---------------------------------------------------------------------------------------------------------------------

Procedure Main() 

  If CreateMainWindow() 

    ; start the thread now 
    ;
    SystemInfo\Thread = CreateThread(@ReadSystemInfo_WorkerThread(), @SystemInfo) 

    Repeat
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow ; <<<<<<<<<<<<<<< 

          If Not WaitThread(SystemInfo\Thread, 1000) ; timed out 
            If ConfirmBox("Reading of SystemInfo is not finished, yet." + #LF$ + "Terminate it now?") 
              SystemInfo\TerminateNow = #True 
            EndIf 
          Else 
            Break ; bye 
          EndIf 

        Case #PB_Event_SizeWindow ; <<<<<<<<<<<<<<<
          Debug "  Resize Window to " + WindowWidth(#WND_Main) + ", " + WindowHeight(#WND_Main) 

        Case #EVT_ThreadFinished ; <<<<<<<<<<<<<<<

          ; append the lines to the gadget 
          ;
          ForEach(SystemInfo\OutputLines()) 
            AddGadgetItem(#GDT_lstResult, -1, SystemInfo\OutputLines()) 
          Next 

          ; Update UI 
          ;
          HideGadget(#GDT_lblInfo,   1) 
          HideGadget(#GDT_btnCancel, 1) 
          HideGadget(#GDT_lstResult, 0) 

      EndSelect ; WaitWindowEvent 
    ForEver 
  EndIf
  ProcedureReturn 0 
EndProcedure 

End Main() 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: SystemInfoViewer [Win Only]

Post by Quin »

Very cool, and today I learned about the systeminfo command. Thanks for sharing 8)
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: SystemInfoViewer [Win Only]

Post by Axolotl »

Me too, and thanks for the feedback.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Post Reply