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>();
int runningServicesCount;
int runningServicesCount = 0;
const QVector<ServiceInfo> services =
serviceInfoManager->loadServiceInfoList(ServiceInfo::TypeWin32, ServiceInfo::StateAll,
/*displayName=*/false, &runningServicesCount);

View File

@ -65,6 +65,41 @@ bool checkIsSvcHostService(const RegKey &svcReg)
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,
DWORD state = SERVICE_STATE_ALL, bool displayName = true,
int *runningServicesCount = nullptr)
@ -79,52 +114,24 @@ QVector<ServiceInfo> getServiceInfoList(SC_HANDLE mngr, DWORD serviceType = SERV
DWORD serviceCount = 0;
DWORD resumePoint = 0;
int runningCount = 0;
while (EnumServicesStatusExW(mngr, SC_ENUM_PROCESS_INFO, serviceType, state, (LPBYTE) buffer,
sizeof(buffer), &bytesRemaining, &serviceCount, &resumePoint, nullptr)
|| GetLastError() == ERROR_MORE_DATA) {
const ENUM_SERVICE_STATUS_PROCESSW *service = &buffer[0];
for (int infoIndex = infoList.size(); serviceCount > 0;
--serviceCount, ++service, ++infoIndex) {
int runningCount = 0;
fillServiceInfoList(
infoList, servicesReg, service, serviceCount, displayName, runningCount);
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);
if (runningServicesCount) {
*runningServicesCount += runningCount;
}
if (bytesRemaining == 0)
break;
}
if (runningServicesCount) {
*runningServicesCount = runningCount;
}
return infoList;
}