Jump to content
Sign in to follow this  
arcker

_Service_UDF v4 : Build your own service with autoit code

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!

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

i can not use

winwait()

winexists()

winClose()

winKill()

please specify all the functions of your UDF included information of it's limitations.

Same Problem with me.

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
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 will you add? please explain...

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By SgtHugoStiglitz
      HI people,
                        I got a questions, I maintain code in autoIT and every time I compile a new version or modification, Scite generate 2 folders. One folder is finish by released and the other is tentative. I try to make some google search, but never find answer would satisfied myself. The dev who was maintain it before were using AutoIt3Wrapper_GUI, and I don't know if it's related. I look both folder, but they seem pretty identical, but I would like to know what it's the difference between those to folder. Anyone have a clue?
    • By Rhidlor
      I don't have any experience compiling AutoIt scripts or know of any best practices regarding said scripts so I was wondering, what are some of the options for passing data to complied scripts? Here's an example of what I mean by "option": Writing data to a text file, then running a script which reads the data from that text file.
      Thanks!
      I apologize if this topic is a little lackluster
    • By Skeletor
      Hey All,
      Yes, this is an odd request. 
      But is there a way to bulk up the size of the compiled program? 
      I know many want to reduce it's size, but I want to increase it.
      What methods do you suggest?
    • By dimm
      Is it possible to compile script without any icon resources included?
      When I make exe it uses default system icon of current OS even if I compile as console application.
      On other versions of Windows it looks a little strange

       
      Update:
      It seems that the only way is remove icons from exe after compilation using resource editors like ResHacker.

       
      another bad solution:
      Using corrupted 35 bytes icon file. Windows can't load it and shows default system icon.
      #pragma compile(Icon, broken.ico) broken.ico
    • By Dreamfire
      Hi,
      Since today, exe's are being flagged as having a trojan by Windows Defender (Fuery.B!cl)
      Version:  3.3.14.3 - SciTE Version 3.7.3



       

×
×
  • Create New...