Jump to content

_Service_UDF v4 : Build your own service with autoit code


arcker
 Share

Recommended Posts

OK... I think I have this working correctly but it seems like it may not work for me? :mellow:

I am using one exe to create the service. Then I have another exe which is the service. The exe that creates it seems to create it just fine.

The service seems to run. I see it in taskmanager. But it is supposed to check a regkey for info. If it finds that key it is supposed to delete it then execute the exe it found in that key.

But it doesn't seem to do anything. :(

Is this also a limitation of running this as a service?

Is it not possible to read / write / delete registry keys when it is run as a service?

I was hoping to use this method instead of the srvany.exe method because then I can get rid of srvany.exe and not have to use an extra exe.

Any ideas?

Thanks!

Link to comment
Share on other sites

OK... I think I have this working correctly but it seems like it may not work for me? :mellow:

I am using one exe to create the service. Then I have another exe which is the service. The exe that creates it seems to create it just fine.

The service seems to run. I see it in taskmanager. But it is supposed to check a regkey for info. If it finds that key it is supposed to delete it then execute the exe it found in that key.

But it doesn't seem to do anything. :(

Is this also a limitation of running this as a service?

Is it not possible to read / write / delete registry keys when it is run as a service?

I was hoping to use this method instead of the srvany.exe method because then I can get rid of srvany.exe and not have to use an extra exe.

Any ideas?

Thanks!

I can't think of any reason that SYSTEM wouldn't have priv to edit the registry. It would be easier for us to help if we could see your code.
Spoiler

Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder
Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array
Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc
Cool Stuff: AutoItObject UDF â—Š Extract Icon From Proc â—Š GuiCtrlFontRotate â—Š Hex Edit Funcs â—Š Run binary â—Š Service_UDF

 

Link to comment
Share on other sites

http://www.autoitscript.com/forum/index.ph...st&p=579806

Also, check these out. If the service doesn't exist when it's run is asks if you want to install it.

http://www.autoitscript.com/forum/index.ph...st&p=578259

So which code exactly do i use..??

I dont want to use any temp CMD or Batch files to install or start the service...so what would i do now..??

what exactly does your code here below do..??

$Servicename = StringLeft(@ScriptName,StringInstr(@ScriptName,".")-1)
$sServiceName = "AutoIt_Service_" & $Servicename
If Not _ServiceExists("", $sServiceName) Then InstallService()
Run(@comspec & " /c " & 'sc start ' & '"' & $sServiceName & '"',"",@SW_HIDE)
_Service_Init($sServiceName)

Func _Main()
    While 1
        ;Service Code Loop
        sleep(10)
    WEnd
EndFunc   ;==>main

Func _ServiceExists($sComputerName, $sServiceName)
    Local $hAdvapi32
    Local $hKernel32
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    $hAdvapi32 = DllOpen("advapi32.dll")
    If $hAdvapi32 = -1 Then Return 0
    $hKernel32 = DllOpen("kernel32.dll")
    If $hKernel32 = -1 Then Return 0
    $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", "str", $sComputerName, "str", "ServicesActive", "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
        $arRet = DllCall($hKernel32, "long", "GetLastError")
        $lError = $arRet[0]
    Else
        $hSC = $arRet[0]
        $arRet = DllCall($hAdvapi32, "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", $SERVICE_STOP)
        If $arRet[0] = 0 Then
            $arRet = DllCall($hKernel32, "long", "GetLastError")
            $lError = $arRet[0]    
        EndIf
        DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    DllClose($hAdvapi32)
    DllClose($hKernel32)   
    If $lError <> -1 Then 
        SetError($lError)
        Return 0
    EndIf
    Return 1
EndFunc
Link to comment
Share on other sites

So which code exactly do i use..??

I dont want to use any temp CMD or Batch files to install or start the service...so what would i do now..??

what exactly does your code here below do..??

$Servicename = StringLeft(@ScriptName,StringInstr(@ScriptName,".")-1)
$sServiceName = "AutoIt_Service_" & $Servicename
If Not _ServiceExists("", $sServiceName) Then InstallService()
Run(@comspec & " /c " & 'sc start ' & '"' & $sServiceName & '"',"",@SW_HIDE)
_Service_Init($sServiceName)

Func _Main()
    While 1
        ;Service Code Loop
        sleep(10)
    WEnd
EndFunc   ;==>main

Func _ServiceExists($sComputerName, $sServiceName)
    Local $hAdvapi32
    Local $hKernel32
    Local $arRet
    Local $hSC
    Local $hService
    Local $lError = -1

    $hAdvapi32 = DllOpen("advapi32.dll")
    If $hAdvapi32 = -1 Then Return 0
    $hKernel32 = DllOpen("kernel32.dll")
    If $hKernel32 = -1 Then Return 0
    $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", "str", $sComputerName, "str", "ServicesActive", "long", $SC_MANAGER_CONNECT)
    If $arRet[0] = 0 Then
        $arRet = DllCall($hKernel32, "long", "GetLastError")
        $lError = $arRet[0]
    Else
        $hSC = $arRet[0]
        $arRet = DllCall($hAdvapi32, "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", $SERVICE_STOP)
        If $arRet[0] = 0 Then
            $arRet = DllCall($hKernel32, "long", "GetLastError")
            $lError = $arRet[0]    
        EndIf
        DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hSC)
    EndIf
    DllClose($hAdvapi32)
    DllClose($hKernel32)   
    If $lError <> -1 Then 
        SetError($lError)
        Return 0
    EndIf
    Return 1
EndFunc
Try looking at the code and see what it does. The second link has a blank service template and a modified service udf file that has the _ServiceExists func in it.
Spoiler

Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder
Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array
Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc
Cool Stuff: AutoItObject UDF â—Š Extract Icon From Proc â—Š GuiCtrlFontRotate â—Š Hex Edit Funcs â—Š Run binary â—Š Service_UDF

 

Link to comment
Share on other sites

Why is AutoIt3 Syntax Checker finding these Errors in your code spudw2k..??

>C:\Program Files\AutoIt3\SciTE\..\au3check.exe "C:\Documents and Settings\VM_WARE\Desktop\service.au3"
AutoIt3 Syntax Checker v1.54.8  Copyright (c) Tylo 2007

C:\Documents and Settings\VM_WARE\Desktop\service.au3(26,134) : WARNING: $SC_MANAGER_CONNECT: possibly used before declaration.
    $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", "str", $sComputerName, "str", "ServicesActive", "long", $SC_MANAGER_CONNECT)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\VM_WARE\Desktop\service.au3(32,118) : WARNING: $SERVICE_STOP: possibly used before declaration.
        $arRet = DllCall($hAdvapi32, "long", "OpenService", "long", $hSC, "str", $sServiceName, "long", $SERVICE_STOP)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\VM_WARE\Desktop\service.au3(26,134) : ERROR: $SC_MANAGER_CONNECT: undeclared global variable.
    $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", "str", $sComputerName, "str", "ServicesActive", "long", $SC_MANAGER_CONNECT)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\VM_WARE\Desktop\service.au3(3,62) : ERROR: InstallService(): undefined function.
If Not _ServiceExists("", $sServiceName) Then InstallService()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\VM_WARE\Desktop\service.au3(5,28) : ERROR: _Service_Init(): undefined function.
_Service_Init($sServiceName)
~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\VM_WARE\Desktop\service.au3 - 3 error(s), 2 warning(s)
>Exit code: 2    Time: 0.253
Link to comment
Share on other sites

Why is AutoIt3 Syntax Checker finding these Errors in your code spudw2k..??

It is missing the service.au3 (or _service.au3) include.
Spoiler

Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX Builder
Misc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retrieve SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose Array
Projects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalc
Cool Stuff: AutoItObject UDF â—Š Extract Icon From Proc â—Š GuiCtrlFontRotate â—Š Hex Edit Funcs â—Š Run binary â—Š Service_UDF

 

Link to comment
Share on other sites

How does this work if you are running Vista and get alerts from "Interactive Service Dialog" if your compiled script has a GUI such as a Systray Icon? Vista Seems to run Interactive Services in another Windows Session. How can I make the copmiled exe GUI/Systray run in my normal logged in user so I can see it in the systray and interact with it?

I have already disabled the windows Service "Interactive Service"

Good work,

GoogleDude

Link to comment
Share on other sites

One process will interact and be launched as user, while the service will be run at system.

The thing you need is to create this other process ( since autoit doesn't support thread ) and interact with it.

edit : I'm in a good mood so i repost the link :mellow:

http://msdn.microsoft.com/en-us/library/ms683502.aspx

ShminkyBoy did some good research on it. Maybe it what you are looking for.

http://www.autoitscript.com/forum/index.php?showtopic=83099

Edited by arcker

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

So then does this mean that when I install the Service I need to really be installing a Watch Dog type service that will make sure that my compiled script is always running? I think im getting confused here. I would think that even if a watch dog type service launches my compiled script I would think it would launch it in the same session/winstation that the watch dog was running in? Or am I getting ahead of my self.

Thanks for any help.

GD

Edited by GoogleDude
Link to comment
Share on other sites

arcker, thank you for great script.

I have trouble: need execute code on exit, but OnAutoItExit () - don't working by stop service.

Try insert code into Func _Service_Ctrl($ctrlCode):

Case $SERVICE_CONTROL_STOP
----->MyFunc() - generate error on stop service, but simple command as "ConsoleWrite" working without generate error.
        ;MsgBox(0,"","Service stop")
            _Service_ReportStatus($SERVICE_STOP_PENDING, $NO_ERROR, 0);
            _Service_SetStopEvent();
            _Service_Cleanup()
            Exit

Anybody can help me?

Link to comment
Share on other sites

  • 3 weeks later...

ok i will add it. Please make only background process. Services are made for that ... not for GUI's purpose.

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

ok i will add it. Please make only background process. Services are made for that ... not for GUI's purpose.

What would you suggest for a help desk system that runs in the system tray and has a right-click menu, then? I would like it installed as a service and for it to have a GUI form and such, but I also want it to be a service. Does that make sense?

Edited by Yorn
Link to comment
Share on other sites

i would suggest two processes : on that load as service, and the other as user. the talks each other with differents techniques. That's what MS recommends too.

search on this thread i've posted a link if i remember well.

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Link to comment
Share on other sites

choose :

; Service start types
Global Const $SERVICE_BOOT_START = 0x00000000
Global Const $SERVICE_SYSTEM_START = 0x00000001
Global Const $SERVICE_AUTO_START = 0x00000002
Global Const $SERVICE_DEMAND_START = 0x00000003
Global Const $SERVICE_DISABLED = 0x00000004

by default the example is with DEMAND_START just for testing purposes.

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
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

×
×
  • Create New...