Jump to content
Sign in to follow this  
arcker

_Service_UDF v4 : Build your own service with autoit code

Recommended Posts

Hi everybody,

first: That script is awesome. Thanks for that.

I copied the code from the example. Starting my script works properly. It runs with no problems.

Unfortunately I get the error 1061 "The service can not receive control messages" (translated from german) while exiting the service via services.msc.

I already tried the modification setting

_Service_ReportStatus($SERVICE_STOP_PENDING, $NO_ERROR, 1000);

to

_Service_ReportStatus($SERVICE_STOP_PENDING, $NO_ERROR, 0);

But still get the error.

Even in the service example and with the latest Service.au3 and ServiceConstants.au3. (See thread post)

I am using:

- AutoIT 3.3.8.1

- Windows 7 32Bit

- Windows 7 64Bit

- Windows Server 2003

- Windows Server 2008 R2

Any ideas? How did you guys solve that?

Thanks in advance.

Share this post


Link to post
Share on other sites

@ItsJustMe

Join the club, if you have read the thread you should have read more people have the same problem..

i did not find the solution

Good Luck

 

I have read it. ;-)

Maybe someone found a solution and did not post it here. ;-)

It is pretty sad, because I need one pretty urgent. :

The old version of the script exited well. But it does not work with the new version of AutoIT.

Just the new script version has the problem.

Share this post


Link to post
Share on other sites

I tested AutoIT v3.3.6.1 using the current version of the service script.
Result: Does not work, logically.

Using the old version of the service script (from 2010, I guess; see attached zip file).

Result: Worked. I just had to modify my script (the service start/stop initialization) according to my other program.

Like this:

; includes, etc.

_Service_init($SERVICE_NAME)
Func _Svc_Main()
    _WinAPI_WaitForSingleObject($service_stop_event, 0)
    While $gServiceStateRunning
        ; Enter your code here
                Sleep(1000)
    WEnd

    _Service_Cleanup()
EndFunc

exit

Now I do not know the new features in v3.3.8.1 but my script works fine.

Hope that helps you.

Services_and ServicesConstants_for_AutoIT3361.zip

Edited by ItsJustMe

Share this post


Link to post
Share on other sites

Hi,

Just to inform that i'm working on the solution.

The old version worked with events, I don't like too much it, but if it's the solution.

There is a problem while the service is declared as running. That should explained while

it hangs too while it's running.

I found some things, but not quite good for now.

Hope to fix it today  :zorro: 

edit 1 29/07/2013 :

Ok, that's amazing.

With the "old" version, everything is fine on Win7

With the new, i've fusionned some functions, and service stop bugs.

There is something to do with event, but I still don't understand why the old works.

Will post my findings later.

edit 2 :

I've found the problem. Really it was annoying.

I'll post the solution with optimized code soon.

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

It's fixed !

See the first post for the V3.

New script is easier, i've just let main function.

Please report here for bugs, i've activated mail notification so I'll be aware of post.

GUI FUNCTIONS ARE NOT SUPPORTED so they are removed.


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

Hi Arcker,

Thanks for the solution..i will try it

- Why don't you start a new thread...

- Or clean the first post

Edited by Emiel Wieldraaijer

Best regards,Emiel Wieldraaijer

Share this post


Link to post
Share on other sites

Hi Arcker,

Thanks for the solution..i will try it

- Why don't you start a new thread...

- Or clean the first post

... what do you mean

the first post is like this to reflect work but yes maybe it need cleaning. I'm not good at presentation :/

btw i'll wait for your tests. I haven't scripts to test on my side.


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

@arcker

Wednesday i'm back at home .. will take a look

Thnx

This first post is a bit  .. messy/grimy (don't know a good translation)

Edited by Emiel Wieldraaijer

Best regards,Emiel Wieldraaijer

Share this post


Link to post
Share on other sites

Hi Arcker,

Sorry for not replying a bit earlier

Your Example is not correct "Insert your running code here" is not in the correct place. Modified version below

Still getting the 1061 error when stopping the service.. on both Windows 8 X64 and Windows 7 X86

On Windows 8 X64  The console messages aren't displayed at the commandline but they are fired an closed direct afterwards.. this makes it hard to read 

I believe this is an Windows 8 problem because on Windows 7 X86 the problem does not occur

Correct Func _Main for your Example

Func _main($iArg, $sArgs)
local $count = 0
    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 --> insert your running code here
        if $count < 5 then logprint("main count" & $count & @crlf)

        $count += 1
        Sleep(1000)
        if $count = 4 then
        while 1
            if not $bServiceRunning then
                logprint("stop loop 4")
                ExitLoop
            EndIf
        WEnd
        endif
        #endregion  --> insert your running code here
    WEnd
EndFunc   ;==>main

Best regards,Emiel Wieldraaijer

Share this post


Link to post
Share on other sites

Is it normal that the service_example.exe running as a service takes so much CPU all the time (around 50%) ?

See in process explorer screenshot below :

service_example.jpg

Edited by Meganono

Share this post


Link to post
Share on other sites

Hi Arcker,

I'm using your UDF since 2 Years and it worked fine.

Because it worked so fine I extended my Service in the past. The Service is running on multiple OS (Win Xp to Win 2012).

My Problem is, on some machines and only sometimes I became an Application Popup when the Service stops. The Application Popup reports a read error on different memory range.

The service should check at startup if anything is to do, and stop by itself when not.

I've already tryed several methods to exit (exitloop(), _exit() function) but nothing worked.

What is wrong with my Service?

#NoTrayIcon
#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Snapshot.ico
#AutoIt3Wrapper_Outfile=d:\ADGSnapshot_V3.exe
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;~ #AutoIt3Wrapper_Version=beta
;~ Opt("MustDeclareVars", 1) ; just for self control  FPRIVATE "TYPE=PICT;ALT=smile.gif" dont to forget declare vars
Global $MainLog = @ScriptDir & "\Snapshot_Service.log"
FileDelete($MainLog)

#include <Winapi.au3> ;just for constants
#include "services.au3"
#include <Date.au3>
#include <Timers.au3> ; i used it for timers func

Global $sServiceName = "ADGSnapshot"
Global $config = @ScriptDir & "\config.ini"
Global $bDebug = TRUE

Func _main($iArg, $sArgs)
    #region --> insert your running code here
    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
        If FileExists(@ScriptDir & "\config.ini") Then
            If FileExists($config) Then
                Local $Szeit = IniRead($config, "Client", "Sicherungszeit", "")
                Local $Styp = IniRead($config, "Client", "Intervalltyp", "")
                Local $STage = IniRead($config, "Client", "Wochentage", "")
                Local $nachholen = IniRead($config, "Allgemein", "SnapNachholen", "")
                Local $dayarray, $aktweekday
                Local $mo, $di, $mi, $do, $fr, $sa, $so

                If $nachholen = 1 Then
                    Sleep(60000)
                    IniWrite($config, "Allgemein", "SnapNachholen", "0")
                    $runsnap = Run(@ScriptDir & "\ADGKomplettsicherung.exe startdelayed", @ScriptDir)
                    logPrint("main Snapshot Delayed " & $runsnap)
                    _Stopping()
                    Sleep(3000)
                    _exit()
                EndIf
                If $Szeit = "" Or $STage = "" Or $Styp = "" Then
                    _Stopping()
                    Sleep(3000)
                    _exit()
                Else
                    $Szeit = StringSplit($Szeit, ":", 2)
                    $Szeit[1] = StringLeft($Szeit[1], 2)

                    If Not StringInStr($STage, ",") = 0 Then
                        $dayarray = StringSplit($STage, ",", 2)
                        For $i = 0 To UBound($dayarray) - 1 Step 1
                            Select
                                Case $dayarray[$i] = 1
                                    $mo = 2
                                Case $dayarray[$i] = 2
                                    $di = 3
                                Case $dayarray[$i] = 3
                                    $mi = 4
                                Case $dayarray[$i] = 4
                                    $do = 5
                                Case $dayarray[$i] = 5
                                    $fr = 6
                                Case $dayarray[$i] = 6
                                    $sa = 7
                                Case $dayarray[$i] = 7
                                    $so = 1
                            EndSelect
                        Next
                    Else
                        Select
                            Case $STage = 1
                                $mo = 2
                            Case $STage = 2
                                $di = 3
                            Case $STage = 3
                                $mi = 4
                            Case $STage = 4
                                $do = 5
                            Case $STage = 5
                                $fr = 6
                            Case $STage = 6
                                $sa = 7
                            Case $STage = 7
                                $so = 1
                        EndSelect
                    EndIf

                    Select
                        Case $Styp = "Manuell" Or $Styp = "Wöchentlich"
                            If @WDAY = $mo Or @WDAY = $di Or @WDAY = $mi Or @WDAY = $do Or @WDAY = $fr Or @WDAY = $sa Or @WDAY = $so Then
                                If @HOUR = $Szeit[0] Then
                                    If @MIN = $Szeit[1] Then
                                        $runsnap = Run(@ScriptDir & "\ADGKomplettsicherung.exe start", @ScriptDir)
                                        logPrint("main Snapshot " & $runsnap)
                                        Sleep(60000)
                                    EndIf
                                EndIf
                            EndIf
                            Sleep(10000)
                        Case $Styp = "Monatlich"
                            If @MDAY > 0 And @MDAY < 8 Then
                                $aktweekday = _DateToDayOfWeek(@YEAR, @MON, @MDAY)
                                If $aktweekday = $mo Or $aktweekday = $di Or $aktweekday = $mi Or $aktweekday = $do Or $aktweekday = $fr Or $aktweekday = $sa Or $aktweekday = $so Then
                                    If @HOUR = $Szeit[0] Then
                                        If @MIN = $Szeit[1] Then
                                            $runsnap = Run(@ScriptDir & "\ADGKomplettsicherung.exe start", @ScriptDir)
                                            logPrint("main Snapshot " & $runsnap)
                                            Sleep(60000)
                                        EndIf
                                    EndIf
                                EndIf
                            EndIf
                            Sleep(10000)
                    EndSelect
                EndIf
            EndIf
        EndIf
    WEnd
    #endregion --> insert your running code here
EndFunc   ;==>_main

Thanks

Daniel

Share this post


Link to post
Share on other sites

When using the Services.au3 linked on the first page, it's better but not perfect:

#NoTrayIcon

Global $MainLog = @ScriptDir & "\test_service.log"
Global $sServiceName="Autoit_Service"


#include <Winapi.au3> ;just for constants
#include "services.au3"
#include <Timers.au3> ; i used it for timers func


Func _main($iArg, $sArgs)
   
   local $count = 0
   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
       logprint("main count" & $count)
       logprint("       $bServiceRunning: " & $bServiceRunning)    
       
       $count += 1
       Sleep(10000)
   WEnd
   logprint("stop")
   _Service_Cleanup()
   Exit(0)
EndFunc   ;==>main





_Service_init($sServiceName)
Exit(0)
    
    
    


; some loging func
Func logprint($text, $nolog = 0)
    If $nolog Then
        MsgBox(0, "MyService", $text, 1)
    Else
        If Not FileExists($MainLog) Then FileWriteLine($MainLog, "Log created: " & @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC)
        FileWriteLine($MainLog, @YEAR & @MON & @MDAY & " " & @HOUR & @MIN & @SEC & " [" & @AutoItPID & "] >> " & $text)
    EndIf
    Return 0
;~ ConsoleWrite($text & @CRLF)
EndFunc   ;==>logprint

$bServiceRunning is always true so I can never stop the service.

Moreover, if I comment the While/Wend, it logprints "stop", then call the Service_Cleanup which really stops the service but also triggers an error ( "The instruction at 0x0040c7ad referenced memory at 0x0000030. The memory could not be read."

Any idea for both of them?

Share this post


Link to post
Share on other sites

Error 1061 SOLVED! :D
 
After some Research i could figure out where the problem comes from. The function _Service_Ctrl() was never called (simple tested with a filewrite), so the script could not properly react to the Stop event.
 
Additionally i have added an exit function to give the chance that a program can exit properly (eg write a data file)
 
Updated files here: '?do=embed' frameborder='0' data-embedContent>>

Share this post


Link to post
Share on other sites

I have solved all my problems except the one when trying to stop my service (error 1061).

@bitboy: interesting, I will follow your path :)

Share this post


Link to post
Share on other sites

Error 1061 SOLVED! :D

 

After some Research i could figure out where the problem comes from. The function _Service_Ctrl() was never called (simple tested with a filewrite), so the script could not properly react to the Stop event.

 

Additionally i have added an exit function to give the chance that a program can exit properly (eg write a data file)

 

Updated files here: '?do=embed' frameborder='0' data-embedContent>>

 

I think you mean _Service_SetStopEvent that is never called... even when adding it, there is no change on my side :(

Share this post


Link to post
Share on other sites

that's weird, i didn't test my last update on WinXP and I just seen that it doesn't stop on WinXP ....

I'll see the fix too, thx for the research.

@bitboy, from my debug, the service ctrl is well called but there is something that is not executed after.

btw we can't see your link, access denied

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

@arcket: I'm not particularly good at DllStructCreate/DllStructSetData/DllCall and other stuff like this, but in _Service_Init the method "_Service_ServiceMain" is well defined and not "_Service_Ctrl".

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By SgtHugoStiglitz
      HI people,
                        I got a questions, I maintain code in autoIT and every time I compile a new version or modification, Scite generate 2 folders. One folder is finish by released and the other is tentative. I try to make some google search, but never find answer would satisfied myself. The dev who was maintain it before were using AutoIt3Wrapper_GUI, and I don't know if it's related. I look both folder, but they seem pretty identical, but I would like to know what it's the difference between those to folder. Anyone have a clue?
    • By Rhidlor
      I don't have any experience compiling AutoIt scripts or know of any best practices regarding said scripts so I was wondering, what are some of the options for passing data to complied scripts? Here's an example of what I mean by "option": Writing data to a text file, then running a script which reads the data from that text file.
      Thanks!
      I apologize if this topic is a little lackluster
    • By Skeletor
      Hey All,
      Yes, this is an odd request. 
      But is there a way to bulk up the size of the compiled program? 
      I know many want to reduce it's size, but I want to increase it.
      What methods do you suggest?
    • By dimm
      Is it possible to compile script without any icon resources included?
      When I make exe it uses default system icon of current OS even if I compile as console application.
      On other versions of Windows it looks a little strange

       
      Update:
      It seems that the only way is remove icons from exe after compilation using resource editors like ResHacker.

       
      another bad solution:
      Using corrupted 35 bytes icon file. Windows can't load it and shows default system icon.
      #pragma compile(Icon, broken.ico) broken.ico
    • By Dreamfire
      Hi,
      Since today, exe's are being flagged as having a trojan by Windows Defender (Fuery.B!cl)
      Version:  3.3.14.3 - SciTE Version 3.7.3



       

×
×
  • Create New...