I'm sure there is an easier way of doing this but, when I had to do that for work I used a VB script and sent the output to a file (i.e. “UserLoginTimes.log”). Then inside C++ I parsed the file to find the user I wanted the info from.
Here is the script I used…
Code:
' List last logon times
' 2005 Zak AInsworth
On Error Resume Next
sEnterDCs = "NSOLAB"
sObjects = Split(sEnterDCs, ",")
Set oDomain = GetObject("WinNT://" & sObjects(0))
oDomain.Filter = Array("User")
WScript.Echo "Showing last login times of accounts from: " & oDomain.Name & vbNewLine
For Each oDomainItem In oDomain
sUsrLogin = oDomainItem.LastLogin
If UBound(sObjects) >= 1 Then
For ii = 1 To UBound(sObjects)
Set oUsr = GetObject("WinNT://" & sObjects(ii) & "/" & oDomainItem.Name & ",user")
If oUsr.LastLogin > sUsrLogin Then sUsrLogin = oUsr.LastLogin
Next
End If
WScript.Echo "Username: " & Left(oDomainItem.Name & Space(22),22) & "Last login: " & FormatDateTime(sUsrLogin)
Next
(on the line ‘sEnterDCs = "NSOLAB"’, change “NSOLAB” to your domain name.)
Name the script something like “GetUserLoginTimes.vbs”.
You can add this line of code to your C++ source…
Code:
System(“cscript GetUserLoginTimes.vbs > UserLoginTimes.log”);
This will run the script and send the output to “UserLoginTimes.log”.
Now it’s up to you to do the parsing. The script lists each user on a new line which makes is pretty easy.
Good Luck…
-Zak
Bookmarks