Mark Russinovich at www.sysinternals.com has released PsLoggedOn freeware utility displays both the locally logged on users and users logged on via resources for either the local computer, or a remote one. Full source code is included. Useful to Windows NT admin.
PsLoggedOn’s definition of a locally logged on user is one that has their profile loaded into the Registry, so LoggedOn determines who is logged on by scanning the keys under the HKEY_USERS key. For each key that has a name that is a user SID (security Identifier), PsLoggedOn looks up the corresponding user name and displays it. To determine who is logged onto a computer via resource shares, LoggedOn uses the NetSessionEnum API. Note that PsLoggedOn will show you as logged on via resource share to remote computers that you query because a logon is required for LoggedOn to access the Registry of a remote system.