arcker

_Service_UDF v4 : Build your own service with autoit code

366 posts in this topic




#22 ·  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

Share this post


Link to post
Share on other sites

#23 ·  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]

Share this post


Link to post
Share on other sites

Very nice!

although this will make it easier for people to make 'autoit virusses wich are hard to detect'

Share this post


Link to post
Share on other sites

#25 ·  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

Share this post


Link to post
Share on other sites

Post it. I'm sure that arcker wouldn't mind


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#27 ·  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

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#31 ·  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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#35 ·  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

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

#37 ·  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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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]

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

  • Similar Content

    • khathiatz
      CompileMe - Compile script faster without GUI, using hotkey
      By khathiatz
      - Compile script faster
      - Using Hotkey, no GUI
      - Using *.BAT and *.VBS script to compile
      - Full info for *.exe file. Ex: Software name, version, companyname ...
       
      English: https://khathiatz.blogspot.com/2016/08/compileme-en.html
      Tiếng Việt: https://khathiatz.blogspot.com/2016/07/compile-me.html
       
       
       
       
    • rootx
      Compile question help
      By rootx
      I would like to ask you some questions about compiling.
      I use Autoit wrapper gui 15.920.938.0
      1. I would like to delete all comments from my compiled code.
      If is possible...
      I am using the 3.3.14.2 version of Autoit, I would like this time to protect my code, without using external tools that are seen as viruses, how do you protect your code usually?
      Thx
    • RaiNote
      Little Question
      By RaiNote
      Hello,
      Just a Little question why the autoit exes also with a very Little Code get when generated around 800-1000kb also would there be anyday away to compile a script to a Dynamic Link Library(dll)? Is there a way to Change to encryption of Compiling to something what makes the compile file a bit Shorter :O?
      Thanks in advance.
    • ur
      Version Missing
      By ur
      When we compile our code through Aut2exe, version is missing if we check the properties of the compiled executable.
      Can any one suggest how to set a version number to the compiled exe.
    • HelpfulToasty
      Compiling problems
      By HelpfulToasty
      So, I made a script, and I need to compile it so I can use it elsewhere and stuff. Whenever I try to compile it, I get an 
      Error: Unable to add resources.
      C:\Users\User\AppData\Local\AutoIt v3\Aut2Exe\???????.exe    ( - The "??????"'s change each time I try, but it's a .exe.)
      It's not my Anti Virus, I hope. My Anti Virus is Microsoft Security Essentials.
      Please help me.