Jump to content

Au3@Service


ptrex
 Share

Recommended Posts

Is anyone interested to run Au3 (compiled scripts) as a windows service ?

Let me know, I have a solution. Just have to finalize a GUI for it.

Maybe I can trade this in against a SQLite plugin B)

OK since you all replied so enthusiastic. :o Here is the script.

;-------------------------------------
;
; Run AutoIT Scripts as a service
; PTREX 03/11/2005
;
;-------------------------------------
#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <Process.au3>

#NoTrayIcon

;Declare Vars
Global $objWMIService
Global $strComputer = "."

Const $OWN_PROCESS = 16;16 is own process
Const $INTERACTIVE = true;True changes the $Own_Process to 272 is interact with desktop
Const $NORMAL_ERROR_CONTROL = 1
Dim $Script, $SrvAny, $info, $Help

;Main Gui
;Generated with Form Designer preview, great Job !!
GUICreate("Au3@Service", 570, 668, 289, 123, $WS_OVERLAPPEDWINDOW)
$ListView1 = GUICtrlCreateListView("Service Name |Status", 24, 160, 520, 481, $LVS_ICON+$LVS_SORTASCENDING )
_GUICtrlListViewSetColumnWidth ($listview1, 0,320)
$GUI_FileMenu   = GUICtrlCreateMenu     ("&File")
$GUI_FileOpen   = GUICtrlCreateMenuitem ("&Open..."     ,$GUI_FileMenu)

$Helpmenu = GUICtrlCreateMenu ("?")
$Helpitem = GUICtrlCreateMenuitem ("Help",$Helpmenu)
$Infoitem = GUICtrlCreateMenuitem ("Info",$Helpmenu)

$Edit1 = GUICtrlCreateInput("Select a compiled script to start", 24, 20, 273, 21)
$Checkbox1 = GUICtrlCreateCheckbox ("Interact with Desktop. (Your script has a GUI)", 24, 130, 280, 20)
GUICtrlSetState (-1,1)            
$Button1 = GUICtrlCreateButton("Add Any Service", 24, 110, 93, 17)
$Button2 = GUICtrlCreateButton("Delete Any Service", 120, 110, 120, 17)
$Button3 = GUICtrlCreateButton("Open Services", 303, 110, 91, 17)
$Button4 = GUICtrlCreateButton("Select Script ...", 24, 50, 91, 17)
$Button5 = GUICtrlCreateButton("Assing Script to run" , 120, 50, 120, 17)

GUICtrlCreateGroup ("Services Control", 400, 5, 140, 150)
$RadioButton1 = GUICtrlCreateRadio("List All Services", 416, 24, 89, 17)
$RadioButton2 = GUICtrlCreateRadio("List Au3 Services", 416, 54, 113, 17)
$RadioButton3 = GUICtrlCreateRadio("Start Au3 Service", 416, 94, 120, 17)
$RadioButton4 = GUICtrlCreateRadio("Stop Au3 Servioe", 416, 124, 120, 17)
GUICtrlSetState ($RadioButton1,$GUI_CHECKED)


GUISetState(@SW_SHOW)
Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount ($listview1) ]

_List_Services1()

While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $GUI_FileOpen
        _File_Open()
    Case $msg = $infoitem
        Info()
        Msgbox(0,"Info",$Info)
    Case $msg = $Helpitem
        Help()
        Msgbox(0,"Help",$Help)
    Case $msg = $Button1
        _Install_Service()
    Case $msg = $Button2
        _Delete_Service()
    Case $msg = $Button3
        _Open_Services()
    Case $msg = $Button4
        _Select_Script()
    Case $msg = $Button5
        _Assign_Script()
    Case $msg = $RadioButton1
        _List_Services1()
    Case $msg = $RadioButton2
        _List_Services2()
    Case $msg = $RadioButton3
        _Start_Service()
    Case $msg = $RadioButton4
        _Stop_Service()
    Case $msg = $Checkbox1
        _Interact_Desktop()
    EndSelect
WEnd
Exit

;Functions
Func _File_Open()
    $FileOD = FileOpenDialog("Select SrvAny.exe File", "C:\", "EXE (*.exe)", 3)
    $File = FileOpen($FileOD, 0)
    GUICtrlSetData($Edit1,Chr(34) & $FileOD  & chr(34))
    $SrvAny = (Chr(34) & $FileOD  & chr(34) )
    If $File = -1 Then
        MsgBox(0, "Error", "Unable to open file, or no file selected !!")
    EndIf
EndFunc

Func _Select_Script()
    $FileOD = FileOpenDialog("Select Au3 Script File", "C:\", "EXE (*.exe)", 3) 
    $File = FileOpen($FileOD, 0)
    GUICtrlSetData($Edit1,Chr(34) & $FileOD  & chr(34))
    $Script = (Chr(34) & $FileOD  & chr(34) )
    If $File = -1 Then
        MsgBox(0, "Error", "Unable to open file, or no file selected !!")
    EndIf
Endfunc

Func _List_Services1()
    _GUICtrlListViewDeleteAllItems ($listview1)    
    $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $colRunningServices = $objWMIService.ExecQuery("Select * from Win32_Service")
        For $objService in $colRunningServices 
    local $Data = ($objService.DisplayName  & "|" & $objService.State)
    GUICtrlCreateListViewItem($Data,$listview1)
    Next
EndFunc

Func _List_Services2()
    _GUICtrlListViewDeleteAllItems ($listview1)    
    $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $colRunningServices = $objWMIService.ExecQuery("Select * from Win32_Service Where Name like '_Au%'")
        For $objService in $colRunningServices 
    local $Data = ($objService.DisplayName  & "|" & $objService.State)
    GUICtrlCreateListViewItem($Data,$listview1)
    Next
EndFunc

Func _Install_Service()
    If $SrvAny = "" Then
    MsgBox(0,"Warning ", "Select the SvrAny.exe using the File Open first")
    Else
    MsgBox(0,"Information","This has to be activated only ONCE !!")
    $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $objService = $objWMIService.Get("Win32_BaseService")
    $errReturn = $objService.Create("_Au3@Service" ,"_Au3@Service" , $SrvAny, $OWN_PROCESS, $NORMAL_ERROR_CONTROL, _
    "Manual", $INTERACTIVE, "LocalSystem", ""  )
    EndIf
EndFunc
       
Func _Delete_Service()
    $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '_Au3@Service'")
        For $objService in $colListOfServices
            $objService.StopService()
            $objService.Delete()
    Next
EndFunc

Func _Start_Service()
    Local $var1 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service", "ImagePath")
    If @error = -1 or StringRight ($var1, 11) <> "srvany.exe"&chr(34) Then
        MsgBox(4096,"",  "RegKey SrvAny not yet created, or not correct"&$var1)
    Else
        Local $var2 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service\Parameters", "Application")
        If @error = 1 Then
        MsgBox(0,"Warning","No Script has been assigned yet to the Any service!!")
        EndIf
    _RunDos("Net start _au3@service")
    EndIf
EndFunc

Func _Stop_Service()
    _RunDos("Net stop _au3@service")
EndFunc

Func _Open_Services()
    _RunDos("services.msc")
EndFunc

Func _Interact_Desktop()
    Local $var1 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service", "ImagePath")
    if @error = -1 or StringRight ($var1, 11) <> "srvany.exe"&chr(34) Then
        MsgBox(4096,"",  "RegKey not yet created, or not correct")
    Elseif GUICtrlRead($Checkbox1)= 1 Then
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service", "Type", "REG_DWORD", "272")
    Elseif GUICtrlRead($Checkbox1)= 4 Then
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service", "Type", "REG_DWORD", "16")
    EndIf
EndFunc

Func _Assign_Script()
        Local $var1 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service", "ImagePath")
    if @error = -1 or StringRight ($var1, 11) <> "srvany.exe"&chr(34) Then
        MsgBox(4096,"",  "RegKey not yet created, or not correct")
    Else 
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service\Parameters", "AppDirectory", "REG_SZ", @WorkingDir)
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\_Au3@service\Parameters", "Application", "REG_SZ", $Script)
    EndIf
EndFunc

Func Info()
    $Info = "Sytem Requirements :"&@CR&@CR& _
            "- In order to use this application you need to have 98/NT/2K/XP."&@CR&@CR& _
            "- You need to copy the file SrvAny.exe to your Windows directory."&@CR& _
            "  Afterwards you will need to register this file as a service, once."&@CR& _
            "  See Help for more info"
            
EndFunc

Func Help()
    $Help = "Registering the SrvAny.exe file as a service :"&@CR& _
            "- Go to the Windows directory where you have placed the SrvAny.exe file, using the FILE OPEN menu. " &@CR& _
            "- Press the Add Any Service button. This procedure has to be done only once."  &@CR&@CR& _
            "Assigning a Script to the service :"&@CR& _
            "- Use the Select Script button to pick a compiled script."&@CR& _
            "- Press the Assign Script to run button, to link this to the service."&@CR&@CR& _
            "Starting the service :"&@CR& _
            "- Click the start Au3 Service radio button, to start your script service."&@CR&@CR& _
            "Stopping the service :"&@CR& _
            "- Click the stop Au3 Service radio button, to stop your script service."&@CR&@CR& _
            "Open Services :"&@CR& _
            "- This button will allow you to open the Services.msc."
EndFunc

You will need this file to put in your Windows Dir.

This SrvAny.exe file(provided by MS), works as a wrapper for your custom services.

Read the Help included in the Gui to proceed from there.

This is a Test script you can use.

If everthing works fine a countdown will appear in a msgbox.

You can logoff and logon again and still see the countdown proceeding.

sleep (3000)
For $i = 100 to 1 Step -1
    MsgBox(0, "Count down!", $i,1)
    MsgBox(0,"Services", "Test is still running",1)
Next

Update for 3.2.11.12 and later.

Au3_Services.au3

Edited by ptrex
Link to comment
Share on other sites

Link to comment
Share on other sites

@SumTingWong

I checked out your suggestion. Thanks by the way !!

But is seems more a utillty for the JAVA oriented JudoScripters.

Not what I had in mind.

I haven't actually needed to use it myself but I know colleagues who have used it to run compiled AutoIT scripts as services and it does work.

Link to comment
Share on other sites

Link to comment
Share on other sites

@gcriaco

Thanks for the input.

I know there are lot's of these tools available elsewhere.

But AutoIt makes nice GUI's to manage your Run as a Service dynamically.

I like that a lot about AutoIT. I think this make it a nice playground B)

And it is a good reference for other Script authors, if they want to do similar scripts.

Link to comment
Share on other sites

  • 3 months later...
Link to comment
Share on other sites

@Adam1213

I know that you can run a script like you indicate, but this is not quite the same as running a script AS a SERVICE.

This way a script keeps on running even if you have logged off !!

Thanks for the feedback anyway.

Not certain on your way. Only had a quick look at it

Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

@Fossil Rock

Late reply, too late maybe ?

The delete Any Service feature, does what it says.

It remove the Au3@service from the Windows services list.

This is kind if uninstall for the Au3@service, to get rid of it.

When you pressed that button, you will see that the "_au3@service" has been removed from the services list.

Link to comment
Share on other sites

  • 3 weeks later...

Good job dude. Is there any way one can disable a service in the services list using au3 script?

[font="Franklin Gothic Medium"]RnJ[/font][font="Franklin Gothic Medium"]Script: AutoIT v3.1.1.124 (beta)[/font][font="Franklin Gothic Medium"]OS: Windows XP/2000[/font]
Link to comment
Share on other sites

It is possible.

this will diable all services that are have MANUAL as start type.

Not tested of course.

#Include <Constants.au3>
Func RemoteReboot()

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = inputbox( "Please enter the name of the computer you want to reboot", "Input" )
$objWMIService = ObjGet("winmgmts:{(RemoteShutdown)}//" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_Service where StartMode = 'Manual'")

    For $objOS in $colItems
    $objOS.Change( , , , , "Disabled")   
    Next
endFunc

Success

Link to comment
Share on other sites

  • 3 months later...
  • Moderators

Is SrvAny free to use in free programs ? I just tried RunExeSvc and the Admin Tool tells me :

"Error 1073 : The specified service is already running" but it is not running.

:nuke: RootKit :P

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...