Jump to content

_Service_UDF v4 : Build your own service with autoit code


arcker
 Share

Recommended Posts

wraithdu

please do :)

if they work well i'll add them in the main udf with pleasure

thx for your contribution.

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

  • 3 weeks later...

..great stuff you've coded here. Now, my problem:

I make 2 scripts, different services, different names and descriptions, but when I stop 1 the other stops too.

What needs changing to make'em work independently ?

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 2 weeks later...

Here's an update v0.3 to the AServiceMenu.au3 example; this one actually works while paying a little more respect to the MSDN way though, in that the user portion of the script is run as a separate process to the service portion.

That being said, in practice you will want the agent menu mode to run in the user's environment, and NOT just a run() as it is in this example, per the advice of Arker and ugdeen for security reasons.

The 'agent menu' portion of the script could definitely be handled better, since the example provides an unclean way of agent menu control by the service (right now the service just kills the menu unceremoniously when a user logs off =) it really should clean things up..)

You need to compile it with Arker's *updated* udf's for it to work..

Thanks Arker for updates to services.au3, this proves autoit can do ALL service functions natively!!!

Edit- see first post

Edited by ShminkyBoy
Link to comment
Share on other sites

FYI, on Win7 (maybe Vista) you have to start the 'Interactive Services Detection' - 'UI0Detect' service for interactive services to work.

Is this a new API call or sth?

I only have XP, I'll have to wait until I upgrade to go any further with Vista/Win 7 programming :)

Edited by ShminkyBoy
Link to comment
Share on other sites

  • 3 weeks later...

Ok, got a strange problem here ... The service start, runs to completion, throws the attached file as an error message. The FOR loop at the end was an attempt to let everything come to "rest" before stopping the service. The biggest stumper is that the error is random, seems to occur more frequently once it has appeared and has never happened once on my machine. Any ideas??

Func Main()
    If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
    If Not FileExists('C:\Boot_Metric\Timer_Event.exe') Then FileInstall('U:\AutoIt3\Scripts\BootMetric\Timer_Event.exe', 'C:\Boot_Metric\Timer_Event.exe', 1)

    Dim $keynames[10] = [9, 'Date', 'BootTime', 'Model', 'CPU', 'RAM', 'OS', 'MachineOU', 'UserID', 'ProfileSize']
    For $i = 1 To $keynames[0]
        RegWrite('HKLM\SOFTWARE\BootTimer', $keynames[$i], 'REG_SZ', '')
        Sleep(100)
    Next

    RegWrite('HKLM\SOFTWARE\BootTimer', 'LogUpdate', 'REG_SZ', '0')

    $duration = -1000000
    If IniRead(@ScriptDir & '\BootMetric.ini', 'Settings', 'Beep', '0') = '1' Or Not @Compiled Then
        $beep = True
    Else
        $beep = False
    EndIf

    Do
        If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
        Sleep(100)

        $query = _Service_QueryStatus('seclogon')
        If Not IsArray($query) Then ContinueLoop
    Until $query[1] = $SERVICE_RUNNING

    $duration = Int(_Date_Time_GetTickCount())
    If $beep Then _BeepTwice()

    While 1
        If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)

        Sleep(250)
        $user = _GetLoggedOn()

        If $user Then
            If $beep Then _BeepTwice()
            ExitLoop
        EndIf
    WEnd

    $timer = TimerInit()
    $pid = Run(@ScriptDir & '\BootTimerSvc.exe -process', @ScriptDir, @SW_HIDE)

    $sid = _Security__LookupAccountName($user, @ComputerName)
    Do
        If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
        Sleep(100)

        $profile = RegRead('HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\' & $sid[0], 'ProfileImagePath')
    Until $profile <> ''

    Do
        If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
        Sleep(100)
    Until FileExists($profile & '\Start Menu\Programs\Startup')

    If Not FileExists($profile & '\Start Menu\Programs\Startup\Timer_Event.lnk') Then FileCreateShortcut('C:\Boot_Metric\Timer_Event.exe', $profile & '\Start Menu\Programs\Startup\Timer_Event.lnk', 'C:\Boot_Metric')

    $trigger = IniRead(@ScriptDir & '\BootMetric.ini', 'Settings', 'Name', 'Timer_Event.exe')
    Do
        If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
        Sleep(100)
    Until ProcessExists($trigger)

    $duration += Int(TimerDiff($timer))
    If $beep Then _BeepTwice()

    Dim $hrs, $mins, $secs
    _TicksToTime($duration, $hrs, $mins, $secs)
    $write = _Now() & @TAB & StringFormat("%02i:%02i:%02i", $hrs, $mins, $secs) & @TAB

    $specs = _GetSpecs($user, $profile)
    For $i = 1 To $specs[0]
        $write &= $specs[$i] & @TAB
    Next
    $write = StringTrimRight($write, 1)

    If Not FileExists(@ScriptDir & '\' & @ComputerName & '.log') Then
        FileWriteLine(@ScriptDir & '\' & @ComputerName & '.log', 'Date/Time' & @TAB & 'Boot Time' & @TAB & 'Model' & @TAB & 'Processor' & @TAB & 'RAM' & @TAB & 'Operating System' & @TAB & 'Machine OUs' & @TAB & 'User ID' & @TAB & 'Profile Size')
        Sleep(500)
    EndIf

    FileWriteLine(@ScriptDir & '\' & @ComputerName & '.log', $write)
    Sleep(500)

    If _FileCountLines(@ScriptDir & '\' & @ComputerName & '.log') > 91 Then
        Dim $record
        If _FileReadToArray(@ScriptDir & '\' & @ComputerName & '.log', $record) Then
            Do
                _ArrayDelete($record, 2)
                $record[0] -= 1

                If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
                Sleep(100)
            Until $record[0] = 91
        EndIf

        If _FileWriteFromArray(@ScriptDir & '\' & 'temp.log', $record, 1) Then FileMove(@ScriptDir & '\' & 'temp.log', @ScriptDir & '\' & @ComputerName & '.log', 1)
    EndIf

    $regwrite = StringSplit($write, @TAB, 1)
    For $i = 1 To $keynames[0]
        RegWrite('HKLM\SOFTWARE\BootTimer', $keynames[$i], 'REG_SZ', $regwrite[$i])
        Sleep(100)
    Next

    RegWrite('HKLM\SOFTWARE\BootTimer', 'LogUpdate', 'REG_SZ', '1')
    Do
        Sleep(100)
        If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
    Until Not ProcessExists($pid)

    If Not @Compiled Then Exit

    For $i = 1 To 100
        If @Compiled Then _Service_ReportStatus($SERVICE_RUNNING, $NO_ERROR, 10000)
        Sleep(100)
    Next

    _Service_Stop($SERVICE_NAME)

    Exit
EndFunc   ;==>Main

post-10790-12613201556908_thumb.jpg

Link to comment
Share on other sites

It's impossible to tell where your error is with only what you've given us. Follow Arcker's advice, THEN come here and start a NEW topic in the HELP forum with your problem.

Did you declare all vars?

Do you have a function to clean up and close open handles before the script ends?

Do you inform the SCM when the script is stopping? (services are different than user processes, they don't like being stopped ungracefully)

Does this script run properly in application mode?

At first glance, instead of testing for @compiled in Main(), test for $SERVICE_RUNNING in Main() instead. You should only be testing for @compiled at script start, not over and over again in the Main(). (once you verify that @compiled = true at script start, it can not change state during run, no?)

So, sth like:

$query = _Service_QueryStatus('seclogon')
If Not IsArray($query) Then yourcleanupfunc()
IF $query[1] <> $Service_Running then yourcleanupfunc()

Then add a clean up func (ex.- sth like the cleanup routines in the arcker service example) that makes sure that all file handles are closed etc., THEN end.

Cleaning up BEFORE giving the script an opportunity to stop is a Good Idea, espesially since you seem to be doing registry writes.

This way when you run, the SCM can know whether the script is running or not, and if not, the script can inform the SCM, and you can clean up gracefully before exiting.

Hard to help you with only what you've given us.

And, this thread is for _service_UDF, please don't hijack; Start a new thread, you'll get better responses! Thanks! =)

Edited by ShminkyBoy
Link to comment
Share on other sites

I wonder - has anyone tried to use this UDF?

I did and it complains about 2 missing constants: $TOKEN_ADJUST_PRIVILEGES and $TOKEN_QUERY and another include - SecurityConstants.au3 - is nowhere to be found (it is not in the AutoIt default include pack, neither mentioned anywhere in this thread).

Anyone able to point me to SecurityConstants.au3? I presume that's the reason behind these 2 constants missing.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Have you actually tried to USE the udf? (download both the udf and the constants file) before answering to my post??

I guess not, because $TOKEN_ADJUST_PRIVILEGES and $TOKEN_QUERY are nowhere to be found.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Have you actually tried to USE the udf? (download both the udf and the constants file) before answering to my post??

I guess not, because $TOKEN_ADJUST_PRIVILEGES and $TOKEN_QUERY are nowhere to be found.

Then maybe you should read Constants.au3 before being crass, no?

That is where both of those constants reside.

(Scite offers a search function that will look for your terms in ANY FILE of the folder you select, I pointed it at AutoIt's \includes folder, and found them in Constants.au3)

Just add

#include <Constants.au3>

to your script.

And yes, it works, otherwise, myself and all these other people that ARE using it are imagining? hahaha Happy Holidays =)

Edited by ShminkyBoy
Link to comment
Share on other sites

  • 3 weeks later...

My bad about not searching all the includes.

Don't you think it would be better to add Constants.au3 with an #include-once in the udf? that would prevent other mistakes (since it is not obvious that constants.au3 needs to be included)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

hi all

UDF updated with Wraithdu updated script to work with Win7 and Unicode (thx a lot )

I've updated shminkyBoy's AServiceMenu too

@enaiman : sry for the inconvenience, didn't realised that i've not updated the correct securityconstants.au3

It's corrected now ;)

please give it a try.

and thx SHminkyBoy for his great stuff.

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

Um, I believe those are all my updates, not ShminkyBoy's (no offense), from work on my System Elevation script...

Oh ... what a mistake ...

soz

i replace it here ( ive mixed AserviceMenu and your elevation script )

Corrected now :D

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

Can someone attach the most recent version of the services UDF? Or better yet, add it to the library? The last update with a valid link was from December which doesn't appear to work on Win7. Also, every time I try to compile against services.au3 I get the following:

ERROR: _Svc_Main(): undefined function.

I even get that when compiling the example posted.

Thanks!

Edited by skreien
Link to comment
Share on other sites

Can someone attach the most recent version of the services UDF? Or better yet, add it to the library? The last update with a valid link was from December which doesn't appear to work on Win7. Also, every time I try to compile against services.au3 I get the following:

ERROR: _Svc_Main(): undefined function.

I even get that when compiling the example posted.

Thanks!

?? really weird ??

works for me

i've reposted the services.au3,service_example.au3 and service_constants.au3 for you.

Please give it a try.

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

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