Jump to content
Sign in to follow this  
amfony

Best Practice with writing a service

Recommended Posts

amfony

Hi All,

I have made a simple application that will:

screen scrape at every mouse click

dump jpg to local folder

on startup delete folders (of screen dumps) that are older then X

Very simple.

I want to 'service' this app so it can run in a lab with or without user logged on, load with the computer, and most importantly not fail.

What ive done so far:

created a service via the sc.exe command with the options of type = own and interact (with desktop) pointed it to the compiled script and ... error.

The service is not responding to the control function.

I attributed this to the app never ending as it will loop looking for mouseclick forever.

I then created a service pointing to a launcher.exe that simply calls the screen scraper exe and that is my current work around. What this doesnt do though it make my 'service' durable. If the screen scrape app fails (which it does for some reason) it does not restart as it it not the 'serviced' the launcher.exe is.

I think i have my thought process is wrong with writing a service in general, what a service executable should do (ie not loop) and how i can get the result i require.

TIA for any tips and direction.

AutoItSetOption("TrayIconHide", 1)
AutoItSetOption("TrayIconDebug", 1)

#Include <ScreenCapture.au3>
#Include <Misc.au3>
#Include <date.au3>
#Include <File.au3>
#Include <Array.au3>


$today = @YEAR&"-"&@MON&"-"&@MDAY

_ScreenCapture_SetJPGQuality(40)

$dll = DllOpen("user32.dll")

_FileListToArray("C:\captures")
If Not @error= 4 Then

    $Folders_Array = _FileListToArray("C:\captures")
    ;_ArrayDisplay($Folders_Array)

    $i = 0

    For $day in $Folders_Array

        ConsoleWrite(_DateDiff("D",$Folders_Array[$i],$today) & @CRLF)

        if _DateDiff("D",$Folders_Array[$i],$today) > 7 Then
            ConsoleWrite("deleting: "&"C:\CAPTURES\"&$day&@CRLF)
            DirRemove("C:\CAPTURES\"&$day,1)
        EndIf

        $i = $i + 1

    Next

EndIf


While 1
    Sleep ( 50 )
    If _IsPressed("01", $dll) Then ScreenCap()
    ;exit
WEnd

DllClose($dll)


;=================================================

Func ScreenCap()
    Local $hBmp


    $user = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon","DefaultUserName")


    if not FileExists("C:\captures\"&@YEAR&"-"&@MON&"-"&@MDAY&"\"&$user) Then DirCreate("C:\captures\"&@YEAR&"-"&@MON&"-"&@MDAY&"\"&$user)

    ; Capture full screen
    $hBmp = _ScreenCapture_Capture ("")

    ; Save bitmap to file
    _ScreenCapture_SaveImage ("C:\captures\"&@YEAR&"-"&@MON&"-"&@MDAY&"\"&$user&"\"&@HOUR&"-"&@MIN&"-"&@SEC&"-"&$user&".jpg", $hBmp)

EndFunc   ;==>_Main

Share this post


Link to post
Share on other sites
spudw2k

Check out this thread for how to compile a proper service script: http://www.autoitscript.com/forum/index.php?showtopic=80201&view=&hl=&fromsearch=1

Also, you'll have to think about the services running user. I believe by default (SYSTEM) would not be able to capture screenshots of a logged in user if it is not Interactive.

Edited by spudw2k

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  

×