I am trying to get the LastLogin information from AD at my work. I have about 1200 accounts. When i run a Query and include the LastLogin information, for about 40 - 45% of the accounts, i get a correct Date returned. For the others, i get a default value. Which could be 01-01-1601 or 01-01-1970, depending on what kind of conversion i use. When i use the very slow: Net User /Domain, i can extract the last login information, but instead of 10 seconds, i need 10 minutes. Obviously, i want something faster.
Here is the code i use:
var
LI: OleVariant;
int64Value: Int64;
LocalTime: TFileTime;
SystemTime: TSystemTime;
FileTime : TFileTime;
begin
try
LI := rs.Fields[FieldNumber].Value;
int64Value := LI.HighPart;
int64Value := int64Value shl 32;
int64Value := int64Value or LI.LowPart;
FileTime := TFileTime(int64Value);
Result := EncodeDate(1601,1,1);
if FileTimeToLocalFileTime(FileTime, LocalTime) then
if FileTimeToSystemTime(LocalTime, SystemTime) then
Result := SystemTimeToDateTime(SystemTime);
except
Result := 0;
end;
end;
The above works, but only in about 40 - 45% of the cases. Either the value is not returned by AD or there is something wrong in the conversion. i do not know which of those it is. Since some part of it works, i am inclined to say AD does not return me the values i need. When i check directly in AD, i see the correct value. Retreiving them with a query does not allways give me this information. The problem for me is, i took this piece of code from the internet and honestly i understand a little but not everything. I get lost with the numbers i can't see. Futhermore, debugging everything is not possible for me. I am not allowed to install Delphi at my work. I can only develop on a computer that is not connected to the network and copy my executable to the network to try/test everything.