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,28 +65,10 @@ bool checkIsSvcHostService(const RegKey &svcReg)
return true;
}
QVector<ServiceInfo> getServiceInfoList(SC_HANDLE mngr, DWORD serviceType = SERVICE_WIN32,
DWORD state = SERVICE_STATE_ALL, bool displayName = true,
int *runningServicesCount = nullptr)
void fillServiceInfoList(QVector<ServiceInfo> &infoList, const RegKey &servicesReg,
const ENUM_SERVICE_STATUS_PROCESSW *service, DWORD serviceCount, bool displayName,
int &runningCount)
{
QVector<ServiceInfo> infoList;
const RegKey servicesReg(RegKey::HKLM, servicesSubKey);
constexpr DWORD bufferMaxSize = 32 * 1024;
ENUM_SERVICE_STATUS_PROCESSW buffer[bufferMaxSize / sizeof(ENUM_SERVICE_STATUS_PROCESSW)];
DWORD bytesRemaining = 0;
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) {
@ -116,15 +98,40 @@ QVector<ServiceInfo> getServiceInfoList(SC_HANDLE mngr, DWORD serviceType = SERV
infoList.append(info);
}
}
QVector<ServiceInfo> getServiceInfoList(SC_HANDLE mngr, DWORD serviceType = SERVICE_WIN32,
DWORD state = SERVICE_STATE_ALL, bool displayName = true,
int *runningServicesCount = nullptr)
{
QVector<ServiceInfo> infoList;
const RegKey servicesReg(RegKey::HKLM, servicesSubKey);
constexpr DWORD bufferMaxSize = 32 * 1024;
ENUM_SERVICE_STATUS_PROCESSW buffer[bufferMaxSize / sizeof(ENUM_SERVICE_STATUS_PROCESSW)];
DWORD bytesRemaining = 0;
DWORD serviceCount = 0;
DWORD resumePoint = 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];
int runningCount = 0;
fillServiceInfoList(
infoList, servicesReg, service, serviceCount, displayName, runningCount);
if (runningServicesCount) {
*runningServicesCount += runningCount;
}
if (bytesRemaining == 0)
break;
}
if (runningServicesCount) {
*runningServicesCount = runningCount;
}
return infoList;
}