Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a Windows service

Automation

How to create a Windows service

by  baltman  Posted    (Edited  )
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

[white]FoxPro programming rocks![/white]

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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top