Users: improve performance (Windows)

This commit is contained in:
李通洲
2022-11-23 19:11:10 +08:00
parent b28da1057c
commit 0f08233581
3 changed files with 57 additions and 36 deletions
+2 -1
View File
@@ -450,7 +450,7 @@ elseif(WIN32)
src/detection/terminalfont/terminalfont_windows.c
src/detection/terminalshell/terminalshell_windows.cpp
src/detection/uptime/uptime_windows.c
src/detection/users/users_windows.cpp
src/detection/users/users_windows.c
src/detection/wmtheme/wmtheme_windows.c
src/util/windows/getline.c
src/util/windows/pwd.c
@@ -555,6 +555,7 @@ elseif(WIN32)
PRIVATE "version"
PRIVATE "setupapi"
PRIVATE "dxgi"
PRIVATE "wtsapi32"
)
if(USE_WIN_NTAPI)
target_compile_definitions(libfastfetch PRIVATE FF_USE_WIN_NTAPI)
+55
View File
@@ -0,0 +1,55 @@
#include "users.h"
#include "util/windows/unicode.h"
#include <wtsapi32.h>
//at the time of writing, <wtsapi32.h> of MinGW doesn't have the definition of WTSEnumerateSessionsExW
typedef struct _WTS_SESSION_INFO_1W {
DWORD ExecEnvId;
WTS_CONNECTSTATE_CLASS State;
DWORD SessionId;
LPWSTR pSessionName;
LPWSTR pHostName;
LPWSTR pUserName;
LPWSTR pDomainName;
LPWSTR pFarmName;
} WTS_SESSION_INFO_1W, * PWTS_SESSION_INFO_1W;
BOOL
WINAPI
WTSEnumerateSessionsExW(
HANDLE hServer,
DWORD* pLevel,
DWORD Filter,
PWTS_SESSION_INFO_1W* ppSessionInfo,
DWORD* pCount);
void ffDetectUsers(FFlist* users, FFstrbuf* error)
{
WTS_SESSION_INFO_1W* sessionInfo;
DWORD sessionCount;
DWORD level = 1;
if(!WTSEnumerateSessionsExW(WTS_CURRENT_SERVER_HANDLE, &level, 0, &sessionInfo, &sessionCount))
{
ffStrbufAppendS(error, "WTSEnumerateSessionsW(WTS_CURRENT_SERVER_HANDLE) failed");
return;
}
for (DWORD i = 0; i < sessionCount; i++)
{
WTS_SESSION_INFO_1W* session = &sessionInfo[i];
if(session->State != WTSActive)
continue;
FF_STRBUF_AUTO_DESTROY domainName = ffStrbufFromWchar(session->pDomainName);
FF_STRBUF_AUTO_DESTROY userName = ffStrbufFromWchar(session->pUserName);
ffStrbufInitF((FFstrbuf*)ffListAdd(users), "%s\\%s", domainName.chars, userName.chars);
}
WTSFreeMemory(sessionInfo);
if(users->length == 0)
ffStrbufAppendS(error, "Unable to detect users");
}
-35
View File
@@ -1,35 +0,0 @@
extern "C" {
#include "users.h"
}
#include "util/windows/wmi.hpp"
void ffDetectUsers(FFlist* users, FFstrbuf* error)
{
FFWmiQuery query(L"SELECT Antecedent FROM Win32_LoggedOnUser", error);
if(!query)
return;
next:
while(FFWmiRecord record = query.next())
{
FFstrbuf antecedent;
ffStrbufInit(&antecedent);
record.getString(L"Antecedent", &antecedent); // \\.\root\cimv2:Win32_Account.Domain="DOMAIN",Name="NAME"
ffStrbufTrimRight(&antecedent, '"'); // \\.\root\cimv2:Win32_Account.Domain="DOMAIN",Name="NAME
ffStrbufSubstrAfterFirstC(&antecedent, '"'); // DOMAIN",Name="NAME
uint32_t index = ffStrbufFirstIndexC(&antecedent, '"');
ffStrbufRemoveSubstr(&antecedent, index, ffStrbufLastIndexC(&antecedent, '"')); // DOMAIN"NAME
antecedent.chars[index] = '\\';
for(uint32_t i = 0; i < users->length; ++i)
{
if(ffStrbufComp((FFstrbuf*)ffListGet(users, i), &antecedent) == 0)
goto next;
}
*(FFstrbuf*)ffListAdd(users) = antecedent;
}
if(users->length == 0)
ffStrbufAppendS(error, "Unable to detect users");
}