{"id":108369,"date":"2023-06-22T07:00:00","date_gmt":"2023-06-22T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108369"},"modified":"2023-06-19T06:11:35","modified_gmt":"2023-06-19T13:11:35","slug":"20230622-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230622-00\/?p=108369","title":{"rendered":"How can I find out the last time a user logged on from C++?"},"content":{"rendered":"<p>The PowerShell <code>Get-LocalUser<\/code> cmdlet gives you information about a local user, including the last logon time.<\/p>\n<pre>PS&gt; Get-LocalUser Fred | Format-List\r\n\r\nAccountExpires         :\r\nDescription            :\r\nEnabled                : True\r\nFullName               :\r\nPasswordChangeableDate : 04\/02\/2023 2:25:04 PM\r\nPasswordExpires        :\r\nUserMayChangePassword  : True\r\nPasswordRequired       : False\r\nPasswordLastSet        : 04\/01\/2023 2:25:04 PM\r\nLastLogon              : 04\/01\/2023 2:28:41 PM\r\nName                   : Fred\r\nSID                    : S-1-5-21-162119347-98882960-612995125-1001\r\nPrincipalSource        : Local\r\nObjectClass            : User\r\n<\/pre>\n<p>How can you get this information from C++?<\/p>\n<p>For historical reasons, commands for operating with local users are in the LAN Manager API family. That&#8217;s because MS-DOS was a single-user operating system, so the only time you had to worry about &#8220;users&#8221; was if you were operating over the network.<\/p>\n<p>This is also why a bunch of user management operations are handled by <code>NET.EXE<\/code>. The <code>NET.EXE<\/code> program was the part of LAN Manager that gave you access to all the network magic stuff.<\/p>\n<pre>C:\\&gt; NET USER Fred\r\n\r\nUser name                    Fred\r\nFull Name\r\nComment\r\nUser's comment\r\nCountry\/region code          000 (System Default)\r\nAccount active               Yes\r\nAccount expires              Never\r\n\r\nPassword last set            04\/01\/2023 2:25:04 PM\r\nPassword expires             Never\r\nPassword changeable          04\/02\/2023 2:25:04 PM\r\nPassword required            No\r\nUser may change password     Yes\r\n\r\nWorkstations allowed         All\r\nLogon script\r\nUser profile\r\nHome directory\r\nLast logon                   04\/01\/2023 2:28:41 PM\r\n\r\nLogon hours allowed          All\r\n\r\nLocal Group Memberships      *Administrators\r\nGlobal Group memberships     *None\r\nThe command completed successfully.\r\n<\/pre>\n<p>The native function for getting this information is <code>NetUserGetInfo<\/code>. In particular, asking for information level 2 gives you a <code>USER_INFO_2<\/code> structure which contains, among other things, <code>usri2_last_logon<\/code>.<\/p>\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/api\/lmaccess\/nf-lmaccess-netusergetinfo\"> The documentation<\/a> includes a sample program showing how to request various levels of information and then print the results, so I won&#8217;t bother repeating it here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The information is in the historically-named LANMAN APIs.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-108369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The information is in the historically-named LANMAN APIs.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=108369"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108369\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=108369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}