Click here to Skip to main content
15,920,596 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Advice on writing a GUI application that can run on very old Windows operating systems without Service Packs and Frameworks Pin
Richard MacCutchan14-Jun-21 8:51
mveRichard MacCutchan14-Jun-21 8:51 
AnswerRe: Advice on writing a GUI application that can run on very old Windows operating systems without Service Packs and Frameworks Pin
Eddy Vluggen14-Jun-21 9:18
professionalEddy Vluggen14-Jun-21 9:18 
AnswerRe: Advice on writing a GUI application that can run on very old Windows operating systems without Service Packs and Frameworks Pin
Maximilien15-Jun-21 5:22
Maximilien15-Jun-21 5:22 
QuestionWindows 10 - find last logon time in C++ ?? Pin
Derell Licht5-Jun-21 16:28
professionalDerell Licht5-Jun-21 16:28 
QuestionRe: Windows 10 - find last logon time in C++ ?? Pin
David Crow5-Jun-21 16:53
David Crow5-Jun-21 16:53 
AnswerRe: Windows 10 - find last logon time in C++ ?? Pin
Derell Licht5-Jun-21 17:06
professionalDerell Licht5-Jun-21 17:06 
AnswerRe: Windows 10 - find last logon time in C++ ?? Pin
Derell Licht5-Jun-21 17:11
professionalDerell Licht5-Jun-21 17:11 
AnswerRe: Windows 10 - find last logon time in C++ ?? Pin
Randor 6-Jun-21 5:21
professional Randor 6-Jun-21 5:21 
Hi,

The NetUserGetInfo function is an old derelict function. (pun intentional) Maybe you could use the Security and Identity[^] framework to get session information.

Disclaimer: This is a code sample, it doesn't handle dynamic daylight saving time. Also, the pointer arithmetic could be refactored.
C++
#pragma comment(lib, "Secur32.lib")

#include <iostream>
#include <windows.h>
#include <ntsecapi.h>
INT main()
{
    DWORD lc = 0;
    DWORD status = 0;
    PLUID list = nullptr;

    LsaEnumerateLogonSessions(&lc, &list);
    for (DWORD i = 0; i < lc; i++)
    {
        PSECURITY_LOGON_SESSION_DATA pData;

        status = LsaGetLogonSessionData((PLUID)((INT_PTR)list + sizeof(LUID) * i), &pData);
        if (0 == status)
        {
            if (Interactive == pData->LogonType)
            {
                FILETIME ft;
                SYSTEMTIME st_utc, st_local;
                TIME_ZONE_INFORMATION tzi;

                ft.dwHighDateTime = pData->LogonTime.HighPart;
                ft.dwLowDateTime = pData->LogonTime.LowPart;

                GetTimeZoneInformation(&tzi);
                FileTimeToSystemTime(&ft, &st_utc);
                SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_local);

                wprintf(L"UserName: %s\n", pData->UserName.Buffer);
                wprintf(L"Last logon %s: %d/%d/%d-%d:%d:%d:%d\n", tzi.StandardName, st_local.wMonth, st_local.wDay, st_local.wYear, st_local.wHour, st_local.wMinute, st_local.wSecond, st_local.wMilliseconds);
            }

            LsaFreeReturnBuffer(pData);
        }
    }
}


I noticed that the C# guys have much better time zone tools[^]. For some reason nobody invested any time into doing this for the public native API.

So I whipped up something real quick for converting to an arbitrary time zone:

C++
//The registry 'Olson TZ structure' is partially documented here https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/ns-timezoneapi-time_zone_information  --David Delaune
typedef struct _REG_TZI_FORMAT
{
    LONG Bias;
    LONG StandardBias;
    LONG DaylightBias;
    SYSTEMTIME StandardDate;
    SYSTEMTIME DaylightDate;
} REG_TZI_FORMAT;

BOOL ConvertToTzSpecificTimeZone(const wchar_t time_zone[], TIME_ZONE_INFORMATION * tzi, const SYSTEMTIME * lpUniversalTime, const LPSYSTEMTIME lpLocalTime)
{
    HKEY k;
    DWORD size = 0;
    REG_TZI_FORMAT tzdb;
    BOOL bRet = FALSE;

    SecureZeroMemory(tzi, sizeof(tzi));
    
    LSTATUS status = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", &k);

    if (ERROR_SUCCESS == status)
    {
        size = sizeof(REG_TZI_FORMAT);
        status = RegGetValueW(k, time_zone, L"TZI", RRF_RT_REG_BINARY, 0, &tzdb, &size);
        if (ERROR_SUCCESS == status)
        {
            tzi->Bias = tzdb.Bias;
            tzi->DaylightBias = tzdb.DaylightBias;
            tzi->DaylightDate = tzdb.DaylightDate;
            tzi->StandardBias = tzdb.StandardBias;
            tzi->StandardDate = tzdb.StandardDate;

            wcscpy_s(tzi->StandardName, time_zone);

            bRet = SystemTimeToTzSpecificLocalTime(tzi, lpUniversalTime, lpLocalTime);
        }
        RegCloseKey(k);
    }
    return bRet;
}


You would use it something like this:

C++
if (ConvertToTzSpecificTimeZone(L"Tokyo Standard Time", &tzi, &st_utc, &st_local))
{
    wprintf(L"Last logon %s: %d/%d/%d-%d:%d:%d:%d\n", tzi.StandardName, st_local.wMonth, st_local.wDay, st_local.wYear, st_local.wHour, st_local.wMinute, st_local.wSecond, st_local.wMilliseconds);
}


Best Wishes,
-David Delaune

edit: added call to RegCloseKey

modified 6-Jun-21 14:30pm.

GeneralRe: Windows 10 - find last logon time in C++ ?? Pin
Derell Licht6-Jun-21 6:27
professionalDerell Licht6-Jun-21 6:27 
GeneralRe: Windows 10 - find last logon time in C++ ?? Pin
Randor 6-Jun-21 7:26
professional Randor 6-Jun-21 7:26 
GeneralRe: Windows 10 - find last logon time in C++ ?? Pin
Derell Licht6-Jun-21 7:44
professionalDerell Licht6-Jun-21 7:44 
GeneralRe: Windows 10 - find last logon time in C++ ?? Pin
Randor 6-Jun-21 8:11
professional Randor 6-Jun-21 8:11 
AnswerRe: Windows 10 - find last logon time in C++ ?? Pin
Derell Licht6-Jun-21 15:20
professionalDerell Licht6-Jun-21 15:20 
GeneralRe: Windows 10 - find last logon time in C++ ?? Pin
Richard MacCutchan6-Jun-21 21:16
mveRichard MacCutchan6-Jun-21 21:16 
GeneralRe: Windows 10 - find last logon time in C++ ?? Pin
Derell Licht7-Jun-21 3:14
professionalDerell Licht7-Jun-21 3:14 
GeneralRe: Windows 10 - find last logon time in C++ ?? Pin
Maximilien7-Jun-21 9:15
Maximilien7-Jun-21 9:15 
Questionurgent question Pin
MAHMOUD ALI Jun20215-Jun-21 1:03
MAHMOUD ALI Jun20215-Jun-21 1:03 
AnswerRe: urgent question Pin
Victor Nijegorodov5-Jun-21 1:24
Victor Nijegorodov5-Jun-21 1:24 
GeneralRe: urgent question Pin
MAHMOUD ALI Jun20215-Jun-21 1:37
MAHMOUD ALI Jun20215-Jun-21 1:37 
GeneralRe: urgent question Pin
Victor Nijegorodov5-Jun-21 1:41
Victor Nijegorodov5-Jun-21 1:41 
AnswerRe: urgent question Pin
Richard MacCutchan5-Jun-21 2:20
mveRichard MacCutchan5-Jun-21 2:20 
AnswerRe: urgent question Pin
David Crow5-Jun-21 4:03
David Crow5-Jun-21 4:03 
AnswerRe: urgent question Pin
Dave Kreskowiak5-Jun-21 4:22
mveDave Kreskowiak5-Jun-21 4:22 
AnswerRe: urgent question Pin
enhzflep10-Jun-21 21:13
enhzflep10-Jun-21 21:13 
AnswerRe: urgent question Pin
SeeSharp211-Jun-21 1:23
SeeSharp211-Jun-21 1:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.