1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Anomander
  • 11
  • 2
  • Without knowing the actual value that `rs.Fields[FieldNumber].Value` is actually returning, it is hard to diagnose this kind of problem. But one thing you should do is make sure the returned `OleVariant` is not empty before you process its value. You are not doing that in the code you showed. – Remy Lebeau Jul 17 '13 at 20:00
  • rs.Fields[FieldNumber].Value returns the value that is read from the AD with the name: LastLogon It is run in a loop for every account once. – Anomander Jul 18 '13 at 07:31
  • Problem solved. int64Value := int64Value or LI.LowPart; was the problem. In some cases (50%) the result value of this line is a negative value. If i omit this line, everything works fine. – Anomander Jul 18 '13 at 09:25
  • " I am not allowed to install Delphi at my work " - then install something free like Lazarus or CodeTyphon, this MIGHT be helpful to find the problem, or not. " i took this piece of code from the internet" , so it was just a proof of concept not expected to really work. Www.translate.ru -> http://www.gunsmoker.ru/2010/05/90.html – Arioch 'The Dec 27 '16 at 13:48

0 Answers0