ログオン日時を取得する


ユーザーのログオン日時は LsaGetLogonSessionData 関数で得られる SECURITY_LOGON_SESSION_DATA 構造体のLogonTimeメンバで取得できます。
LogonTimeはLARGE_INTEGER型ですが、FILETIME型として変換すれば正しい日時を取得できます。


Sample code
#include <windows.h>
#include <tchar.h>
#include <ntsecapi.h>
#include <stdio.h>

#pragma comment(lib, "secur32.lib")

#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS  ((NTSTATUS)0x00000000L)
#endif

int _tmain()
{
        ULONG logonSessionCount;
        PLUID logonSessionList;
        if(LsaEnumerateLogonSessions(&logonSessionCount, &logonSessionList) != STATUS_SUCCESS) {
            return 0;
        }
        
        PSECURITY_LOGON_SESSION_DATA pLogonSessionData;
        for(ULONG i = 0; i < logonSessionCount; i++) {
            if(LsaGetLogonSessionData(&logonSessionList[i], &pLogonSessionData) == STATUS_SUCCESS) {
                //ユーザー名の表示
                pLogonSessionData->UserName.Buffer[pLogonSessionData->UserName.Length / sizeof(WCHAR)] = L'\0';
                wprintf(pLogonSessionData->UserName.Buffer);
                //ログオン日時の表示
                FILETIME logonTime = *reinterpret_cast<FILETIME*>(&pLogonSessionData->LogonTime);
                FILETIME localFileTime;
                FileTimeToLocalFileTime(&logonTime, &localFileTime);
                SYSTEMTIME st;
                FileTimeToSystemTime(&localFileTime, &st);
                _tprintf(TEXT(":%d/%d/%d %d:%d:%d\r\n"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
                
                LsaFreeReturnBuffer(pLogonSessionData);
            }
        }
        LsaFreeReturnBuffer(logonSessionList);
        return 0;
}

一覧メニューへ