Jump to content

Recommended Posts

Posted (edited)

EDIT:

This is the code I am trying to run. The file does not show up but the service starts. Is this because its running under the system account?

This is the install...

#include<Service.au3>
$sServiceName = "Autoit_Service"

InstallService()

Func InstallService()
    ConsoleWrite("Installing service, please wait" & @CRLF)
    _Service_Create("", $sServiceName, "Autoit Service Test", @DesktopDir & "\servicetest.exe")
    If @error Then
        ConsoleWrite("Problem installing service, Error number is " & @error & @CRLF & " message  : " & _WinAPI_GetLastErrorMessage())
    Else
        ConsoleWrite("Installation of service successful")
    EndIf
    Exit
EndFunc   ;==>InstallServiceoÝ÷ Ù8b²+-ë®øx¦¶¼¢hëm¢»§jëh×6#include<Service.au3>
$sServiceName = "Autoit_Service"

_Service_init($sServiceName)
if fileexists("c:\test.txt") Then
    FileDelete("c:\test.txt")
endif
while 1
    $file = FileOpen("c:\test.txt", 1)
    fileWrite($file, "working" & @CRLF)
    FileClose($file)
    sleep (20000)
WEnd
Edited by SoulA
Posted (edited)

ok you're right, the main function is not executed,

and two : the example has a bug at this line, should be :

_Service_Create("", $sServiceName, "Autoit Service Test", '"' & @ScriptFullPath & '"')

ok i've understood.

First the SCM controls the process.

So every call is done by SCM and not by autoit.

So i've added the "main()" call in the service_main function.

That works ok now.

thx for poiting me this bug.

i've uploaded service.au3 and service_example.au3 fixed.

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]
Posted (edited)

Excellent. I was messing with calling the main func from services.au3 but couldn't figure out the right placement. I made a template for a script example, that will force the exe to run as a service and will prompt to install it if is not. I cleaned up the UDF a bit too. (comments and extra lines mostly). Posted below.

Edited by spudw2k
Posted (edited)

I renamed the create function too, cause I'm anal and like standard formatting. Here's my cleaned up service and example template.

All credit goes to arcker, and SumTingWong. I only tailored their code.

whoops, little bug in service name. removed period at end.

Ok, last change. Added a feature to run and stop the service; based on the _selfdelete func. :)

_Service.au3

_Blank.au3

Edited by spudw2k
Posted

ok you've cleaned it out. I've let the commentary in the original

UDF to help people the C++ source and its conversion to autoit.

In your example you made a "run me as service " function.

I've already thought of that. Your example is not bad but it's imply a temporary

batch.

batch is not necessary since you know the real good function in DOS : &&

you can do this way :

Func _SelfRun($servicename,$action)
    $sCmdFile = 'cmd /c ping 127.0.0.1 -n 1 && sc ' & $action & ' "' & $servicename & '"' 
    ;FileWrite(@TempDir & "\runsvc.bat", $sCmdFile)
    Run($sCmdFile, @TempDir, @SW_HIDE)
    Exit
EndFunc

IMO : run me as service is not really necessary since services are generally installed by another main process and start from there.

thx for your contribution :)

will add your blank example later.

-- 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]
Posted

The reason I made the batch file is cause I also implemented the _Singleton func....unless maybe I'm over thinking it. I suppose the service control manager will only "manage" a single instance of the process anyways right? Maybe it is a little overkill.

Posted

Ok, I'm going to admit I did not follow those instructions at all. It may just be because I'm not actually doing them but none the less could some one lay this out barney style for me? Is it possible to make this automatically so the script will just do this? Or do I have to go through all this command prompt crap every time I load it onto a different computer. In other words can I take out the the main source of errors in this equation (the user).

[center][/center]Working on the next big thing.Currently Playing: Halo 4, League of LegendsXBL GT: iRememberYhslaw

Posted (edited)

Ok, I'm going to admit I did not follow those instructions at all. It may just be because I'm not actually doing them but none the less could some one lay this out barney style for me? Is it possible to make this automatically so the script will just do this? Or do I have to go through all this command prompt crap every time I load it onto a different computer. In other words can I take out the the main source of errors in this equation (the user).

You can code it to start however you want. If you want it to auto install and run then you'll at least need a func to check if the service is installed, and if not then install it.

$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
Edited by spudw2k
Posted

OK, my understanding before was that you had to compile the script, then do some tricks with it in command prompt to get it to work. I didn't realise it was "in-script" Thank you for the clarification.

[center][/center]Working on the next big thing.Currently Playing: Halo 4, League of LegendsXBL GT: iRememberYhslaw

Posted

OK, my understanding before was that you had to compile the script, then do some tricks with it in command prompt to get it to work. I didn't realise it was "in-script" Thank you for the clarification.

Actually, yes it does need to be compiled.
Posted

I am still confused on how the main function is called?

Great code, but can anyone explain to me the benefits of running it as a service, I konw you did a little on the first post but how would I implement that functionality?

Thanks

Posted (edited)

I am still confused on how the main function is called?

Great code, but can anyone explain to me the benefits of running it as a service, I konw [sic] you did a little on the first post but how would I implement that functionality?

Thanks

I'm sure someone, including the author could give you a technical reason, but here's my understanding.

There are some processes that should be running in the background at all times. Running it as a service allows this to happen without a user logged in to run it or using a user account (if desired). It can also be configured to do fail-over functions like restart and on-crash functions that are part of the Service Control Manager in Windows. The special thing about this script is, prior to it you needed special software to run software as a service. Service applications are written in such a way that the SCM must be able to "communicate" with it. Arcker has converted c++ (i think) code into autoit.

Also, the Main() func is called from the service.au3 UDF. Hope that helps a little.

Edited by spudw2k
Posted

for me i needed yo make an agent. So i needed a script that runs under system privileges (so without user profile needed ) and that windows would run and alters me if there any problem with it.

Now i'm making an install service : Many software for their "remote installation" do this that way :

1 -copy the exe

2 -create the service (you can create a service remotely with admin privileges )

3 - start it : system privileges, so it can interact with the user, so you can make a progress bar or something visible during installation.

BTW : I've precised that the script need to be compiled. I will rewrite it in bold to stop confusing :)

-- 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]
Posted (edited)

Services are loaded before anything at boot time, even before winlogon (this means that you can do what ever you want with windows before logging to it - very mean things can be done, like avoiding activation of windows), so that everything is ready for the user upon logging.

Edited by trancexx

♡♡♡

.

eMyvnE

Posted

Great explanations! I appreciated all your support. I have a pretty solid understanding of it all now.

The only problem I continue to encounter is the Error 0. Here are the steps of how I do everything.

1. Save service.au3

2. Save Service_Example.au3

3. Compile Service_Example.au3

4. With command prompt run the command Service_Example.exe -i

5. Returned "Serviced Installed Successfully"

6. I then execute Service_Example.exe

7. Everything pauses for a little while, then I get a MsgBox saying Error 0.

Any ideas?

Thanks,

Steve

Posted

Great explanations! I appreciated all your support. I have a pretty solid understanding of it all now.

The only problem I continue to encounter is the Error 0. Here are the steps of how I do everything.

1. Save service.au3

2. Save Service_Example.au3

3. Compile Service_Example.au3

4. With command prompt run the command Service_Example.exe -i

5. Returned "Serviced Installed Successfully"

6. I then execute Service_Example.exe

7. Everything pauses for a little while, then I get a MsgBox saying Error 0.

Any ideas?

Thanks,

Steve

See post #13
Posted

6. I then execute Service_Example.exe

7. Everything pauses for a little while, then I get a MsgBox saying Error 0.

Any ideas?

Thanks,

Steve

... so you need to know what a service is :)

a service is launched by the SCM, not by you , meaning "manually"

re-read the procedure i've posted >_<

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

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
×
×
  • Create New...