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
//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.
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
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...