Click here to Skip to main content
15,899,679 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How do I access private variable of an object ? Pin
CPallini17-Jun-21 7:43
mveCPallini17-Jun-21 7:43 
AnswerRe: How do I access private variable of an object ? Pin
Magnus Forslund15-Jul-21 2:19
Magnus Forslund15-Jul-21 2:19 
QuestionMessage Closed Pin
15-Jun-21 10:35
Member 1496877115-Jun-21 10:35 
AnswerRe: Pass a function with parameter Pin
Greg Utas15-Jun-21 11:38
professionalGreg Utas15-Jun-21 11:38 
GeneralMessage Closed Pin
15-Jun-21 13:30
Member 1496877115-Jun-21 13:30 
GeneralRe: Pass a function with parameter Pin
Greg Utas15-Jun-21 13:39
professionalGreg Utas15-Jun-21 13:39 
GeneralMessage Closed Pin
15-Jun-21 15:23
Member 1496877115-Jun-21 15:23 
GeneralRe: Pass a function with parameter Pin
Greg Utas16-Jun-21 0:22
professionalGreg Utas16-Jun-21 0:22 
QuestionRegarding Constructor Calling in C++ Pin
Member 1522917415-Jun-21 3:08
Member 1522917415-Jun-21 3:08 
AnswerRe: Regarding Constructor Calling in C++ Pin
David Crow15-Jun-21 3:29
David Crow15-Jun-21 3:29 
GeneralRe: Regarding Constructor Calling in C++ Pin
Member 1522917415-Jun-21 3:55
Member 1522917415-Jun-21 3:55 
GeneralRe: Regarding Constructor Calling in C++ Pin
Richard MacCutchan15-Jun-21 5:02
mveRichard MacCutchan15-Jun-21 5:02 
AnswerRe: Regarding Constructor Calling in C++ Pin
Maximilien15-Jun-21 5:01
Maximilien15-Jun-21 5:01 
AnswerRe: Regarding Constructor Calling in C++ Pin
CPallini15-Jun-21 23:39
mveCPallini15-Jun-21 23:39 
QuestionAdvice on writing a GUI application that can run on very old Windows operating systems without Service Packs and Frameworks Pin
arnold_w14-Jun-21 8:32
arnold_w14-Jun-21 8:32 
AnswerRe: Advice on writing a GUI application that can run on very old Windows operating systems without Service Packs and Frameworks Pin
Victor Nijegorodov14-Jun-21 8:43
Victor Nijegorodov14-Jun-21 8:43 
AnswerRe: Advice on writing a GUI application that can run on very old Windows operating systems without Service Packs and Frameworks Pin
Mircea Neacsu14-Jun-21 8:45
Mircea Neacsu14-Jun-21 8:45 
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.

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.