

How can I find out the last time a user logged on from C++?
source link: https://devblogs.microsoft.com/oldnewthing/20230622-00/?p=108369
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How can I find out the last time a user logged on from C++?

Raymond Chen
The PowerShell Get-LocalUser
cmdlet gives you information about a local user, including the last logon time.
PS> Get-LocalUser Fred | Format-List AccountExpires : Description : Enabled : True FullName : PasswordChangeableDate : 04/02/2023 2:25:04 PM PasswordExpires : UserMayChangePassword : True PasswordRequired : False PasswordLastSet : 04/01/2023 2:25:04 PM LastLogon : 04/01/2023 2:28:41 PM Name : Fred SID : S-1-5-21-162119347-98882960-612995125-1001 PrincipalSource : Local ObjectClass : User
How can you get this information from C++?
For historical reasons, commands for operating with local users are in the LAN Manager API family. That’s because MS-DOS was a single-user operating system, so the only time you had to worry about “users” was if you were operating over the network.
This is also why a bunch of user management operations are handled by NET.EXE
. The NET.EXE
program was the part of LAN Manager that gave you access to all the network magic stuff.
C:\> NET USER Fred User name Fred Full Name Comment User's comment Country/region code 000 (System Default) Account active Yes Account expires Never Password last set 04/01/2023 2:25:04 PM Password expires Never Password changeable 04/02/2023 2:25:04 PM Password required No User may change password Yes Workstations allowed All Logon script User profile Home directory Last logon 04/01/2023 2:28:41 PM Logon hours allowed All Local Group Memberships *Administrators Global Group memberships *None The command completed successfully.
The native function for getting this information is NetUserGetInfo
. In particular, asking for information level 2 gives you a USER_INFO_2
structure which contains, among other things, usri2_last_logon
.
The documentation includes a sample program showing how to request various levels of information and then print the results, so I won’t bother repeating it here.
Recommend
-
15
Create a Process as Logged-On-User from PowerShell Roger Zander ...
-
19
Logged in User Details In Azure AD App Service Idenifying the Logged-In User in Azure ADLately I have been experimenting with Azure AD and putting Apps behind Azure AD. Once they are behind Azure AD Auth...
-
11
Reporting on user’s last logged in date in Office 365 After a long, long wait, Microsoft is finally addressing one of the most common requests from Office 365/Microsoft 365/Azure AD admins – the ability to easily...
-
12
Get Logged-in User Info in Jakarta EE - The Simplest Way ...
-
16
How to get all the users except current logged in user in Laravel 423 views 5 months ago Laravel While working in L...
-
7
Pankaj Bagul 21 minutes ago how to send logged-in user id from SAP BTP launchpad tile to SAP ABAP application 5...
-
5
ASP.NET Core: Three ways to refresh the claims of a logged-in user An ASP.NET Core application uses claims to hold the logged-in user’s
-
8
krishna sidda October 29, 2022 2 minute read
-
5
krishna sidda October 29, 2022 2 minute read
-
6
How to log out BTP user logged in SAP Build Apps? Skip to Content
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK