Since my API memory is a little rusty, anyone know off hand how to simply stop and start a service in NT?
Posted on 2001-02-15 11:45:00 by Schwartzteufel
ControlService
The ControlService function sends a control code to a Win32-based service. 

BOOL ControlService(
  SC_HANDLE hService,  // handle to service
  DWORD dwControl,     // control code
  LPSERVICE_STATUS lpServiceStatus 
                       // pointer to service status structure
);
 
Parameters
hService 
Handle to the service. This handle is returned by the OpenService or CreateService function. The access required for this handle depends on the dwControl code requested. 
dwControl 
Specifies the requested control code. This value can be one of the standard control codes in the following table: Value Meaning 
 
SERVICE_CONTROL_STOP Requests the service to stop. The hService handle must have SERVICE_STOP access. 
SERVICE_CONTROL_PAUSE Requests the service to pause. The hService handle must have SERVICE_PAUSE_CONTINUE access. 
SERVICE_CONTROL_
CONTINUE Requests the paused service to resume. The hService handle must have SERVICE_PAUSE_CONTINUE access. 
SERVICE_CONTROL_
INTERROGATE Requests the service to update immediately its current status information to the service control manager. The hService handle must have SERVICE_INTERROGATE access. 
SERVICE_CONTROL_
SHUTDOWN The ControlService function fails if this control code is specified. 


This value can also be a user-defined control code, as described in the following table: Value Meaning  
Range 128 to 255. The service defines the action associated with the control code. The hService handle must have SERVICE_USER_DEFINED_CONTROL access.  



lpServiceStatus 
Pointer to a SERVICE_STATUS structure to receive the latest service status information. The information returned reflects the most recent status that the service reported to the service control manager. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError. 

Errors
The following error codes can be set by the service control manager. Other error codes can be set by the registry functions that are called by the service control manager. 

Value Meaning 
ERROR_ACCESS_DENIED 
 The specified handle was not opened with the necessary access. 
ERROR_DEPENDENT_SERVICES_RUNNING 
 The service cannot be stopped because other running services are dependent on it. 
ERROR_INVALID_SERVICE_CONTROL 
 The requested control code is not valid, or it is unacceptable to the service. 
ERROR_SERVICE_CANNOT_ACCEPT_CTRL 
 The requested control code cannot be sent to the service because the state of the service is SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING. 
ERROR_SERVICE_NOT_ACTIVE 
 The service has not been started. 
ERROR_SERVICE_REQUEST_TIMEOUT 
 The service did not respond to the start request in a timely fashion. 


Remarks
The ControlService function asks the service control manager to send the requested control code to the service. The service control manager sends the code if the service accepts the control and if the service is in a state in which a control can be sent to it. You cannot stop and start a service unless the security descriptor allows you to. The default security descriptor allows LocalSystem, Administrators, and Power Users to stop and start services. To change the security descriptor of a service, use SetServiceObjectSecurity. 

The QueryServiceStatus or function returns a SERVICE_STATUS structure whose dwCurrentState and dwControlsAccepted members indicate the current state and controls accepted by a running service. All running services accept the SERVICE_CONTROL_INTERROGATE control code by default. Each service specifies the other control codes that it accepts when it calls the SetServiceStatus function to report its status. A service should always accept these codes when it is running, no matter what it is doing. 

The following table shows the action of the service control manager in each of the possible service states: 

Service state Stop Other controls 
STOPPED (c) (c)  
STOP_PENDING (b) (b
Posted on 2001-02-15 12:16:00 by Hiroshimator
OpenService
The OpenService function opens a handle to an existing service. 

SC_HANDLE OpenService(
  SC_HANDLE hSCManager,  // handle to service control manager 
                         // database
  LPCTSTR lpServiceName, // pointer to name of service to start
  DWORD dwDesiredAccess  // type of access to service
);
 
Parameters
hSCManager 
Handle to the service control manager database. The OpenSCManager function returns this handle. 
lpServiceName 
Pointer to a null-terminated string that names the service to open. The maximum string length is 256 characters. The service control manager database preserves the case of the characters, but service name comparisons are always case insensitive. Forward-slash (/) and backslash (\) are invalid service name characters. 
dwDesiredAccess 
Specifies the access to the service. Before granting the requested access, the system checks the access token of the calling process against the discretionary access-control list of the security descriptor associated with the service object. 
The STANDARD_RIGHTS_REQUIRED constant enables the following service object access types: Standard rights Description 
DELETE Enables calling of the DeleteService function to delete the service. 
READ_CONTROL Enables calling of the QueryServiceObjectSecurity function to query the security descriptor of the service object. 
WRITE_DAC|WRITE_OWNER Enables calling of the SetServiceObjectSecurity function to modify the security descriptor of the service object. 



You can specify any or all of the following service object access types: Access Description 
SERVICE_ALL_ACCESS Includes STANDARD_RIGHTS_REQUIRED in addition to all of the access types listed in this table. 
SERVICE_CHANGE_CONFIG Enables calling of the ChangeServiceConfig function to change the service configuration. 
SERVICE_ENUMERATE_DEPENDENTS Enables calling of the EnumDependentServices function to enumerate all the services dependent on the service. 
SERVICE_INTERROGATE Enables calling of the ControlService function to ask the service to report its status immediately. 
SERVICE_PAUSE_CONTINUE Enables calling of the ControlService function to pause or continue the service. 
SERVICE_QUERY_CONFIG Enables calling of the QueryServiceConfig function to query the service configuration. 
SERVICE_QUERY_STATUS Enables calling of the QueryServiceStatus function to ask the service control manager about the status of the service. 
SERVICE_START Enables calling of the StartService function to start the service. 
SERVICE_STOP Enables calling of the ControlService function to stop the service. 
SERVICE_USER_DEFINED_CONTROL Enables calling of the ControlService function to specify a user-defined control code. 



You can specify any of the following generic access types: Generic access Service access 
GENERIC_READ Combines the following access types: STANDARD_RIGHTS_READ, SERVICE_QUERY_CONFIG, SERVICE_QUERY_STATUS, and SERVICE_ENUMERATE_DEPENDENTS. 
GENERIC_WRITE Combines the following access types: STANDARD_RIGHTS_WRITE and SERVICE_CHANGE_CONFIG. 
GENERIC_EXECUTE Combines the following access types: STANDARD_RIGHTS_EXECUTE, SERVICE_START, SERVICE_STOP, SERVICE_PAUSE_CONTINUE, SERVICE_INTERROGATE, and SERVICE_USER_DEFINED_CONTROL. 



Return Values
If the function succeeds, the return value is a handle to the service.

If the function fails, the return value is NULL. To get extended error information, call GetLastError. 

Errors
The following error codes can be set by the service control manager. Others can be set by the registry functions that are called by the service control manager. 

Error code Meaning 
ERROR_ACCESS_DENIED The specified service control manager database handle does not have access to the service. 
ERROR_INVALID_HANDLE The specified handle is invalid. 
ERROR_INVALID_NAME The specified service name is invalid. 
ERROR_SERVICE_DOES_NOT_EXIST The specified service does not exist. 


Remarks
The returned handle is only valid for the process that called OpenService. It can be clo
Posted on 2001-02-15 12:20:00 by Hiroshimator
CreateService
The CreateService function creates a service object and adds it to the specified service control manager database. 

SC_HANDLE CreateService(
  SC_HANDLE hSCManager,  // handle to service control manager 
                         // database
  LPCTSTR lpServiceName, // pointer to name of service to start
  LPCTSTR lpDisplayName, // pointer to display name
  DWORD dwDesiredAccess, // type of access to service
  DWORD dwServiceType,   // type of service
  DWORD dwStartType,     // when to start service
  DWORD dwErrorControl,  // severity if service fails to start
  LPCTSTR lpBinaryPathName,  // pointer to name of binary file
  LPCTSTR lpLoadOrderGroup,  // pointer to name of load ordering 
                             // group
  LPDWORD lpdwTagId,     // pointer to variable to get tag identifier
  LPCTSTR lpDependencies,  // pointer to array of dependency names
  LPCTSTR lpServiceStartName,
                           // pointer to account name of service
  LPCTSTR lpPassword       // pointer to password for service account
);
 
Parameters
hSCManager 
Handle to the service control manager database. This handle is returned by the OpenSCManager function and must have SC_MANAGER_CREATE_SERVICE access. 
lpServiceName 
Pointer to a null-terminated string that names the service to install. The maximum string length is 256 characters. The service control manager database preserves the case of the characters, but service name comparisons are always case insensitive. Forward-slash (/) and back-slash (\) are invalid service name characters. 
lpDisplayName 
Pointer to a null-terminated string that is to be used by user interface programs to identify the service. This string has a maximum length of 256 characters. The name is case-preserved in the service control manager. display name comparisons are always case-insensitive. 
dwDesiredAccess 
Specifies the access to the service. Before granting the requested access, the system checks the access token of the calling process. 
The STANDARD_RIGHTS_REQUIRED constant enables the following service object access types: Standard rights Description 
DELETE Enables calling of the DeleteService function to delete the service. 
READ_CONTROL Enables calling of the QueryServiceObjectSecurity function to query the security descriptor of the service object. 
WRITE_DAC|WRITE_OWNER Enables calling of the SetServiceObjectSecurity function to modify the security descriptor of the service object. 



You can specify any or all of the following service object access types: Access Description 
SERVICE_ALL_ACCESS Includes STANDARD_RIGHTS_REQUIRED in addition to all of the access types listed in this table. 
SERVICE_CHANGE_CONFIG Enables calling of the ChangeServiceConfig function to change the service configuration. 
SERVICE_ENUMERATE_DEPENDENTS Enables calling of the EnumDependentServices function to enumerate all the services dependent on the service. 
SERVICE_INTERROGATE Enables calling of the ControlService function to ask the service to report its status immediately. 
SERVICE_PAUSE_CONTINUE Enables calling of the ControlService function to pause or continue the service. 
SERVICE_QUERY_CONFIG Enables calling of the QueryServiceConfig function to query the service configuration. 
SERVICE_QUERY_STATUS Enables calling of the QueryServiceStatus function to ask the service control manager about the status of the service. 
SERVICE_START Enables calling of the StartService function to start the service. 
SERVICE_STOP Enables calling of the ControlService function to stop the service. 
SERVICE_USER_DEFINED_CONTROL Enables calling of the ControlService function to specify a user-defined control code. 



You can specify any of the following generic access types: Generic access Service access 
GENERIC_READ Combines the following access types: STANDARD_RIGHTS_READ, SERVICE_QUERY_CONFIG, SERVICE_QUERY_STATUS, and SERVICE_ENUMERATE_DEPENDENTS. 
GENERIC_WRITE Combines the following access types: STANDARD_RIGHTS_WRITE and SERVICE_CHANGE_CONFIG.
Posted on 2001-02-15 12:26:00 by Hiroshimator
StartService
The StartService function starts a service. 

BOOL StartService(
  SC_HANDLE hService,            // handle of service
  DWORD dwNumServiceArgs,        // number of arguments
  LPCTSTR *lpServiceArgVectors   // array of argument strings 
                                 // string pointers
);
 
Parameters
hService 
Handle to the service. This handle is returned by the OpenService or CreateService function, and it must have SERVICE_START access. 
dwNumServiceArgs 
Specifies the number of argument strings in the lpServiceArgVectors array. If lpServiceArgVectors is NULL, this parameter can be zero. 
lpServiceArgVectors 
Pointer to an array of pointers that point to null-terminated argument strings passed to a service. Driver services do not receive these arguments. If no arguments are passed to the service being started, this parameter can be NULL. The service accesses these arguments through its ServiceMain function. The first argument (argv[0]) is the name of the service by default, followed by the arguments, if any, in the lpServiceArgVectors array. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError. 

Errors
The following error codes can be set by the service control manager. Others can be set by the registry functions that are called by the service control manager. 

Value Meaning 
ERROR_ACCESS_DENIED The specified handle was not opened with SERVICE_START access. 
ERROR_INVALID_HANDLE The specified handle is invalid. 
ERROR_PATH_NOT_FOUND The service binary file could not be found. 
ERROR_SERVICE_ALREADY_RUNNING An instance of the service is already running. 
ERROR_SERVICE_DATABASE_LOCKED The database is locked. 
ERROR_SERVICE_DEPENDENCY_DELETED The service depends on a service that does not exist or has been marked for deletion. 
ERROR_SERVICE_DEPENDENCY_FAIL The service depends on another service that has failed to start. 
ERROR_SERVICE_DISABLED The service has been disabled. 
ERROR_SERVICE_LOGON_FAILED The service could not be logged on. 
ERROR_SERVICE_MARKED_FOR_DELETE The service has been marked for deletion. 
ERROR_SERVICE_NO_THREAD A thread could not be created for the service. 
ERROR_SERVICE_REQUEST_TIMEOUT The service did not respond to the start request in a timely fashion. 


Remarks
When a driver service is started, the StartService function does not return until the device driver has finished initializing. 

When a Win32-based service is started, the service control manager spawns the service process, if necessary. If the specified service shares a process with other services, the required process may already exist. The StartService function does not wait for the first status update from the new service, because it can take a while. Instead, it returns when the service control manager receives notification from the service control dispatcher that the ServiceMain thread for this service was created successfully. 

The service control manager sets the following default status values before returning from StartService: 

Current state of the service is set to SERVICE_START_PENDING. 
Controls accepted is set to none (zero). 
The CheckPoint value is set to zero. 
The WaitHint time is set to 2 seconds. 
The calling process can determine if the new service has finished its initialization by calling the QueryServiceStatus function periodically to query the service's status. 

A service cannot call StartService during initialization. The reason is that the service control manager locks the service control database during initialization, so a call to StartService will block. Once the service reports to the service control manager that it has successfully started, it can call StartService. 

QuickInfo
  Windows NT: Requires version 3.1 or later.
  Windows: Unsupported.
  Windows CE: Unsupported.
  Header: Declared in winsvc.h.
  Import Library: Use advapi32.lib.
  Unicode: Implemented as Unicode and ANSI versions on W
Posted on 2001-02-15 12:28:00 by Hiroshimator
this should you get further I guess This message was edited by Hiroshimator, on 2/15/2001 12:52:59 PM
Posted on 2001-02-15 12:49:00 by Hiroshimator