Wiki Home

Windows Service


Namespace: WIN_COM_API
A Windows Service is basically a program with no (or very little) user interface which provides programmatic services on the computer on which it is running. (These services can be used by other programs on the same computer, or by other computers across a network).
In Windows 95/98/ME, there is very little difference between a Service and any other application.
In Windows NT/2k/XP, Services are registered in the "service control manager database" (presently in the Windows registry), and can be managed (started, stopped, restarted, configured) through the Services mode of the Computer Management Console.

Functions to manipulate services: WinApi Service Functions
To programmatically register a Service: CreateService
An article about how services work: http://www.windowsitpro.com/Windows/Article/ArticleID/8943/8943.html
Does anyone know how to restart a Windows Service from VFP?
I am guessing that this is done via an API call, but have been unable to find out how from any of the API - via - FoxPro resources that I have.
You would want to first get a handle to the service: Open ServiceOffsite link to http://msdn.microsoft.com/library/en-us/dllproc/base/openservice.asp
Then you could send the SERVICE_CONTROL_STOP message using: Control ServiceOffsite link to http://msdn.microsoft.com/library/en-us/dllproc/base/controlservice.asp
Next, you would restart the service using: Start ServiceOffsite link to http://msdn.microsoft.com/library/en-us/dllproc/base/startservice.asp
And, finally, you'd want to close the handle using: Close Service HandleOffsite link to http://msdn.microsoft.com/library/en-us/dllproc/base/closeservicehandle.asp

See the WinApi Service Functions and related pages for how to use these functions.
- wgcs

Below you'll find a sample code for checking status of Windows Service (SQL Server service). It'doesn't include any error checking. For more details and API functions that allows to stop/start service see MSDN at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/service_functions.asp

* Service Control Manager object specific access types
#define SC_MANAGER_CONNECT             0x0001
#define SC_MANAGER_CREATE_SERVICE      0x0002
#define SC_MANAGER_ENUMERATE_SERVICE   0x0004
#define SC_MANAGER_LOCK                0x0008
#define SC_MANAGER_QUERY_LOCK_STATUS   0x0010
#define SC_MANAGER_MODIFY_BOOT_CONFIG  0x0020

* Service object specific access type
#define SERVICE_QUERY_CONFIG           0x0001
#define SERVICE_CHANGE_CONFIG          0x0002
#define SERVICE_QUERY_STATUS           0x0004
#define SERVICE_ENUMERATE_DEPENDENTS   0x0008
#define SERVICE_START                  0x0010
#define SERVICE_STOP                   0x0020
#define SERVICE_PAUSE_CONTINUE         0x0040
#define SERVICE_INTERROGATE            0x0080
#define SERVICE_USER_DEFINED_CONTROL   0x0100

* Service State -- for CurrentState
#define SERVICE_STOPPED                0x00000001
#define SERVICE_START_PENDING          0x00000002
#define SERVICE_STOP_PENDING           0x00000003
#define SERVICE_RUNNING                0x00000004
#define SERVICE_CONTINUE_PENDING       0x00000005
#define SERVICE_PAUSE_PENDING          0x00000006
#define SERVICE_PAUSED                 0x00000007

DECLARE Long OpenSCManager IN Advapi32 ;
	  STRING lpMachineName, STRING lpDatabaseName, Long dwDesiredAccess
DECLARE Long OpenService IN Advapi32 ;
	  Long hSCManager, String lpServiceName, Long dwDesiredAccess
DECLARE Long QueryServiceStatus IN Advapi32 ;
	  Long hService, String @ lpServiceStatus
DECLARE Long CloseServiceHandle  IN Advapi32 ;
	  Long hSCObject

lhSCManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT + SC_MANAGER_ENUMERATE_SERVICE)
IF lhSCManager = 0
	* Error
ENDIF
lcServiceName = "MSSQLSERVER"
lhSChandle = OpenService(lhSCManager, lcServiceName, SERVICE_QUERY_STATUS)
IF lhSCManager = 0
	* Error
ENDIF
lcQueryBuffer = REPLICATE(CHR(0), 4*7 )
lnRetVal = QueryServiceStatus(lhSChandle, @lcQueryBuffer )
IF lnRetVal = 0
	* Error
ENDIF
* Close Handles
CloseServiceHandle(lhSChandle)
CloseServiceHandle(lhSCManager)
lnServiceStatus = ASC(SUBSTR(lcQueryBuffer,5,1))
IF lnServiceStatus <> SERVICE_RUNNING
	* Service isn't running
	? "Service isn't running"
ENDIF	

Where can I find constant definitions for use with WINAPI calls from FoxPro?
example:
#define SC_MANAGER_CREATE_SERVICE 0x0002

oops never mind I see them in WIN32API.TXT. (was looking for WIN32API.H )
Now my question is, how do I convert items such as &H2 to a value that I can use? Good question, I'd like to know as well. I imagine it's as simple as doing a hex-to-decimal conversion, but an example would confirm. - William Fields

Replace &H with 0x - that's zero-smallx. ? 0xFF && '255' -- tr
Puedes saber si el servicio esta corriendo de la siguiente forma ... el ejemplo es con MySQL

Os1 = CreateObject("Shell.Application")
?oS1.IsServiceRunning("Mysql")

Y si deseas arrancarlo  .... el segundo parámetro es para que guarde la configuración

Os1 = CreateObject("Shell.Application")
?oS1.ServiceStart("Mysql", .T.)

Otra forma de conocer si se esta ejecutando es através de WMI

objWMIService = GetObject("winmgmts:\\")

colServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name = 'MSSQLServer'")

If colServices.Count > 0 Then
    For Each objService in colServices

        ? "SQL Server esta " + objService.State + "."
    Next
Else
    ? "SQL Server no esta instalado en el PC."
EndIf

David Amador Tapia
WebMaster de "La Web de Davphantom"
www.davphantom.net

See Also: Windows Services
Contributors: Sergey Berezniker, wgcs
( Topic last updated: 2007.09.19 10:11:43 AM )