Help needed to retrieve the LogonType using Win32LogonSession class in C++

Rohan Pande 445 Reputation points
2024-07-12T06:19:11.6866667+00:00

Hello,

I am trying to use Win32LogonSession to retrieve the LogonType in my C++ code. However, I'm encountering several errors and I am unsure if I'm writing the correct code. Can anyone provide assistance?

Thank you.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,649 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,755 questions
{count} votes

Accepted answer
  1. RLWA32 45,691 Reputation points
    2024-07-12T07:02:31.25+00:00

    You can get the logon type using the Windows API without WMI --

    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    #include <NTSecAPI.h>
    #pragma comment(lib, "secur32")
    
    #include <stdio.h>
    #include <memory>
    
    PCWSTR GetLogonTypeAsString(SECURITY_LOGON_TYPE type)
    {
        switch (type)
        {
        case UndefinedLogonType: return L"UndefinedLogonType";
        case Interactive: return L"Interactive";
        case Network: return L"Network";
        case Batch: return L"Batch";
        case Service: return L"Service";
        case Proxy: return L"Proxy";
        case Unlock: return L"Unlock";
        case NetworkCleartext: return L"NetworkCleartext";
        case NewCredentials: return L"NewCredentials";
        case RemoteInteractive: return L"RemoteInteractive";
        case CachedInteractive: return L"CachedInteractive";
        case CachedRemoteInteractive: return L"CachedRemoteInteractive";
        case CachedUnlock: return L"CachedUnlock";
        default: return L"Error";
        }
    }
    
    int main()
    {
        DWORD dwLength{};
        GetTokenInformation(GetCurrentProcessToken(), TokenStatistics, nullptr, 0, &dwLength);
        std::unique_ptr<BYTE[]> buffer = std::make_unique<BYTE[]>(dwLength);
        if (GetTokenInformation(GetCurrentProcessToken(), TokenStatistics, buffer.get(), dwLength, &dwLength))
        {
            PTOKEN_STATISTICS pts = reinterpret_cast<PTOKEN_STATISTICS>(buffer.get());
            PSECURITY_LOGON_SESSION_DATA pLogonSessionData{};
            auto status = LsaGetLogonSessionData(&pts->AuthenticationId, &pLogonSessionData);
            if (status == 0)
            {
                wprintf_s(L"Logon type was %s\n", GetLogonTypeAsString((SECURITY_LOGON_TYPE) pLogonSessionData->LogonType));
                LsaFreeReturnBuffer(pLogonSessionData);
            }
        }
        return 0;
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.