枚举Windows系统服务,通过进程ID取服务名



//枚举Windows系统服务

//使用到的函数以及MSDN的说明例如以下:
//1、OpenSCManager说明
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms684323(v=vs.85).aspx

//2、EnumServicesStatusEx说明
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms682640(v=vs.85).aspx

//3、CloseServiceHandle说明
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms682028(v=vs.85).aspx

// 測试代码:

#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h>
int main(int argc, char *argv[])
{
	LONG lRet = 0;
	BOOL bRet = FALSE;
	SC_HANDLE hSCM = NULL;              // 服务数据库句柄
	char *pBuf = NULL;                  // 缓冲区指针
	DWORD dwBufSize = 0;                // 传入的缓冲长度
	DWORD dwBufNeed = 0;                // 须要的缓冲长度
	DWORD dwNumberOfService = 0;        // 返回的服务个数
	ENUM_SERVICE_STATUS_PROCESS *pServiceInfo = NULL;   // 服务信息

	// 建立了一个到服务控制管理器的连接,并打开指定的数据库
	hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_CONNECT);
	if(NULL == hSCM)
	{
		printf("OpenSCManager error.
");
		return -1;
	}

	// 获取须要的缓冲区大小
	EnumServicesStatusEx(hSCM, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, 
		NULL, dwBufSize, &dwBufNeed, &dwNumberOfService, NULL, NULL);

	// 多设置存放1个服务信息的长度
	dwBufSize = dwBufNeed + sizeof(ENUM_SERVICE_STATUS_PROCESS);
	pBuf = (char *)malloc(dwBufSize);
	if(NULL == pBuf)
	{
		printf("malloc error.
");
		return -1;
	}
	memset(pBuf, 0, dwBufSize);

	// 获取服务信息
	bRet = EnumServicesStatusEx(hSCM, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, 
		(LPBYTE)pBuf, dwBufSize, &dwBufNeed, &dwNumberOfService, NULL, NULL);
	if(bRet == FALSE)
	{
		printf("EnumServicesStatusEx error.
");
		::CloseServiceHandle(hSCM);
		free(pBuf);
		return -1;
	}
	// 关闭打开的服务句柄
	bRet = ::CloseServiceHandle(hSCM);
	if(bRet == FALSE)
	{
		printf("CloseServiceHandle error.
");
	}
	printf("Service Num:%d
", dwNumberOfService);

	pServiceInfo = (LPENUM_SERVICE_STATUS_PROCESS)pBuf;
	// 打印取得的服务信息
	for(unsigned int i = 0; i < dwNumberOfService; i++)
	{
		printf("----------%d----------
", i);
		printf("DisplayName 		 : %s 
", pServiceInfo[i].lpDisplayName);
		printf("ServiceName 		 : %s 
", pServiceInfo[i].lpServiceName);
		printf("ServiceType 		 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwServiceType);
		printf("CurrentState 		 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwCurrentState);
		printf("ControlsAccepted 	 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwControlsAccepted);
		printf("Win32ExitCode 		 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwWin32ExitCode);
		printf("ServiceSpecificExitCode  : %d 
", pServiceInfo[i].ServiceStatusProcess.dwServiceSpecificExitCode);
		printf("CheckPoint 		 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwCheckPoint);
		printf("WaitHint 		 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwWaitHint);
		printf("Process Id 		 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwProcessId);
		printf("ServiceFlags 		 : %d 
", pServiceInfo[i].ServiceStatusProcess.dwServiceFlags);
	}
	free(pBuf);
	system("PAUSE");
	return 0;
}


// 既然可以获取到全部的服务信息。
// 那么依据进程ID查询该进程是否为服务。取得服务名等一系列的操作就能够按自己的需求来完毕了
// 获取进程ID GetCurrentProcessId()



原文地址:https://www.cnblogs.com/wgwyanfs/p/7241344.html