Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

_Service_UDF v4 : Build your own service with autoit code

Service 1053 1061 compile

  • Please log in to reply
331 replies to this topic

#21 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 11 September 2008 - 11:35 PM

Never, yet the script continues to run??? :)







#22 SoulA

SoulA

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 251 posts

Posted 12 September 2008 - 12:37 AM

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...
AutoIt         
#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   ;==>InstallServiceƒo݊÷ Ù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, 12 September 2008 - 02:31 AM.


#23 arcker

arcker

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 581 posts

Posted 12 September 2008 - 07:45 AM

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, 12 September 2008 - 08:15 AM.

-- Arck System _ Soon -- Ideas make everything

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

 

Projects :

  • Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here

#24 ludocus

ludocus

    Possibly inventive crap going on right here

  • Active Members
  • PipPipPipPipPipPip
  • 668 posts

Posted 12 September 2008 - 02:51 PM

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

#25 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 12 September 2008 - 03:56 PM

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, 12 September 2008 - 04:24 PM.


#26 trancexx

trancexx

    Queen F. Elizabeth MCXI

  • Active Members
  • PipPipPipPipPipPip
  • 5,987 posts

Posted 12 September 2008 - 04:06 PM

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

Maybe I'm in love (Sshh, it's a secret)

.

 

eMyvnE


#27 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 12 September 2008 - 04:22 PM

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

Attached Files


Edited by spudw2k, 12 September 2008 - 05:24 PM.


#28 arcker

arcker

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 581 posts

Posted 13 September 2008 - 11:24 AM

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 :

  • Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here

#29 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 15 September 2008 - 03:46 PM

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.

#30 BillLuvsU

BillLuvsU

    Useless Member

  • Active Members
  • PipPipPipPipPipPip
  • 1,561 posts

Posted 15 September 2008 - 10:20 PM

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

Posted Image

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

#31 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 15 September 2008 - 11:25 PM

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.
AutoIt         
$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, 15 September 2008 - 11:25 PM.


#32 BillLuvsU

BillLuvsU

    Useless Member

  • Active Members
  • PipPipPipPipPipPip
  • 1,561 posts

Posted 16 September 2008 - 01:45 AM

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.

Posted Image

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

#33 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 16 September 2008 - 03:07 AM

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.

#34 StAbb

StAbb

    Seeker

  • Active Members
  • 49 posts

Posted 16 September 2008 - 03:12 AM

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

#35 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 16 September 2008 - 05:25 AM

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, 16 September 2008 - 05:28 AM.


#36 arcker

arcker

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 581 posts

Posted 16 September 2008 - 07:32 AM

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 :

  • Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here

#37 trancexx

trancexx

    Queen F. Elizabeth MCXI

  • Active Members
  • PipPipPipPipPipPip
  • 5,987 posts

Posted 16 September 2008 - 10:02 AM

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, 16 September 2008 - 12:28 PM.

Maybe I'm in love (Sshh, it's a secret)

.

 

eMyvnE


#38 StAbb

StAbb

    Seeker

  • Active Members
  • 49 posts

Posted 16 September 2008 - 01:46 PM

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

#39 spudw2k

spudw2k

    passionately misinformed

  • Active Members
  • PipPipPipPipPipPip
  • 1,257 posts

Posted 16 September 2008 - 03:22 PM

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

#40 arcker

arcker

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 581 posts

Posted 16 September 2008 - 04:25 PM

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 :

  • Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here





Also tagged with one or more of these keywords: Service, 1053, 1061, compile

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users