ranhalt Posted August 15 Share Posted August 15 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. expandcollapse popup#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 More sharing options...
Solution Resiak1811 Posted August 15 Solution Share Posted August 15 replace $TextInput by GUICtrlRead($TextInput) for e.g : expandcollapse popup#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 ranhalt 1 Link to comment Share on other sites More sharing options...
ranhalt Posted August 16 Author Share Posted August 16 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 More sharing options...
ranhalt Posted August 16 Author Share Posted August 16 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! expandcollapse popup#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 More sharing options...
Nine Posted August 16 Share Posted August 16 Here : expandcollapse popup#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 ranhalt and ioa747 2 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now