下面的函數用於被服務執行或者使用
函數 描述
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 or
deleted 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
UnlockServiceDatabase
Build 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