Service Function Programming

Source: Internet
Author: User

The following functions are used for service execution or use.

Function Description

Handler An application-defined callback function used with the RegisterServiceCtrlHandler function.
HandlerEx
RegisterServiceCtrlHandler registers a function to process control code requests.
RegisterServiceCtrlHandlerEx
ServiceMain service program entry function.
SetServiceBits Registers a service type with the service control manager and the Server service.
SetServiceStatus Updates the service control managers status information for the calling service.
StartServiceCtrlDispatcher Connects the main thread of a service process to the service control manager.


The following functions are used to manage and configure services.

Function Description

ChangeServiceConfig changes the service startup status.
ChangeServiceConfig2 changes the service description.
CloseServiceHandle closes the Service handle.
When a service is enabled, ControlService sends a control code to the service.
ControlServiceEx
CreateService creates a service object and adds it to the service control management database.
DeleteService indicates the service to be deleted in the service control management database.
EnumDependentServices obtains the names and current status of all services in the service management database.
EnumServicesStatusEx
GetServiceDisplayName gets the service description.
GetServiceKeyName Retrieves the service name of the specified service.
Yybootconfigstatus Reports the boot status to the service control manager.
Policyservicestatuschange Enables an application to receive notification when the specified service is created or

Deleted or when its status changes.
OpenSCManager establishes a connection with the Service Control Manager of the specified Machine and opens the Service Control Manager database.
OpenService opens an existing service.
QueryServiceConfig
QueryServiceConfig2
QueryServiceObjectSecurity Retrieves a copy of the security descriptor associated with a service object.
QueryServiceStatusEx queries the current running status of the service program.
SetServiceObjectSecurity Sets the security descriptor of a service object.
StartService starts a service.

Deprecated Functions

The following functions have been deprecated.

EnumServicesStatus
LockServiceDatabase
QueryServiceLockStatus
QueryServiceStatus
UnlockServiceDatabase

Build date: 12/3/2009

Program example:

# Include <windows. h>
# Include <stdio. h>

Bool Start_Service (wchar_t * ServiceName );
Bool Stop_Service (wchar_t * ServiceName );
Bool Create_Service (wchar_t * ServiceName );
Bool Delete_Service (wchar_t * ServiceName );
Void ReconfigureService (wchar_t * ServiceName, wchar_t * ServiceDes );
Void ChangeServiceRun (wchar_t * ServiceName );
Void EnumService (void );

Int main ()
{
Wchar_t * ServiceDisp = L "quick cache service, which provides cache for network file exchange and improves network connection speed. ";
// Start_Service (L "WmdmPmSN ");
// Stop_Service (L "WmdmPmSN ");
// Create_Service (L "ServiceTest ");
// Delete_Service (L "ServiceTest ");
// ReconfigureService (L "ServiceTest", ServiceDisp );
ChangeServiceRun (L "WmdmPmSN ");
EnumService ();
Return 0;
}

Bool Start_Service (wchar_t * ServiceName)
{
SC _HANDLE schSCManager = OpenSCManager (NULL, NULL, SC _MANAGER_ALL_ACCESS );
If (NULL! = SchSCManager)
{
// L "WmdmPmSN"
SC _HANDLE schService = OpenService (schSCManager, ServiceName, SERVICE_ALL_ACCESS );
If (NULL! = SchService)
{
If (StartService (schService, 0, NULL ))
{
CloseServiceHandle (schService );
CloseServiceHandle (schSCManager );
Return 1;
}
CloseServiceHandle (schService );
CloseServiceHandle (schSCManager );
Wprintf (L "Start Service failed! ");
Return 0;
}
CloseServiceHandle (schSCManager );
Wprintf (L "Open Service failed! ");
Return 0;
}
Wprintf (L "OpenSCManager failed! ");
CloseServiceHandle (schSCManager );
Return 0;
}

Bool Stop_Service (wchar_t * ServiceName)
{
SC _HANDLE schSCManager = OpenSCManager (NULL, NULL, SC _MANAGER_ALL_ACCESS );
If (NULL! = SchSCManager)
{
// L "WmdmPmSN"
SC _HANDLE schService = OpenService (schSCManager, ServiceName, SERVICE_ALL_ACCESS );
If (NULL! = SchService)
{
SERVICE_STATUS ServiceStatus;
If (ControlService (schService, SERVICE_CONTROL_STOP, & ServiceStatus ))
{
CloseServiceHandle (schService );
CloseServiceHandle (schSCManager );
Return 1;
}
CloseServiceHandle (schService );
CloseServiceHandle (schSCManager );
Wprintf (L "Start Service failed! ");
Return 0;
}
CloseServiceHandle (schSCManager );
Wprintf (L "Open Service failed! ");
Return 0;
}
Wprintf (L "OpenSCManager failed! ");
CloseServiceHandle (schSCManage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.