Windows服务打造(二)

1. 欲访问windows服务,得调用Windows API OpenSCManager(...)访问服务数据库,查看MSDN中DOpenSCManager函数如下:

SC_HANDLE WINAPI OpenSCManager(
  __in          LPCTSTR lpMachineName,
  __in          LPCTSTR lpDatabaseName,
  __in          DWORD dwDesiredAccess
);

Parameters

lpMachineName

The name of the target computer. If the pointer is NULL or points to an empty string, the function connects to the service control manager on the local computer.

lpDatabaseName

The name of the service control manager database. This parameter should be set to SERVICES_ACTIVE_DATABASE.If it is NULL, the SERVICES_ACTIVE_DATABASE database is opened by default.

dwDesiredAccess

The access to the service control manager. For a list of access rights, see Service Security and Access Rights.

Before granting the requested access rights, the system checks the access token of the calling process against the discretionary access-control list of the security descriptor associated with the service control manager.

The SC_MANAGER_CONNECT access right is implicitly specified by calling this function.

我们这里访问本机,故lpMachineName 和 lpDatabaseName 都传入NULL,dwDesiredAccess这个我们传入SC_MANAGER_ALL_ACCESS所有权限进行访问。

Access rightDescription
SC_MANAGER_ALL_ACCESS (0xF003F)Includes STANDARD_RIGHTS_REQUIRED, in addition to all access rights in this table.
SC_MANAGER_CREATE_SERVICE (0x0002)Required to call the CreateService function to create a service object and add it to the database.
SC_MANAGER_CONNECT (0x0001)Required to connect to the service control manager.
SC_MANAGER_ENUMERATE_SERVICE (0x0004)Required to call the EnumServicesStatusEx function to list the services that are in the database.
SC_MANAGER_LOCK (0x0008)Required to call the LockServiceDatabase function to acquire a lock on the database.
SC_MANAGER_MODIFY_BOOT_CONFIG (0x0020)Required to call the NotifyBootConfigStatus function.
SC_MANAGER_QUERY_LOCK_STATUS (0x0010)Required to call the QueryServiceLockStatus function to retrieve the lock status information for the database.
 2.再看EnumServicesStatus函数。

BOOL WINAPI EnumServicesStatus(
  __in          SC_HANDLE hSCManager,
  __in          DWORD dwServiceType,
  __in          DWORD dwServiceState,
  __out         LPENUM_SERVICE_STATUS lpServices,
  __in          DWORD cbBufSize,
  __out         LPDWORD pcbBytesNeeded,
  __out         LPDWORD lpServicesReturned,
  __in_out      LPDWORD lpResumeHandle
);
 EnumServicesStatus函数根据

OpenSCManager返回的SC_HANDLE句柄枚举服务数据库,out出了LPENUM_SERVICE_STATUS指针对象。

这里创建出CServConfig封装服务的类,类中添加CServItem节点,储存每一个服务节点的信息;代码如下:

class CServItem {
public:
CString m_strServName;
CString m_strServDispName;
DWORD m_dwServState;//服务当前状态
CServItem *m_pNext;
CServItem() {
m_dwServState = 0;
m_pNext = NULL;
}
};

定义一个函数CServItem *EnumServList();枚举服务。并添加GetStateString函数获取当前服务状态(EnumServicesStatus函数传出状态)

 3.服务访问完毕,调用CloseServiceHandle(...)关闭服务数据库;CloseServiceHandle(...)MSDN如下

BOOL WINAPI CloseServiceHandle(
  __in          SC_HANDLE hSCObject
);

 最终效果如下:



版权声明:本文为qq_27011361原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。