UI: ServiceInfoManager: Simplify getServiceInfoList()

This commit is contained in:
Nodir Temirkhodjaev 2023-06-19 14:28:50 +03:00
parent 322a4e44e3
commit b562e4d848
2 changed files with 41 additions and 34 deletions

View File

@ -1155,7 +1155,7 @@ void ConfManager::updateServices()
{ {
auto serviceInfoManager = IoC<ServiceInfoManager>(); auto serviceInfoManager = IoC<ServiceInfoManager>();
int runningServicesCount; int runningServicesCount = 0;
const QVector<ServiceInfo> services = const QVector<ServiceInfo> services =
serviceInfoManager->loadServiceInfoList(ServiceInfo::TypeWin32, ServiceInfo::StateAll, serviceInfoManager->loadServiceInfoList(ServiceInfo::TypeWin32, ServiceInfo::StateAll,
/*displayName=*/false, &runningServicesCount); /*displayName=*/false, &runningServicesCount);

View File

@ -65,6 +65,41 @@ bool checkIsSvcHostService(const RegKey &svcReg)
return true; return true;
} }
void fillServiceInfoList(QVector<ServiceInfo> &infoList, const RegKey &servicesReg,
const ENUM_SERVICE_STATUS_PROCESSW *service, DWORD serviceCount, bool displayName,
int &runningCount)
{
for (int infoIndex = infoList.size(); serviceCount > 0;
--serviceCount, ++service, ++infoIndex) {
auto serviceName = QString::fromUtf16((const char16_t *) service->lpServiceName);
serviceName = resolveSvcHostServiceName(servicesReg, serviceName);
const RegKey svcReg(servicesReg, serviceName);
if (!checkIsSvcHostService(svcReg))
continue;
const quint32 trackFlags = svcReg.value(serviceTrackFlagsKey).toUInt();
ServiceInfo info;
info.isRunning = (service->ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING);
info.trackFlags = trackFlags;
info.processId = service->ServiceStatusProcess.dwProcessId;
info.serviceName = serviceName;
if (displayName) {
info.displayName = QString::fromUtf16((const char16_t *) service->lpDisplayName);
}
if (info.isRunning) {
++runningCount;
}
infoList.append(info);
}
}
QVector<ServiceInfo> getServiceInfoList(SC_HANDLE mngr, DWORD serviceType = SERVICE_WIN32, QVector<ServiceInfo> getServiceInfoList(SC_HANDLE mngr, DWORD serviceType = SERVICE_WIN32,
DWORD state = SERVICE_STATE_ALL, bool displayName = true, DWORD state = SERVICE_STATE_ALL, bool displayName = true,
int *runningServicesCount = nullptr) int *runningServicesCount = nullptr)
@ -79,52 +114,24 @@ QVector<ServiceInfo> getServiceInfoList(SC_HANDLE mngr, DWORD serviceType = SERV
DWORD serviceCount = 0; DWORD serviceCount = 0;
DWORD resumePoint = 0; DWORD resumePoint = 0;
int runningCount = 0;
while (EnumServicesStatusExW(mngr, SC_ENUM_PROCESS_INFO, serviceType, state, (LPBYTE) buffer, while (EnumServicesStatusExW(mngr, SC_ENUM_PROCESS_INFO, serviceType, state, (LPBYTE) buffer,
sizeof(buffer), &bytesRemaining, &serviceCount, &resumePoint, nullptr) sizeof(buffer), &bytesRemaining, &serviceCount, &resumePoint, nullptr)
|| GetLastError() == ERROR_MORE_DATA) { || GetLastError() == ERROR_MORE_DATA) {
const ENUM_SERVICE_STATUS_PROCESSW *service = &buffer[0]; const ENUM_SERVICE_STATUS_PROCESSW *service = &buffer[0];
for (int infoIndex = infoList.size(); serviceCount > 0; int runningCount = 0;
--serviceCount, ++service, ++infoIndex) { fillServiceInfoList(
infoList, servicesReg, service, serviceCount, displayName, runningCount);
auto serviceName = QString::fromUtf16((const char16_t *) service->lpServiceName); if (runningServicesCount) {
serviceName = resolveSvcHostServiceName(servicesReg, serviceName); *runningServicesCount += runningCount;
const RegKey svcReg(servicesReg, serviceName);
if (!checkIsSvcHostService(svcReg))
continue;
const quint32 trackFlags = svcReg.value(serviceTrackFlagsKey).toUInt();
ServiceInfo info;
info.isRunning = (service->ServiceStatusProcess.dwCurrentState == SERVICE_RUNNING);
info.trackFlags = trackFlags;
info.processId = service->ServiceStatusProcess.dwProcessId;
info.serviceName = serviceName;
if (displayName) {
info.displayName = QString::fromUtf16((const char16_t *) service->lpDisplayName);
}
if (info.isRunning) {
++runningCount;
}
infoList.append(info);
} }
if (bytesRemaining == 0) if (bytesRemaining == 0)
break; break;
} }
if (runningServicesCount) {
*runningServicesCount = runningCount;
}
return infoList; return infoList;
} }