Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DirectoryServices using C# getting lastLogon (LargeInteger) to C# long ...

988 views
Skip to first unread message

Joakim Westman (Elicit AB)

unread,
Mar 26, 2003, 7:19:58 AM3/26/03
to

Hi!
I have problems obtaining the proper date for the lastLogon property using
DirectoryServices.

NET will not cast this property to a correct value (long, required by
DateTime.FromFileTime), so
I have seen that one need to obtain the LargeInteger value from the
ComObject and then convert HighPart and LowPart to a C# long.

However, the code always returns Exceptions.

Has anybody done this successfully ?

See code excerpt:

try

{

ActiveDs.LargeIntegerClass li = (ActiveDs.LargeIntegerClass)
System.Runtime.InteropServices.Marshal.CreateWrapperOfType(result.Properties
["lastLogon"][0], typeof(ActiveDs.LargeIntegerClass));

}

This call gives me the following Exception:

The object's type must be __ComObject or derived from __ComObject. Parameter
name: o

I cannot really see how to convert this properly.

the result.Properties["lastLogon"][0].ToString(), or
System.Convert.ToInt64(result.Properties["lastLogon"][0]) will give me an
incorrect value as you know, but then I know I am getting values from
ActiveDirectory. I get the other properties I want.

Regards,

// Joakim W


David Stucki [MS]

unread,
Mar 26, 2003, 5:35:05 PM3/26/03
to
Yes, this works for me:

//Add a project reference to Active Ds Type Library
using ActiveDs;
using System.DirectoryServices;

[...]

DirectoryEntry entry = new
DirectoryEntry("LDAP://cn=user,cn=users,dc=domain,dc=com");
if(entry.Properties.Contains("lastLogon"))
{
IADsLargeInteger li = (IADsLargeInteger)entry.Properties["lastLogon"][0];
long date = (long)li.HighPart << 32 | (uint)li.LowPart;
DateTime time = DateTime.FromFileTime(date);
Console.WriteLine("Last logged on at: {0}", time);
}

David Stucki
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Joakim Westman (Elicit AB)

unread,
Mar 28, 2003, 2:15:16 AM3/28/03
to
Hi and thank you for your reply.

I tried the code and I get another error: e.Message = "Specified cast is not
valid."
I am calling from a WebService running on Win2000Server.

this is my code:
----------------------------------------------------------------------------
----------------------------------------

using System.DirectoryServices;
using ActiveDs;
[...]
[...]

foreach(SearchResult result in results)
{

EliottWS.Users.DirectoryUser dUser = new EliottWS.Users.DirectoryUser();

// This is working perfectly alright
if(result.Properties.Contains("employeeID"))
dUser.EmployeeID = result.Properties["employeeID"][0].ToString();
else
dUser.EmployeeID = "not available";

if(result.Properties.Contains("lastLogon"))
{
try
{
IADsLargeInteger li =
(IADsLargeInteger)result.Properties["lastLogon"][0];
//long date = (long)li.HighPart << 32 | (uint)li.LowPart;
//DateTime time = DateTime.FromFileTime(date);
}
catch(Exception e)
{
//dUser.LastLogon is of type string
dUser.LastLogon = e.Message;
}
//dUser.LastLogon = time.ToString();
}
else
dUser.LastLogon = "not available";
}

Regards,
// Joakim W


Joakim Westman (Elicit AB)

unread,
Mar 28, 2003, 2:47:07 AM3/28/03
to
Hi again!
I found the problem.

I had a SearchResult.Properties but i needed to get the
DirectoryEntry.Properties in order to get it to work.

Thanks again for your help though.

Cheers,
// Joakim W


"Joakim Westman (Elicit AB)" <_no_spam_Joa...@elicit.se> wrote in
message news:eFx7JnP9...@TK2MSFTNGP11.phx.gbl...

0 new messages