以下是引用不懂才问在2019-10-23 08:19:16的发言:
自动校对时间,可以忽略不计吧,上下也不会差一两分钟
如果对时间要求高,取本机时间可能不可靠(人为改动、时钟失电),有可能在正常使用时突然被关机。
windows环境的系统服务编程参考示例:
/*
scm.c
系统服务编程(以管理员身份运行)
*/
#include <stdio.h>
#include <windows.h>
//获取基本服务信息
BOOL _GetServiceStatus(SC_HANDLE hScm,
LPENUM_SERVICE_STATUS pServices,
DWORD cbBufSize,
LPDWORD lpServicesReturned)
{
DWORD cbBytesNeeded = 0;
DWORD ResumeHandle = 0;
return EnumServicesStatus(hScm, SERVICE_WIN32, SERVICE_STATE_ALL,
pServices, cbBufSize,
&cbBytesNeeded, lpServicesReturned, &ResumeHandle);
}
//获取详细服务信息
BOOL _GetServiceConfig(SC_HANDLE hScm,
LPENUM_SERVICE_STATUS pServices,
DWORD ServiceNum,
LPQUERY_SERVICE_CONFIG pServicesInfo,
DWORD cbBufSize)
{
SC_HANDLE hService = NULL;
DWORD cbBytesNeeded = 0;
BOOL ret = FALSE;
if ((hService = OpenService(hScm, pServices[ServiceNum].lpServiceName, SERVICE_ALL_ACCESS)) != NULL)
{
ret = QueryServiceConfig(hService, pServicesInfo, cbBufSize, &cbBytesNeeded);
CloseServiceHandle(hService);
}
return ret;
}
//列印服务信息
void _PrintServices(LPENUM_SERVICE_STATUS pServices,
DWORD ServiceNum,
LPQUERY_SERVICE_CONFIG pServicesInfo)
{
printf("\n%d\n", ServiceNum);
printf("服务名称: %s\n", pServices[ServiceNum].lpServiceName);
printf("显示名称: %s\n", pServices[ServiceNum].lpDisplayName);
printf("启动状态: ");
switch(pServices[ServiceNum].ServiceStatus.dwCurrentState)
{
case SERVICE_PAUSED:
{
printf("暂停");
break;
}
case SERVICE_RUNNING:
{
printf("运行");
break;
}
case SERVICE_STOPPED:
{
printf("停止");
break;
}
default:
{
printf("其它");
}
}
printf("\n程序路径: %s\n", pServicesInfo->lpBinaryPathName);
printf("启动类型: ");
switch(pServicesInfo->dwStartType)
{
case SERVICE_AUTO_START:
{
printf("自动\n");
break;
}
case SERVICE_DEMAND_START:
{
printf("手动\n");
break;
}
case SERVICE_DISABLED:
{
printf("禁止\n");
break;
}
default:
{
printf("其它\n");
}
}
}
//枚举服务信息
void _EnumServices(SC_HANDLE hScm)
{
DWORD cbBufSize_Status = 256*1024;
//此缓冲区要足够大,最大为256K
DWORD cbBufSize_Config = 8*1024;
//此缓冲区要足够大,最大为8K
TCHAR cbBuf_Status[cbBufSize_Status];
TCHAR cbBuf_Config[cbBufSize_Config];
DWORD ServicesReturned = 0;
LPENUM_SERVICE_STATUS pServices = (LPENUM_SERVICE_STATUS)cbBuf_Status;
LPQUERY_SERVICE_CONFIG pServicesInfo = (LPQUERY_SERVICE_CONFIG)cbBuf_Config;
if (_GetServiceStatus(hScm, pServices, cbBufSize_Status, &ServicesReturned))
{
DWORD i = 0;
for (i=0; i<ServicesReturned; ++i)
{
if (_GetServiceConfig(hScm, pServices, i, pServicesInfo, cbBufSize_Config))
{
_PrintServices(pServices, i, pServicesInfo);
}
if (i%30==0 && i!=0)
{
printf("\n");
system("pause");
}
}
}
}
//启动服务
void _StartService(SC_HANDLE hScm, LPCTSTR lpServiceName)
{
SC_HANDLE hService = NULL;
if ((hService = OpenService(hScm, lpServiceName, SERVICE_ALL_ACCESS)) != NULL)
{
SERVICE_STATUS status;
if (QueryServiceStatus(hService, &status))
{
if (status.dwCurrentState == SERVICE_STOPPED)
{
StartService(hService, 0, NULL);
}
}
CloseServiceHandle(hService);
}
}
//停止服务
void _StopService(SC_HANDLE hScm, LPCTSTR lpServiceName)
{
SC_HANDLE hService = NULL;
if ((hService = OpenService(hScm, lpServiceName, SERVICE_ALL_ACCESS)) != NULL)
{
SERVICE_STATUS status;
if (QueryServiceStatus(hService, &status))
{
if (status.dwCurrentState != SERVICE_STOPPED)
{
ControlService(hService, SERVICE_CONTROL_STOP, &status);
}
}
CloseServiceHandle(hService);
}
}
//创建服务
BOOL _CreateService(SC_HANDLE hScm,
//用OpenSCManager打开的句柄
LPCTSTR lpServiceName,
//服务名称
LPCTSTR lpDisplayName,
//显示名称
DWORD dwStartType,
//启动方式:
//
SERVICE_AUTO_START
自动
//
SERVICE_DEMAND_START
手动
//
SERVICE_DISABLED
禁用
LPCTSTR lpBinaryPathName)
//程序路径
{
SC_HANDLE hService = NULL;
hService = CreateService(hScm, lpServiceName, lpDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
dwStartType, SERVICE_ERROR_NORMAL,
lpBinaryPathName, NULL, NULL, NULL, NULL, NULL);
if (hService != NULL)
{
CloseServiceHandle(hService);
return TRUE;
}
return FALSE;
}
//删除服务
BOOL _DeleteService(SC_HANDLE hScm, LPCTSTR lpServiceName)
{
_StopService(hScm, lpServiceName);
SC_HANDLE hService = NULL;
BOOL ret = FALSE;
if ((hService = OpenService(hScm, lpServiceName, SERVICE_ALL_ACCESS)) != NULL)
{
ret = DeleteService(hService);
CloseServiceHandle(hService);
}
return ret;
}
//设置启动类型 dwStartType
//
SERVICE_AUTO_START
自动
//
SERVICE_DEMAND_START 手动
//
SERVICE_DISABLED
禁止
BOOL _ChangeServiceConfig(SC_HANDLE hScm, LPCTSTR lpServiceName, DWORD dwStartType)
{
SC_HANDLE hService = NULL;
BOOL ret = FALSE;
if ((hService = OpenService(hScm, lpServiceName, SERVICE_ALL_ACCESS)) != NULL)
{
SC_LOCK sclLock = LockServiceDatabase(hScm);
ret = ChangeServiceConfig(hService,
SERVICE_NO_CHANGE,
dwStartType,
SERVICE_NO_CHANGE,
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
UnlockServiceDatabase(sclLock);
CloseServiceHandle(hService);
}
return ret;
}
void _test(void)
{
SC_HANDLE hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hScm == NULL)
{
printf("调用OpenSCManager()失败\n尝试“以管理员身份运行”\n");
return;
}
_EnumServices(hScm);
//_StartService(hScm, "我的服务");
//_StopService(hScm, "我的服务");
//_CreateService(hScm, "我的服务", "我的服务", SERVICE_DEMAND_START, "C:\\TEMP\\_Test.exe");
//_DeleteService(hScm, "我的服务");
//_ChangeServiceConfig(hScm, "我的服务", SERVICE_DISABLED);
CloseServiceHandle(hScm);
}
int main(void)
{
_test();
return 0;
}