服务函数编程
下面的函数用于被服务执行或者使用
函数 描述
Handler An application-defined callback function used with the RegisterServiceCtrlHandler function.
HandlerEx
RegisterServiceCtrlHandler 注册一个函数处理控制码请求。
RegisterServiceCtrlHandlerEx
ServiceMain 服务程序入口函数。
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.
下面的函数被用于管理和配置服务函数 描述
ChangeServiceConfig 改变服务的开机运行状态。
ChangeServiceConfig2 改变服务的描述。
CloseServiceHandle 关闭服务句柄。
ControlService 在一个服务已经被开启的情况下,向这个服务发出控制码。
ControlServiceEx
CreateService 创建一个服务对象,并增加它到服务控制管理数据库。
DeleteService 在服务控制管理数据库中标示要删除的服务。
EnumDependentServices 获取服务管理数据库中所有服务的名称和当前状态。
EnumServicesStatusEx
GetServiceDisplayName 获取服务的描述。
GetServiceKeyName Retrieves the service name of the specified service.
NotifyBootConfigStatus Reports the boot status to the service control manager.
NotifyServiceStatusChange Enables an application to receive notification when the specified service is created ordeleted or when its status changes.
OpenSCManager 和指定机器的服务控制管理器建立连接并打开服务控制管理器数据库。
OpenService 打开一个存在的服务。
QueryServiceConfig
QueryServiceConfig2
QueryServiceObjectSecurity Retrieves a copy of the security descriptor associated with a service object.
QueryServiceStatusEx 查询服务程序现在的运行状态。
SetServiceObjectSecurity Sets the security descriptor of a service object.
StartService 开启一个服务。废弃函数
下面的函数已经被废弃。
EnumServicesStatus
LockServiceDatabase
QueryServiceLockStatus
QueryServiceStatus
UnlockServiceDatabaseBuild date: 12/3/2009
程序例子:
#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"快速缓存服务,为网络文件交换提供缓存,提高网络连接速度。";
//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
补充:软件开发 , C语言 ,