wmic small GUI

Share your advanced PureBasic knowledge/code with the community.
Marc56us
Addict
Addict
Posts: 1005
Joined: Sat Feb 08, 2014 3:26 pm

wmic small GUI

Post by Marc56us »

Hi all :D

Starting from an idea of tatanas on the use of WMI request on network, a small test interface just to demonstrate (to new PB users) how to encapsulate the Windows command wmic.exe
Base topic: viewtopic.php?p=544243#p544243

The program does not process input errors, it is just a minimum interface.

The interface was generated with the form designer and then cut and pasted into a single file. Re-separate if you want to modify easily.

It is necessary that the server machine is configured to accept remote requests, DCOM and that its firwall lets out these protocols

Server can be IP or hostname
Username and Password must exist on server an must be administrator.

Code: Select all

; WMIC GUI
; Marc56 2019/11/07 - Based on a suggestion from tatanas (using wmic.exe)
; https://www.purebasic.fr/english/viewtopic.php?p=544194#p544194

EnableExplicit

SetGadgetFont(#PB_Default, FontID(LoadFont(#PB_Any, "Consolas", 9)))

;XIncludeFile "WMIC_GUI.pbf"
; -------------------------------------------------------------------------------------
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Enumeration FormWindow
  #Window_0
EndEnumeration

Enumeration FormGadget
  #Txt_Server
  #Txt_User
  #Txt_Password
  #Txt_Query
  #Str_Server
  #Str_User
  #Str_Password
  #Str_Query
  #Btn_Send_Query
  #Editor
  #Btn_Stop
EndEnumeration

Enumeration FormFont
  #Font_Window_0_0
EndEnumeration

LoadFont(#Font_Window_0_0,"Consolas", 10)

Declare ResizeGadgetsWindow_0()


Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  OpenWindow(#Window_0, x, y, width, height, "WMI Query Testing Network", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  CreateStatusBar(0, WindowID(#Window_0))
  AddStatusBarField(600)
  StatusBarText(0, 0, "Ready")
  TextGadget(#Txt_Server, 10, 10, 110, 20, "Server")
  TextGadget(#Txt_User, 130, 10, 100, 20, "User")
  TextGadget(#Txt_Password, 250, 10, 100, 20, "Password")
  TextGadget(#Txt_Query, 370, 10, 100, 20, "Query")
  StringGadget(#Str_Server, 10, 30, 110, 25, "")
  StringGadget(#Str_User, 130, 30, 110, 25, "")
  StringGadget(#Str_Password, 250, 30, 110, 25, "", #PB_String_Password)
  StringGadget(#Str_Query, 370, 30, 220, 25, "service list")
  ButtonGadget(#Btn_Send_Query, 370, 60, 80, 25, "Send Query")
  EditorGadget(#Editor, 10, 90, 580, 280)
  SetGadgetFont(#Editor, FontID(#Font_Window_0_0))
  ButtonGadget(#Btn_Stop, 510, 60, 80, 25, "Stop")
EndProcedure

Procedure ResizeGadgetsWindow_0()
  Protected FormWindowWidth, FormWindowHeight
  FormWindowWidth = WindowWidth(#Window_0)
  FormWindowHeight = WindowHeight(#Window_0)
  ResizeGadget(#Str_Query, 370, 30, FormWindowWidth - 380, 25)
  ResizeGadget(#Editor, 10, 90, FormWindowWidth - 20, FormWindowHeight - StatusBarHeight(0) - 97)
  ResizeGadget(#Btn_Stop, 510, 60, FormWindowWidth - 520, 25)
EndProcedure
; --- end of form -----------------------------------------------------------------------

Procedure Send_Query(*Value)
    Debug "Send_Query()"
    StatusBarText(0, 0, "Please Wait...")
    Protected Server$   = GetGadgetText(#Str_Server)
    Protected User$     = GetGadgetText(#Str_User)
    Protected Password$ = GetGadgetText(#Str_Password)
    Protected Query$    = GetGadgetText(#Str_Query)
    
    Protected Full_Line$ = "/node:"         + Chr(34) + Server$     + Chr(34) +
                           " /user:"        + Chr(34) + User$       + Chr(34) + 
                           " /password:"    + Chr(34) + Password$   + Chr(34) + 
                           " "              + Query$
    Debug "Command Line: wmic " + Full_Line$
    
    Protected Run =  RunProgram("wmic", Full_Line$, "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide) 
    If Run
        StatusBarText(0, 0, "Running Query...Please Wait a few seconds...")
        While ProgramRunning(Run)
            If AvailableProgramOutput(Run)
                AddGadgetItem(#Editor, -1, ReadProgramString(Run))
            EndIf
        Wend    
        StatusBarText(0, 0, "Done.") 
    Else
        StatusBarText(0, 0, "Error.")
    EndIf
EndProcedure

OpenWindow_0()

Repeat
    Select WaitWindowEvent()        
        Case #PB_Event_Gadget   
            Select EventGadget()
                Case #Btn_Send_Query 
                    Define ID = CreateThread(@Send_Query(), 1)
                    ;Send_Query(1)
                Case #Btn_Stop
                    If IsThread(ID)
                        KillThread(ID)
                    EndIf
            EndSelect      
            
        Case #PB_Event_Timer     
            
        Case #PB_Event_CloseWindow     
            End
            
        Case #PB_Event_SizeWindow   
            ResizeGadgetsWindow_0()
            
    EndSelect
ForEver

End
Tested of course. (Je n'ai pas traité les accents, mais on peut le faire.)

Enjoy

:wink:

(edit: moved in Tricks 'n' Tips)
Last edited by Marc56us on Sun Mar 08, 2020 10:06 am, edited 1 time in total.
(English is not my native language, I use an online translator.)
tatanas
User
User
Posts: 86
Joined: Wed Nov 06, 2019 10:28 am

Re: wmic small GUI

Post by tatanas »

Simple and effective solution. I like it :)
I hope I will be able to do the same with Win API.
User avatar
Kwai chang caine
Addict
Addict
Posts: 4887
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: wmic small GUI

Post by Kwai chang caine »

Works great on W10 :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply