Jump to content

_Service_UDF v4 : Build your own service with autoit code


arcker
 Share

Recommended Posts

I'm wondering why not everbody can access the file (maybe someone can tell me where is the upload button for attachments... )

So here is the updated code:

Func _Service_ServiceMain($iArg, $pArgs)
Local $ret = DllCall($hAdvapi32_DLL, "ptr", "RegisterServiceCtrlHandlerExW", "ptr", DllStructGetPtr($tServiceName), "ptr", DllCallbackGetPtr($tServiceCtrl), "ptr", 0) ;register service

the first line is commented out in the file in post 1. but it should be enabled.

In function _Service_Ctrl i added the following to the Stop Case

_Service_ReportStatus($SERVICE_STOP_PENDING, $NO_ERROR, 3000)
_Service_SetStopEvent()
_exit() ; <- Give the script the chance to exit properly
_Service_Cleanup()

Sadly there is one issue left.

After some time the Service is running, the main loop seems to stop without reason.

In the Eventlog "System" the following error is raised: Source: Service Control Manager; Event ID 7011

"A timeout (30000 milliseconds) was reached while waiting for a transaction response from the A3Test_Service service."

Link to comment
Share on other sites

that's really weird and i must apologize. It seems that my last update wasn't good and I erased all correction added.

I did some SH** with my dropbox so i've to retry all the last correction.

@bhamp0

I remember something like this, and i remember that correst registerservice to registerservceex and i can't see those correction ....

i Wonder what i've done ....

i'm working by night at my job so i've some time and i'll try to correct it asap

thx for your investigation btw :)

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

what i can see here is that sometimes, the _service_ctrl is not called so there is a problem with a declaration of it.

@bitboy, i found an older version that lead to what you posted, and that seems better, but i've the same problem as you.

My fear is that it could be Inside autoit. I'll test more.

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

"The ServiceMain function should immediately call the RegisterServiceCtrlHandlerEx function to specify a HandlerEx function to handle control requests. Next, it should call the SetServiceStatus function to send status information to the service control manager. After these calls, the function should complete the initialization of the service. Do not attempt to start another service in the ServiceMain function"

so i'm good here.

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

mmm ok so when the process fails to stop, all the whole service doesn't work too.

I've a log that is created at each start and when the service stops it fails too.

Here are my guess : the service is not well declared to scm, or it fails internally but the program

continues to run.

so autoit problem ( i've seen it since first version of my udf ) or the udf ?

i'm pretty sure that a struct or something is not well done.

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

wow there is some hard issue.
I'm pretty this is callback related but ...
sometimes when i try to stop the program, the log shows

20131012 075519 [1956] >> ** SCM command received => 1
20131012 075519 [1956] >> program started

so it shows the beginning ? how that could happen ?
i think f wring memory adress here but i don't know how to detect it.

edit :

20131012 080322 [4608] >> _Service_Init(Autoit_Service)
20131012 080322 [4608] >> Report Status received with = 2
20131012 080322 [4608] >> Report Status received with = 4
20131012 080322 [4608] >> main start
20131012 080322 [4608] >> main count0
20131012 080323 [4608] >> main count1
20131012 080324 [4608] >> main count2
20131012 080325 [4608] >> main count3
20131012 080326 [4608] >> main count4
20131012 080327 [4608] >> main count5
20131012 080328 [4608] >> main count6
20131012 080329 [4608] >> ** SCM command received => 1
20131012 080329 [4608] >> Received stop command for service
20131012 080329 [4608] >> Report Status received with = 3
20131012 080329 [4608] >> program started



it's always so there is something wrong in Relationship between scm and service.
the whole exe is loaded each time i call ctrl from scm ? dunno.
the "program started" should happen each time ? dunno
i'll rework the service main.

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 now i manage to always execute the loop but the stop command fails too with random errors.

the scm doesn't call well the _service_ctrl

edit :

seems to work much better.

all that i can say is that it's better to do nothing when service exiting, even writing a log.

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

Hi arcker

thx for you work.

i'm curently investigating in the Event Error. This happened on an X64 System with a 64Bit compiled script. yesterday i tested at home with 32Bit compiled script on a 32it system. The service was running stable over night. I can't say if thats realy the reason. For the moment i'll just let it run and see if it stops working again.

Update: Seems that i cannot reproduce the error with x86 compiled script. The service is running "rock solid" for hours now without any issue. I'll cross check on monday with the X64 system.

Edited by bitboy
Link to comment
Share on other sites

here what i found ( i'll post the new code tomorrow ) :

=> comment out the two lines with dllcallbackfree ( I Wwnder why i put it here )

=> put the registerctrlhandler Inside main function

at exit ( stop )

at the end of main function :

=> dllcallbackfree

=> setreportstatus service stopped with no wait ( set 3000 to 0 )

=> exit

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

Hi guys

can you try this ?

compile & run debug_server and see if hello happens when start service

then you can run stop & start with benchmark.cmd

i noticed that my crashed was due that my log is synchronised with dropbox.

i will test those for win vista + now.

With my test now i have always the main function that is executed.

Service crash when stopped  1/30 test

thx

 

Services.rar

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

@bitboy

I tuned a little the services.au3.

On xp it's ok if we don't stop & restart the service too quickly ( i set a pause of 3 sec between ). No more crash.

On win7 it's not the same. The script doesn't handle well the stop and often hang.

I'm searching, hope to post a new version within an hour.

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

Hello arcker,

this event occurs randomly, with NO stop request send by me or the SCM. Just seems like the main loop hangs or stops somewhere.

At my Win7 box at home i don't have that issue. So i don't think this issue is related to the services.au3

Link to comment
Share on other sites

the services are running fine on the two system i have.

Bitboy, do you use last beta version ( 3.3.9.21 )? It seems to fix some memory error.

try with attached files please.

btw it doesn't fix the random errors i have too

they are wayyy rare too. so it's better, not perfect, but better.

ServiceExample_v3.au3

Services.au3

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

I have a special version of Services.au3 that is working on Win Server 2008 R2, so I won't change anything for the moment (I should probably clean things up a bit).

For those who are looking for a service template (as I got some issue while using example presented on this topic), here is mine:

#NoTrayIcon


#include <Winapi.au3>
#include <Process.au3>
#include "Services.au3"



Global $sServiceName = "MyServiceName"

Global $MainLog = @ScriptDir & "\" & $sServiceName & ".log"
FileDelete($MainLog)

;;;;;;;;;;;;;;;;;;;;;
; GLOBAL PARAMETERS ;
;;;;;;;;;;;;;;;;;;;;;
Global $parameter1 = "Value1"
Global $parameter2 = "Value2"
Global $parameter3 = "Value3"





Func _Main($iArg, $sArgs)
   
   
   _Service_ReportStatus($SERVICE_START_PENDING, $NO_ERROR, 10)
   
   
   ; INIT SERVICE
   logprint("pre-init")   
   ;;;;; Init all things
   ;;;;; Check processes if necessary
   logprint("post-init")
   
   
   ; START SERVICE
   logprint("pre-start")
   ;;;;; Do something: start a process, ...
   logprint("post-start")
   
   
   _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 30)
   
   
   ; SERVICE UP - LOOPING
   While $bServiceRunning
      Sleep(30 * 1000)      ; Wait 30 seconds

      ;;;;; If necessary, do something, like checking the service should still be up

      _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 30)

   WEnd
   
   
   _Service_ReportStatus($SERVICE_STOP_PENDING, $NO_ERROR, 10)
   
   
   ; STOP SERVICE
   logprint("pre-stop")
   ;;;;; Clean things up
   logprint("post-stop")
   
   
   _Service_ReportStatus($SERVICE_STOPPED, $NO_ERROR, 0)
   
   
   Exit(0)
   
   
EndFunc   ;==>main



 
logprint("service initialization: " & $sServiceName)
_Service_Init($sServiceName)
Exit(0)





Func _WriteError($errorMessage)
   ConsoleWriteError(@YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " [" & @AutoItPID & "] >> " & $errorMessage & @CRLF)
   logprint($errorMessage)
EndFunc

Func logprint($textMessage)
   FileWriteLine($MainLog, @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " [" & @AutoItPID & "] >> " & $textMessage & @CRLF)
EndFunc   ;==>logprint
Link to comment
Share on other sites

ok no more crash on win7 but on winXP sometimes the error, but only at stop.

i've redone the services.au3 to take care of new nested struct features. Doesn't change the problem on winxp

but it's easier to understand in the code :)

edit : I can confirm that on win7, after +500 stop & start, no more crash

not the same issue on WinXP but I don't think it's autoit related now, and it only happens at stop

please post your results too :)

here the updated services.au3

Services.au3

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

Func _Main($iArg, $sArgs)
#region -- STOP -- Service Start
_Service_ReportStatus($SERVICE_START_PENDING, $NO_ERROR, 10)
_Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 30)
If $bDebug Then Debug("***** SERVICE START *******")


If $bDebug Then Debug("Started Main.")


$bServiceRunning = True
#endregion -- STOP Service Start
While $bServiceRunning ; REQUIRED  ( dont change variable name ) ; there are several ways to find that service have to be stoped - $Running flag in loop is the first method


#region --> mycode
_Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 30)
; code.......
#endregion --> mycode


WEnd


_Service_ReportStatus($SERVICE_STOP_PENDING, $NO_ERROR, 10)
_Service_ReportStatus($SERVICE_STOPPED, $NO_ERROR, 0)
Sleep(1000)
ProcessClose(@AutoItPID)
Return
EndFunc

Working perfectly with code from yesterday at 4 PM  on Win7 x64... Start, Stop and Restart! Thx!

 

Log: 

15.10.2013 13:56:38:080 [5124] : ***** SERVICE START *******
15.10.2013 13:56:38:086 [5124] : Started Main.
15.10.2013 13:56:41:592 [5124] : main stopped. Cleanup.
15.10.2013 13:58:50:102 [12056] : _Service_Init(MYSERVICE)
15.10.2013 13:58:50:111 [12056] : RegisterServiceCtrlHandlerEx
15.10.2013 13:58:50:118 [12056] : dwCheckPoint => 2
15.10.2013 13:58:50:124 [12056] : main start
15.10.2013 13:58:50:130 [12056] : dwCheckPoint => 3
15.10.2013 13:58:50:137 [12056] : ***** SERVICE START *******
15.10.2013 13:58:50:159 [12056] : Started Main.
15.10.2013 13:58:53:205 [12056] : dwCheckPoint => 4
15.10.2013 13:58:57:020 [11004] : _Service_Init(MYSERVICE)
15.10.2013 13:58:57:027 [11004] : RegisterServiceCtrlHandlerEx
15.10.2013 13:58:57:034 [11004] : dwCheckPoint => 2
15.10.2013 13:58:57:040 [11004] : main start
15.10.2013 13:58:57:046 [11004] : dwCheckPoint => 3
15.10.2013 13:58:57:053 [11004] : ***** SERVICE START *******
15.10.2013 13:58:57:059 [11004] : Started Main.
15.10.2013 13:58:59:444 [11004] : dwCheckPoint => 4
15.10.2013 13:58:59:451 [11004] : main stopped. Cleanup.
15.10.2013 13:59:18:055 [11868] : _Service_Init(MYSERVICE)
15.10.2013 13:59:18:063 [11868] : RegisterServiceCtrlHandlerEx
15.10.2013 13:59:18:070 [11868] : dwCheckPoint => 2
15.10.2013 13:59:18:076 [11868] : main start
15.10.2013 13:59:18:082 [11868] : dwCheckPoint => 3
15.10.2013 13:59:18:088 [11868] : ***** SERVICE START *******
15.10.2013 13:59:18:094 [11868] : Started Main.
15.10.2013 13:59:46:666 [11868] : dwCheckPoint => 4
15.10.2013 14:01:04:803 [13064] : _Service_Init(MYSERVICE)
15.10.2013 14:01:04:811 [13064] : RegisterServiceCtrlHandlerEx
15.10.2013 14:01:04:818 [13064] : dwCheckPoint => 2
15.10.2013 14:01:04:825 [13064] : main start
15.10.2013 14:01:04:831 [13064] : dwCheckPoint => 3
15.10.2013 14:01:04:838 [13064] : ***** SERVICE START *******
15.10.2013 14:01:04:845 [13064] : Started Main.
15.10.2013 14:01:17:734 [13064] : dwCheckPoint => 4
15.10.2013 14:01:23:968 [9200] : _Service_Init(MYSERVICE)
15.10.2013 14:01:23:975 [9200] : RegisterServiceCtrlHandlerEx
15.10.2013 14:01:23:982 [9200] : dwCheckPoint => 2
15.10.2013 14:01:23:988 [9200] : main start
15.10.2013 14:01:23:993 [9200] : dwCheckPoint => 3
15.10.2013 14:01:24:000 [9200] : ***** SERVICE START *******
15.10.2013 14:01:24:006 [9200] : Started Main.
15.10.2013 14:01:27:136 [9200] : dwCheckPoint => 4
15.10.2013 14:01:28:575 [3976] : _Service_Init(MYSERVICE)
15.10.2013 14:01:28:582 [3976] : RegisterServiceCtrlHandlerEx
15.10.2013 14:01:28:589 [3976] : dwCheckPoint => 2
15.10.2013 14:01:28:595 [3976] : main start
15.10.2013 14:01:28:601 [3976] : dwCheckPoint => 3

...
Edited by simi4
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...