Creating a VFP application as a service:

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





“AppParameters”=”myparm1 myparm2”



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

LPARAMETERS parm1,parm2





*To get SrvAny and SrvInst: go to, 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


PUBLIC oService


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

      \LPARAMETERS oTimer,oService







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

ERASE config.fpw


DEFINE CLASS vfpsrv AS form

      PROCEDURE Init


            DECLARE integer MessageBeep IN WIN32API integer


            WITH this.mytimer


                  .interval=1000    && start it in a sec


      PROCEDURE Destroy



      PROCEDURE logstr(str as String)

            str=TRANSFORM(DATETIME())+” “+str+CHR(13)+CHR(10)




DEFINE CLASS mytimer AS timer

      PROCEDURE timer

            DO (“vfpsrvrtn” ) with this,oService

            CLEAR PROGRAM

            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






Comments (31)

  1. Sometimes it’s useful to run some code in response to an event like somebody locking or unlocking your…

  2. Mandeep says:

    I am not able to understand the application of this article. Please help.

    Thanks in advance.

  3. RoadRunner says:

    I need create windows service with vfp90 … i make this sample step by step … but i have a problem when starting service this message appear.

    Error 1053: El servicio no ha respondido a la peticion o inicio del control en un tiempo adecuado

  4. RoadRunner says:

    <p>I need create windows service with vfp90 … i make this sample step by step

    … but i have a problem when starting service this message appear.<br>


    <span style="background-color: #FFFF00"><b>Error 1053: El servicio no ha

    respondido a la peticion o inicio del control en un tiempo adecuado</b></span><br>


  5. Sergey Berezniker says:

    The Windows Shell Object can be used to control Windows services from within VFP:

    oShell = CREATEOBJECT("Shell.Application")

    ? oShell.IsServiceRunning("VfpSrv")

    ? oShell.ServiceStart("VfpSrv", .F.)

    ? oShell.ServiceStop("VfpSrv", .T.)

  6. amidonbill says:

    I would like some ideas on how to have a vfp service respond to events in a Clipper application.

    I’m trying to sync data from a Clipper App to a VFP app – and would like to trigger the VFP Service "Update" routine with some kind of call from the Clipper app.

    It would run anytime I do a DbCommit() in the Clipper App.  

  7. Vali says:

    I have tried the code above but seems that I cannot start the service.

    "Cannot start the MYSERVICE service on LocalComputer. Error 1053: The service did not respond to the start request in a timely fashion"

    Should the service exe be created in any special way? like a com exe server?

    Many Thanks

  8. deciacco says:


    I can’t seem to get the Destroy event to fire when the service is stopped? Is this normal or do I need to be doing something diff. Any ideas?


  9. It takes a lot of work to create the blog posts and code samples that I put in my blog, and I was curious

  10. Hoppins says:


    I can run the prg and it works but when I go to start my service I get a message: "…started and then stopped. Some services stop automatically if they have no work to do. For example: The performance logs and alerts service."  Any advice.  Thanks

  11. kasmattaa says:

    Посетите наш развлекательный сайт На нем есть новые фильмы, музыка, скрипты, игры, картинки, обои, эротика фото, эротика видео, книги!

    И все это для Вас БЕСПЛАТНО!

    Welcome to our site

    U can see many FREE movies, soft, music, games, pictures, wallpapers, erotics, erotic video, books.

  12. Calvin,

    Thanks for putting together this example and documentary…  I will be testing and implementing a VFP appl. service with the help of this blog entry.

  13. john newman says:

    Calvin, thanks for the example.  This works great on XP pro for me as a service, and in application mode on 2003 server.  However, in 2003 server it loads as a service but does’nt seem to run properly (ie. simple test string written out won’t even work)  Do you know of a security change made recently to 2003 server that would .exe as a service?  



  14. dave banton says:

    Is it possible to handle the stop and start events of the service from within the vfp code?

  15. What the solution to the error: "Error 1053: The service did not respond to the start or control request in a timely fashion."?

  16. tihomir666 says:

    Hi everybody,

    It is a great tool. I manage to start the code on win XP and its work great but I had a problem start it on a windows Vista. I had switched off UAC and start the tool to creat a service. The result is that the service is created succesfull. When I start the service  it start without broblem but the vfp exe is not started. Has anyone know something about it?

    Thanks in advance

  17. Christian B says:

    When I run the .exe directly from the command line, it runs through its entire process, but when I try to start it as a service, I only get the following:


    C:>net start dmcwebsync

    The dmcWebSync service is starting.

    The dmcWebSync service could not be started.

    The service did not report an error.

    More help is available by typing NET HELPMSG 3534.



    Any suggestions as to how to debug the process?  I have code in the Error method of the class that is running in the app as well as the startup .prg of the app that writes to an error/log file.  The log file is created when when I run the .exe from the command line, but not when I attempt to start the service.  The very first lines of code of the startup .prg is creating/writing to the log, and the log is not being created when I attempt to start the service.

    Any ideas???


    Christian B.

  18. Mican says:


    I have been searching an answer to my problem. Unfortunately, I haven’t found one.

    I just developed an application to run as a service. I am having problem running this application in Windows Server 2008.

    When I run the application manually, that is double-clicking the exe file from windows explorer, the my application work perfectly. However, when the applcation is run as a service, it will just stopped at some point in the application and will not continue the whole process.

    I have already set the service properties to use my admin account as service log-on.

    Any thoughts will be highly appreciated.

    Kind Regards,


  19. Archon says:

    Why Don't you just let your .exe run on start up?

    I think that's convenient with same results.

    I made a cafe client software, and it goes fine controlling everything from the clients.

    Even monitoring screen content and program usage.

  20. Osain says:


    Gracias por compartir estos conocimientos. Me fueron de mucha utilidad.

  21. dipendra says:

    Thanks for advance.

    how can run vfp as service on Windows 2008 / windows 2012 server ?

  22. SweDel says:

    I have the same problem, it runs fine on Win7, but will not start on Win2008 server.