【原创】驱动枚举之EnumServicesStatusEx

时间:2021-03-31 00:28:35
  1. BOOL WINAPI EnumServicesStatusEx(
  2. _In_ SC_HANDLE hSCManager,
  3. _In_ SC_ENUM_TYPE InfoLevel,
  4. _In_ DWORD dwServiceType,
  5. _In_ DWORD dwServiceState,
  6. _Out_opt_ LPBYTE lpServices,
  7. _In_ DWORD cbBufSize,
  8. _Out_ LPDWORD pcbBytesNeeded,
  9. _Out_ LPDWORD lpServicesReturned,
  10. _Inout_opt_ LPDWORD lpResumeHandle,
  11. _In_opt_ LPCTSTR pszGroupName
  12. );
函数作用:枚举指定SCM数据库中的驱动
参数:
1. hSCManager:SCM数据库句柄,由OpenSCManager 函数获得, 获同时需要具有SC_MANAGER_ENUMERATE_SERVICE权限
2. InfoLevel:返回哪种服务属性。目前只定义了一种SC_ENUM_PROCESS_INFO,用来获取名字和服务状态信息,这种情况下,lpServices是一个缓冲区指针,用来接收一个ENUM_SERVICE_STATUS_PROCESS结构体数组,缓冲区大小一定要足够大,以容纳结构体以及它们成员所指向的字符串。
3. dwServiceType:要枚举的服务类型
Value Meaning
SERVICE_DRIVER
0x0000000B

Services of type SERVICE_KERNEL_DRIVER and SERVICE_FILE_SYSTEM_DRIVER.

SERVICE_FILE_SYSTEM_DRIVER
0x00000002

File system driver services.

SERVICE_KERNEL_DRIVER
0x00000001

Driver services.

SERVICE_WIN32
0x00000030

Services of type SERVICE_WIN32_OWN_PROCESS and SERVICE_WIN32_SHARE_PROCESS.

SERVICE_WIN32_OWN_PROCESS
0x00000010

Services that run in their own processes.

SERVICE_WIN32_SHARE_PROCESS
0x00000020

Services that share a process with one or more other services. For more information, see Service Programs.

4. dwServiceState:要枚举的服务状态,
Value Meaning
SERVICE_ACTIVE
0x00000001

Enumerates services that are in the following states: SERVICE_START_PENDING, SERVICE_STOP_PENDING, SERVICE_RUNNING, SERVICE_CONTINUE_PENDING, SERVICE_PAUSE_PENDING, and SERVICE_PAUSED.

SERVICE_INACTIVE
0x00000002

Enumerates services that are in the SERVICE_STOPPED state.

SERVICE_STATE_ALL
0x00000003

Combines the SERVICE_ACTIVE and SERVICE_INACTIVE states.

5. lpServices[out, optional] :缓冲区指针,用于接收状态信息。数据的格式取决于第二个形参InfoLevel的值。

数组的最大大小是256K字节。为了获取实际需要的大小,我们可以将此形参赋值为NULL,同时将下一个形参cbBufSize赋值为0,pcbBytesNeeded返回的就是实际的数组大小,同时函数调用失败并返回ERROR_MORE_DATA。XP和2003下,数组的最大大小是64K 字节
6. cbBufSize:缓冲区大小
7. pcbBytesNeeded[out]:指针变量,如果缓冲区太小,该指针指向的变量存储返回剩余服务信息所需要的字节大小。
8. lpServicesReturned [out]:一个指向实际返回的服务的个数
9. lpResumeHandle [in, out, optional]:一个指向变量的指针。在输入上,指定枚举的开始点;在第一次调用EnumServicesStatusEx函数时,指针指向的变量必须设置为0。在输出上,如果函数成功,那么指向的变量的值为0。但是,如果函数返回0,且GetLastError函数得到了ERROR_MORE_DATA错误码,则这个值表示下一个服务的开始点,可以用作下次枚举lpResumeHandle的值。
10. pszGroupName [in, optional] :服务组名字。如果该形参是一个字符串,那么只枚举指定服务组的服务。如果形参为空字符串"",那么只枚举不属于任何服务组的服务。如果形参为NULL,那么枚举所有服务。
 
返回值:
成功返回非0;失败返回0。失败时可以用GetLastError获取错误码
Return code Description
ERROR_ACCESS_DENIED

The handle does not have the SC_MANAGER_ENUMERATE_SERVICE access right.

ERROR_MORE_DATA

The buffer is too small. Not all data in the active database could be returned. The pcbBytesNeeded parameter contains the number of bytes required to receive the remaining entries.

ERROR_INVALID_PARAMETER

An illegal parameter value was used.

ERROR_INVALID_HANDLE

The handle is invalid.

ERROR_INVALID_LEVEL

The InfoLevel parameter contains an unsupported value.

ERROR_SHUTDOWN_IN_PROGRESS

The system is shutting down; this function cannot be called.

说明:如果没有SERVICE_QUERY_STATUS权限,那么服务将会被静默忽略,不会返回任何信息。

本文链接:http://www.cnblogs.com/cposture/p/4720685.html