Sometimes it’s useful to make your application run as a service. A service can run without any user logged in, can automatically start upon reboot, and can survive user logoffs. It can also run as a different user with different access rights.
Normally, Creating a service is fairly complex, but creating a VFP application as a service is fairly straightforward, thanks to a pair of tools called INSTSRV.EXE and SRVANY.EXE that come with the Windows Resource Kit. INSTSRV.EXE takes 2 parameters: the name of the new service to install, and the full path to the SRVANY.EXE file.
For example, to create a service called “VFPSrv”, I ran this on my Windows XP machine:
C:\Program Files\Windows Resource Kits\Tools>instsrv VFPSrv "d:\Program Files\Windows Resource Kits\Tools\srvany.exe"
SRVANY provides all the main work to pretend to be a service, and it will look in the registry to find what EXE file to run to implement the service. The EXE name, the starting directory, and any optional parameters are found in the registry.
Copy/paste the following to a file named VFPSRV.REG (change the paths as necessary: note the double back-slashes) and dbl-click the .REG file to add these entries to the registery.
Windows Registry Editor Version 5.00
You can use the VFP command window to control the service:
!net start vfpsrv
!net stop vfpsrv
or the DOS command window:
net start vfpsrv
The sample service code below executes a file called “vfpsrvrtn.prg” when the timer fires. However, this code is not built into the EXE. This means you can change the service’s behavior dynamically, without having to stop/rebuild/start the service.
The code executes the timer event every 5 seconds, aligned to the 5 second real time clock (so that it occurs at exactly :00, :05, :10, … seconds past the hour).
If you run the prg below, it will run as a normal VFP PRG, but it will also build VFPSRV.EXE.
To start it as a service, use the control panel->Administrative Tools->Services or “net start vfpsrv”
Open the log file in an automatically refreshing editor, such as Visual Studio, from another machine and then log off the main machine. You can still hear the beeps and see the log file changing when no user is logged in.
(using older versions of SRVANY or on older versions of Windows, you may need to use SYS(2340) )
*Save this content to a file called VFPSRV.PRG
*To get SrvAny and SrvInst: go to http://www.microsoft.com/downloads, enter Windows 2003 Resource Kit Tools in the Keywords field, and click Go.
*Then, click the Windows Server 2003 Resource Kit Tools Download button at the Windows Server 2003 Resource Kit Tools
*Web page to download rktools.exe-which contains the most recent versions of Instsrv and Srvany-and run the executable to install the tools on your system.
#define TIMERINT 5 && # of seconds for timer interval. Also syncs to TIMERINT seconds for time of day
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
*Create VFPSRVRTN.PRG that just beeps
SET TEXTMERGE TO vfpsrvrtn.prg
SET TEXTMERGE TO
BUILD PROJECT vfpsrv from vfpsrv
MODIFY PROJECT vfpsrv nowait
STRTOFILE("screen=off"+CHR(13)+CHR(10),"config.fpw") && to hide the VFP runtime desktop
BUILD EXE vfpsrv FROM vfpsrv
DEFINE CLASS vfpsrv AS form
DECLARE integer MessageBeep IN WIN32API integer
.interval=1000 && start it in a sec
PROCEDURE logstr(str as String)
DEFINE CLASS mytimer AS timer
DO ("vfpsrvrtn" ) with this,oService
dtNow=DATETIME() && read datetime() and seconds() close to the same instant
nsec=CEILING((nsec+.5)/TIMERINT)*TIMERINT && target is # of seconds since midnight + a little for calculation time
this.interval=(dtTarget-dtNow) * 1000