Jump to content

Pass inputbox to variable for function


Go to solution Solved by Resiak1811,

Recommended Posts

I'm working on a GUI with an input box to type in the name of a Windows service with buttons to start or stop it passing the input as the variable for the service. I've got some extra crap in here from examples, but where I'm stuck is passing the input into the variable. $TextInput is the inputbox, but I have the "canary" msgboxes to output the value of the inputbox, but I'm getting the value of 3. I haven't done this in over 10 years, I'd appreciate any help.

As for the ability for this exe to have the ability to start/stop services, this is for work and we have Threatlocker, so I can give the exe elevation no problem since we take away admin rights. This is for employees to manage services for the applications they use for our business customers and services.msc is really just all of MMC, so giving elevation to all of MMC gives a way for them to give themselves admin rights back.

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <Process.au3>
#include <MsgBoxConstants.au3>

$ServiceControl = GUICreate("Services",240,240,-1,-1,-1,-1)
$TextInput = GUICtrlCreateInput("Type name of service",40,40,120,20,-1,-1)
$ButtonExit = GUICtrlCreateButton("Exit",120,200,100,24,-1,-1)
$ButtonStart = GUICtrlCreateButton("Start",20,150,100,24,-1,-1)
$ButtonStop = GUICtrlCreateButton("Stop",120,150,100,24,-1,-1)
GUISetState(@SW_SHOW,$ServiceControl)

MsgBox(1,"",$TextInput) ; Canary

GuiSetState()
While 1
$msg=GuiGetMsg() ; $msg = GUI input
;~ If $msg = GUI buttonX then goto function buttonX
If $msg=-3 Then Exit
If $msg=$ButtonStart Then Start()
If $msg=$ButtonStop Then Stop()
If $msg=$ButtonExit Then TestExit()

Wend

Func Start()
    $StartService = _RunDos("net start " & $TextInput)
    MsgBox(1,"start",$TextInput) ; Canary
    EndFunc 

Func Stop()
    $StartService = _RunDos("net stop " & $TextInput)
    MsgBox(1,"stop","stop") ; Canary
    EndFunc 

Func TestExit()
Exit
EndFunc

While 1
Switch GUIGetMsg()
     Case $GUI_EVENT_CLOSE
     Case $ButtonExit
     TestExit()
EndSwitch
WEnd

 

Link to comment
Share on other sites

  • Solution

replace $TextInput by GUICtrlRead($TextInput)

 

for e.g :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <Process.au3>
#include <MsgBoxConstants.au3>

$ServiceControl = GUICreate("Services",240,240,-1,-1,-1,-1)
$TextInput = GUICtrlCreateInput("Type name of service",40,40,120,20,-1,-1)
$ButtonExit = GUICtrlCreateButton("Exit",120,200,100,24,-1,-1)
$ButtonStart = GUICtrlCreateButton("Start",20,150,100,24,-1,-1)
$ButtonStop = GUICtrlCreateButton("Stop",120,150,100,24,-1,-1)
GUISetState(@SW_SHOW,$ServiceControl)

MsgBox(1,"",GUICtrlRead($TextInput)) ; Canary

GuiSetState()
While 1
$msg=GuiGetMsg() ; $msg = GUI input
;~ If $msg = GUI buttonX then goto function buttonX
If $msg=-3 Then Exit
If $msg=$ButtonStart Then Start()
If $msg=$ButtonStop Then Stop()
If $msg=$ButtonExit Then TestExit()

Wend

Func Start()
;~     $StartService = _RunDos("net start " & $TextInput)
    MsgBox(1,"start",GUICtrlRead($TextInput)) ; Canary
    EndFunc

Func Stop()
;~     $StartService = _RunDos("net stop " & $TextInput)
    MsgBox(1,"stop","stop") ; Canary
    EndFunc

Func TestExit()
Exit
EndFunc

;~ While 1
;~ Switch GUIGetMsg()
;~      Case $GUI_EVENT_CLOSE
;~      Case $ButtonExit
;~      TestExit()
;~ EndSwitch
;~ WEnd

 

Link to comment
Share on other sites

Thanks! I feel like that's the big hurdle. Not sure why the function got commented out, but it works when I input a single word service like "Themes", but multi word services aren't being sent correctly, which makes me think that GUICtrlRead($TextInput) needs to be encapsulated in quotes.

 

Appreciate the help!

Link to comment
Share on other sites

I figured out the encapsulation, it's all working now when I elevate the exe. Too bad antivirus thinks it's a virus! Oh well, I can whitelist it.

 

Thanks @Resiak1811!

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <Process.au3>
#include <MsgBoxConstants.au3>

$ServiceControl = GUICreate("Services",240,240,-1,-1,-1,-1)
$TextInput = GUICtrlCreateInput("Type name of service",40,40,120,20,-1,-1)
$ButtonExit = GUICtrlCreateButton("Exit",120,200,100,24,-1,-1)
$ButtonStart = GUICtrlCreateButton("Start",20,150,100,24,-1,-1)
$ButtonStop = GUICtrlCreateButton("Stop",120,150,100,24,-1,-1)
GUISetState(@SW_SHOW,$ServiceControl)


GuiSetState()
While 1
$msg=GuiGetMsg()
If $msg=-3 Then Exit
If $msg=$ButtonStart Then Start()
If $msg=$ButtonStop Then Stop()
If $msg=$ButtonExit Then TestExit()

Wend

Func Start()
   $StartService = _RunDos("net start " & '"' & GUICtrlRead($TextInput) & '"')
    MsgBox(0,"Service Started",GUICtrlRead($TextInput)) ; Canary
    EndFunc

Func Stop()
    $StopService = _RunDos("net stop " & '"' & GUICtrlRead($TextInput) & '"')
    MsgBox(0,"Service Stopped",GUICtrlRead($TextInput)) ; Canary
    EndFunc

Func TestExit()
Exit
EndFunc

 

Link to comment
Share on other sites

Here :

#RequireAdmin
#include <GUIConstants.au3>
#include <Array.au3>

Global $oHandler = ObjEvent("AutoIt.Error", WMIerror)

Main()

Func Main()
  Local $hGUI = GUICreate("Service")
  Local $idServ = GUICtrlCreateCombo("", 10, 10, 250, 25)
  Local $idActiv = GUICtrlCreateButton("Start/Stop", 30, 50)
  GUICtrlSetState(-1, $GUI_DISABLE)
  GUICtrlSetData($idServ, _ArrayToString(GetList()))
  GUISetState()

  While True
    Switch GUIGetMsg()
      Case $GUI_EVENT_CLOSE
        Exit
      Case $idServ
        If Not GUICtrlRead($idServ) Then
          GUICtrlSetData($idActiv, "Start/Stop")
          GUICtrlSetState($idActiv, $GUI_DISABLE)
        Else
          GUICtrlSetData($idActiv, IsStarted(GUICtrlRead($idServ)) ? "Stop" : "Start")
          GUICtrlSetState($idActiv, $GUI_ENABLE)
        EndIf
      Case $idActiv
        SetState(GUICtrlRead($idServ), GUICtrlRead($idActiv))
        If @error Then MsgBox($MB_OK, "Error", "Unable to change state")
        GUICtrlSetData($idActiv, IsStarted(GUICtrlRead($idServ)) ? "Stop" : "Start")
    EndSwitch
  WEnd
EndFunc   ;==>Main

Func GetList()
  Local $oWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
  Local $oList = $oWMIService.ExecQuery('SELECT name, started FROM Win32_Service')
  Local $aServ[$oList.count], $i = 0
  For $oServ In $oList
    $aServ[$i] = $oServ.name
    $i += 1
  Next
  Return $aServ
EndFunc   ;==>GetList

Func IsStarted($sName)
  Local $oWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
  Local $oService = $oWMIService.Get("Win32_Service.Name='" & $sName & "'")
  Return $oService.Started
EndFunc   ;==>IsStarted

Func SetState($sName, $sState)
  Local $oWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
  Local $oService = $oWMIService.Get("Win32_Service.Name='" & $sName & "'")
  If $sState = "Start" Then
    $oService.StartService()
  Else
    $oService.StopService()
  EndIf
  If @error Then Return SetError(1)
EndFunc   ;==>SetState

Func WMIerror($oError)
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...