INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Automation

How to create a Windows service by baltman
Posted: 23 Jul 06 (Edited 28 Sep 06)

I have expanded on Calvin Hsia's blog posting to create a fully automated example of setting up a service by automating the registry entry, service registration and service startup.

Please note that updating the registry more than once may require a reboot before the service will work.

Original blog can be found at:
http://blogs.msdn.com/calvin_hsia/archive/2004/12/13/282351.aspx

FoxPro programming rocks!

CODE

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&                                                             &&
&&  Save this code in a PRG in a folder along with srvany.exe  &&
&&  and instsrv.exe and run the PRG.                           &&
&&                                                             &&
&&  If sucessfully installed, "c:\vfpsrv.log" will be updated  &&
&&  every 5 seconds and will start itself at boot time.        &&
&&                                                             &&
&&  Brian                                                      &&
&&                                                             &&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

CLEAR
cDir=ADDBS(JUSTPATH(SYS(16)))
SET DEFAULT TO &cDir

Make_Registry_Entry()
Execute_Program([VFPSRV.REG])
Build_VFPSRV()
Execute_Program([VFPSRV.PRG])

DOEVENTS
cCmd= [RUN ] + cDir +[INSTSRV VFPSRV ] +cDir +[srvany.exe]
STRTOFILE(cCmd,[temp.txt])
EXECSCRIPT(cCmd)
DOEVENTS

RUN net start VFPSRV
*Run net stop VFPSRV

RETURN
PROCEDURE make_registry_entry
    SET TEXTMERGE ON
    SET TEXTMERGE TO ADDBS(JUSTPATH(SYS(16,1)))+[VFPSRV.REG]
    \\Windows Registry Editor Version 5.00
        \[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\VFPSrv\Parameters]
    \"AppDirectory"="<<STRTRAN(JUSTPATH(SYS(16,1)),[\],[\\])>>"
    \"Application"="<<STRTRAN(JUSTPATH(SYS(16,1)),[\],[\\])>>\\vfpsrv.exe"
    \"AppParameters"="myparm1 myparm2"
    SET TEXTMERGE TO
    SET TEXTMERGE OFF
ENDPROC

PROCEDURE Build_VFPSRV
    SET TEXTMERGE ON
    SET TEXTMERGE TO VFPSRV.PRG
    \\LPARAMETERS parm1,parm2

    \#define TIMERINT  5     && # of seconds for timer interval

    \PUBLIC oService
    \oService=NEWOBJECT("vfpsrv")
    \oService.logstr("Starting Service: got some params: "+;
    \    TRANSFORM(parm1)+" "+TRANSFORM(parm2))
        \IF _vfp.StartMode>0    && if weÆre running as an EXE
    \    READ EVENTS           && message loop
    \    SYS(2335,1)            && ignore logouts, jut keep on running
    \ENDIF
        \*Create VFPSRVSLAVE.PRG that writes a LOG (for now)
    \SET TEXTMERGE TO VFPSRVSLAVE.prg
    \     <<CHR(92)+CHR(92)>>LPARAMETERS oTimer,oService
    \     <<CHR(92)>>oService.logstr("")
    \SET TEXTMERGE TO
        \*RETURN
        \BUILD PROJECT vfpsrv from vfpsrv
    \MODIFY PROJECT vfpsrv nowait
    \STRTOFILE("screen=off"+CHR(13)+CHR(10)+;
    \    "resource=off"+CHR(13)+CHR(10),"config.fpw")
    \_vfp.ActiveProject.Files.Add("config.fpw")
    \_vfp.ActiveProject.Close
        \BUILD EXE vfpsrv FROM vfpsrv
    \ERASE config.fpw
        \DEFINE CLASS vfpsrv AS form
    \    PROCEDURE Init
    \        this.logstr(CURDIR())
    \        this.AddObject("mytimer","mytimer")
        \        WITH this.mytimer
    \            .enabled=.t.
    \            .interval=1000    && start it in a sec
    \        ENDWITH
    \    ENDPROC
        \    PROCEDURE logstr
    \        LPARAMETERS cStr
    \        cStr=TRANSFORM(DATETIME())+" "+cStr+CHR(13)+CHR(10)
    \        STRTOFILE(cStr,"c:\vfpsrv.log",.t.)
    \    ENDPROC
        \    PROCEDURE shutdown
    \        CLEAR EVENTS
    \    ENDPROC
    \ENDDEFINE
        \DEFINE CLASS mytimer AS timer
    \PROCEDURE timer
    \    DO ("C:\data\vfpservice\VFPSRVSLAVE.prg") with this,oService
    \    CLEAR PROGRAM
    \    dtNow=DATETIME()  && read datetime() and seconds()
    \    nSec=SECONDS()
    \    && target is seconds since midnight + calculation time
    \    nSec=CEILING((nsec+.5)/TIMERINT)*TIMERINT
    \    dtTarget=DTOT(DATE())+nsec
    \    this.interval=(dtTarget-dtNow) * 1000
    \ENDDEFINE
    SET TEXTMERGE TO
    SET TEXTMERGE OFF
ENDPROC

PROCEDURE execute_program
LPARAMETERS cFile
    DO CASE
        CASE UPPER(JUSTEXT(cFile))=[REG]
            DECLARE INTEGER ShellExecute IN Shell32.DLL ;
                INTEGER nWinHandle, STRING cOperation,STRING cFileName,;
                STRING cParameters, STRING cDirectory,INTEGER nShowWindow

            ShellExecute(0,"open",ADDBS(JUSTPATH(SYS(16,1)))+cFile,[],[],1)
            MESSAGEBOX([Is registry entry completed?])

        CASE UPPER(JUSTEXT(cFile))=[PRG]
            DO (ADDBS(JUSTPATH(SYS(16,1)))+cFile)
        
        OTHERWISE
            MESSAGEBOX([I don't know what to do with ]+cFile)
    ENDCASE
ENDPROC

Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close