Jump to content

_Service_UDF v4 : Build your own service with autoit code


arcker
 Share

Recommended Posts

Hi Arcker

Good news, i tested now with the current beta.

Additionally i installed the service on an Win2208 R2

Within 6 hours got these results:

No crash on Server 2008 (x86 and X64 comiled version)

One Appcrash with x64 compiled version on my Win 7 x64 Box 

Event ID 7011 is completly gone :)

Link to comment
Share on other sites

Hi guys,

that's really good news, thx for the feedback.

on xp I continue to get some issue.

on win7 it seems that the script hang after some time but i tested with an old version.

If you think it's good, I can publish this new code that would be for now the v4

@bitboy, did you use the last version that I posted ?

here is a new one ( clear some useless function, like setstopevent that is used for multitthread in C, not here )

Services.au3

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

yes i used the last version you provided yesterday.

On the Server 2008 System both services are still running without any issue.

Because it was asked to interact with the desktop there seems to be a way.

i added an example of CreateProcessAsUser function. if you start a Program with this function its displayed on the desktop.

So the Service Process can start a gui process, but afaik there is no easy way to interchange variables and values between two different processes (maybe with permanent reading/writing to an ini file).

Also i cannot say how rdp could be handled, or what happens if multiply users are working on the same machine.

Here is the function if someone would play around with (large code parts taken from here: '?do=embed' frameborder='0' data-embedContent>> ):

CreateProcessAsUser.au3

Link to comment
Share on other sites

hi bitboy, that's what I used for gui too :)

don't tell anyone how to do it since it's an hard process to make a gui since it uses another process and I don't want to talk

about it here, because it's not the first purpose :)

with the gui created in another process, then we can use interprocess ( use pipes or tcp instead of file ) to communicate between use process and service, that's how other program do.

Btw about the old script, it seems that I have less problem if I reboot the computer.

so for you I can post the update as it ?

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

ok last update

it seems the problem was the sleep, not threadsafe ( ??? )

so after 1 day with updated sleep function, no problem to stop it.

I don"t know why but it works. if we can't use internal function it's too bad :/

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

Is there any documentation that services has to be threadsafe?

i use an api-sleep but also a standard processexists function in the main loop and the service is running for 3 days now without any issues.

if i have time tomorrow i use your last update and a second script to permanently start and stop the service. So on monday i can see if any error occured.

Offtopic: in my opinion it doesn't make sense to show a gui within a service. so the createprocessasuser function is more a "yes its possible" than a "yes its useful" function. i think a better way is to have two "standalone" programs, one the service and one with a gui to control the service. In this case there is no trouble handling logon/logoff/RDP-Sessions and the user can decide by himself to start the gui-program or not.

Link to comment
Share on other sites

look the first post, I've already told about threadsafe :)

I'm not really sure that "threadsafe" is the right term.

The sleep function is the most called method in the loop, so maybe other function don't impact it as well.

I just don't know.

the thread safe doesn't correct the problem for xp, but the program is stable.

About the gui, it's useful when you have to control something that the service do, not interact directly.

All antivirus does it that way : one program for the taskbar, and the antivirus itself is the service.

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,

here are my test results.

Test system: Server 2008 R2 fully patched, Compiled with Autoit version 3.3.9.21 Beta

Start and Stop Events: 24299

Errors with x64 compiled Service: 2

Errors with x86 compiled Service: 1

Note: An error was logged if the service process was still running, 3 seconds after a service stop event.

In the windows logs i couldn't find anything because the log size was too small to cover the 3 days runtime

The main loop use a threadsafe sleep with 100ms

Func _Sleep($delay)
    Local $result = DllCall($hKernel32_DLL, "none", "Sleep", "dword", $delay)
EndFunc   ;==>_Sleep

and also a standard ProcessExsis (non threadsafe) and at startup an eventlog entry is created by the service (also non-threadsafe)

Func _CreateEvent($iEventType, $iEventID, $sEventMessage)
    Local $hEventLog, $aData[1] = [0]
    $hEventLog = _EventLog__Open("", "A3SW " & $sServiceName)
    _EventLog__Report($hEventLog, $iEventType, 0, $iEventID, @UserName, $sEventMessage, $aData)
    _EventLog__Close($hEventLog)
EndFunc   ;==>_CreateEvent

According to these results i would say its stable and there is no relation between stability and non-threadsafe function calls.

Link to comment
Share on other sites

from what i see, if I use sleep internal, it doesn't stop well after some time.

From recent test, I can see that it's something related to memory reservation.

That's why I recoded the engine to be more mem ok for the callbacks.

I don't know why sometimes, the service doesn't stop well but what I can see it's well executed,

so it's far better than before.

 

for one error on 24299, I can say it's not bad too :)

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

  • 2 months later...

Hi

I had some Troubles to get the Service stable.

The Service always stoped after 3 to 10 seconds.

Its the same 32 or 64 bit compiled. In the Eventlog there is an error , displayed at the end.

I took the Service example and put in this code in the _main Function

Func _main($iArg, $sArgs)
    Local $l_count=0
    Local $l_waitsec = 1
...

#Region --> insert your running code here
    $l_count += 1
    IF (($l_count / $l_waitsec) == int ($l_count / $l_waitsec)) Then
        logprint(" Service Running since: " & $l_count & " sec")
    EndIf
    IF $l_count > 10 and $l_count <60 Then
        $l_waitsec = 5
    EndIf
    IF $l_count > 600 and $l_count <600 Then
        $l_waitsec = 10
    EndIf
    _Sleep(1000)
#EndRegion --> insert your running code here

 

In the Log File with debug on I see

20140123 092945 [9744] >> Report Status received with = 3
and the Service stoped.

My System Server 2008 R2 on Xenserver virtuaisized

Could anyone help me ?

Regards

The Logfile Content:

 

 Log created: 2014/01/23 10:08:12
20140123 100812 [10532] >> script started
20140123 100812 [10532] >> goinig to install
20140123 100812 [10532] >> Installing Service
20140123 100812 [10532] >> InstallService(): Installation of service successful
20140123 100832 [6092] >> script started
20140123 100832 [6092] >> _Service_Init(AAAu3ServiceExample32)
20140123 100832 [6092] >> RegisterServiceCtrlHandlerEx
20140123 100832 [6092] >> Report Status received with = 2
20140123 100832 [6092] >> dwCheckPoint => 2
20140123 100832 [6092] >> main start
20140123 100832 [6092] >> Report Status received with = 4
20140123 100832 [6092] >>  Service Running since: 1 sec
20140123 100833 [6092] >>  Service Running since: 2 sec
20140123 100834 [6092] >>  Service Running since: 3 sec
20140123 100835 [6092] >>  Service Running since: 4 sec
20140123 100836 [6092] >>  Service Running since: 5 sec
20140123 100837 [6092] >>  Service Running since: 6 sec
20140123 100838 [6092] >>  Service Running since: 7 sec
20140123 100839 [6092] >>  Service Running since: 8 sec
20140123 100840 [6092] >> Report Status received with = 3
20140123 100840 [6092] >> dwCheckPoint => 3
20140123 100840 [6092] >> seems service stopping
20140123 100840 [6092] >> Report Status received with = 1
20140123 100840 [6092] >> main stopped. Cleanup.

 Eventlog

Faulting application name: ServiceExample_v4.exe, version: 0.0.0.0, time stamp: 0x52e0d2a2
Faulting module name: unknown, version: 0.0.0.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x00000000012100fe
Faulting process id: 0x2610
Faulting application start time: 0x01cf181542a725c0
Faulting application path: C:\Program Files\XMLParser\ServiceExample_v4.exe
Faulting module path: unknown
Report Id: 836073a0-8408-11e3-b497-1272a138bc02

 

 

Edited by mko
Link to comment
Share on other sites

  • 6 months later...

   Reading through this thread someone asked if current compiled scripts (3.3.12.0) could run natively as a service. I believe the response was yes. If that is the case couldn't you just take your currently compiled script and install it as a service with the SC command?

   If that is not the case, in laymans terms, what to these "wrappers" do to the exes and why?

Link to comment
Share on other sites

After three days of searching yes. What ever you call them. Shell, Wrapper, Helper whatever. Somewhere in this thread I came across the statement the AutoIT scripts are "Servive Compatible" and the SC command is all you need. I am finding out that is totally incorrect. SC (and several other utilities) will physically put the exe in to the list of services but getting that service to run properly at that point is a whole nother ball game.

So this thread is a wrapper isn't it? If I can get this wrapper to work (which I can't) I can then use a number of different utilities to add it to Service Manager right?

Link to comment
Share on other sites

Hi Arcker,

first of all great code. I found a small bug though. If I create the service without specifying a name for the service ("test.exe -i"), when I remove the service ("test.exe -u"), the program works fine. If I create the service with a name for the service ("test.exe -i AA"), when I try to remove the service ("test.exe -u AA"), the program does not work and the service is not removed.

Edited by Adams

Adams

Link to comment
Share on other sites

I am having problems period. Can't get anything to work as advertised.

When I try and compile ServiceExample_ThreadSafe I get warnings and errors to the effect that $servive_stop_event is used before declaration and is an undeclared global variable. Where is the include file for that?

When I compile ServiceExample_V4 it compiles fine but when I run it with the /I option it does nothing. No error messages at all. Doesn't create the service. Doesn't do anything.

Edited by Trax
Link to comment
Share on other sites

  • 3 weeks later...

Hi,

installing, running, everything works perfect. Thanks a lot for the great work.

I have one problem: If I use the _ServiceRunning of the ServiceControl.au3 function my service created with this UDF v4 will be stopped by leaving the While $bServiceRunning Loop. Is there any way to prevent the service from stopping?

This is the code of the ServiceRunning function:

;===============================================================================
; Description:   Checks if a service is running on a computer
; Parameters:    $sComputerName - name of the target computer. If empty, the local computer name is used
;                $sServiceName - name of the service to check
; Requirements:  None
; Return Values: On Success - 1
;                On Failure - 0
; Note:          This function relies on the fact that only a running service responds
;                to a SERVICE_CONTROL_INTERROGATE control code. Check the ControlService
;                page on MSDN for limitations with using this method.
;===============================================================================
Func _ServiceRunning($sComputerName, $sServiceName)
   Local $hAdvapi32
   Local $arRet
   Local $hSC
   Local $hService   
   Local $bRunning = 0

   $hAdvapi32 = DllOpen("advapi32.dll")
   If $hAdvapi32 = -1 Then Return 0
   $arRet = DllCall($hAdvapi32, "long", "OpenSCManager", _
                    "str", $sComputerName, _
                    "str", "ServicesActive", _
                    "long", $SC_MANAGER_CONNECT)
   If $arRet[0] <> 0 Then
      $hSC = $arRet[0]
      $arRet = DllCall($hAdvapi32, "long", "OpenService", _
                       "long", $hSC, _
                       "str", $sServiceName, _
                       "long", $SERVICE_INTERROGATE)
      If $arRet[0] <> 0 Then
         $hService = $arRet[0]
         $arRet = DllCall($hAdvapi32, "int", "ControlService", _
                          "long", $hService, _
                          "long", $SERVICE_CONTROL_INTERROGATE, _
                          "str", "")
         $bRunning = $arRet[0]
         DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hService)
      EndIf
      DllCall($hAdvapi32, "int", "CloseServiceHandle", "long", $hSC)
   EndIf
   DllClose($hAdvapi32)   
   Return $bRunning
EndFunc
Link to comment
Share on other sites

  • 3 months later...

Error 1053: The service did not respond

Searching Google for this error returns a lot of hits describing many different solutions.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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