Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The namespace of IADsLargeInteger

I looking for way to convert COM object to DateTime and I saw a lot of articles about this problem (like this one -https://msdn.microsoft.com/en-us/library/ms180872(v=vs.80).aspx and this one- How to read "uSNChanged" property using C# )

However, all of those articles talking about using an object from the interface IADsLargeInteger.

I tried to look for the namespace of this interface and I just couldn't find any clue.

like image 618
neriag Avatar asked Oct 22 '15 05:10

neriag


2 Answers

Here is a code sample including everything you need to convert from the AD type to a DateTime:

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using ActiveDs; // Namespace added via ref to C:\Windows\System32\activeds.tlb

private DateTime? getLastLogin(DirectoryEntry de)
{
    Int64 lastLogonThisServer = new Int64();

    if (de.Properties.Contains("lastLogon"))
    {
        if (de.Properties["lastLogon"].Value != null)
        {
            try
            {
                IADsLargeInteger lgInt =
                (IADsLargeInteger) de.Properties["lastLogon"].Value;
                lastLogonThisServer = ((long)lgInt.HighPart << 32) + lgInt.LowPart;

                return DateTime.FromFileTime(lastLogonThisServer);
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
    return null;
}
like image 83
Shane K Avatar answered Sep 30 '22 06:09

Shane K


In addition to the previous answer, which shows correct code to get value from IADsLargeInteger variable , I just want to say that there's no need to add a reference to a COM Types library if you need only this interface.

To work with COM type you can define interface in your own code:

[ComImport, Guid("9068270b-0939-11d1-8be1-00c04fd8d503"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
internal interface IAdsLargeInteger
{
    long HighPart
    {
        [SuppressUnmanagedCodeSecurity] get; [SuppressUnmanagedCodeSecurity] set;
    }

    long LowPart
    {
        [SuppressUnmanagedCodeSecurity] get; [SuppressUnmanagedCodeSecurity] set;
    }
}

and use it the same way:

var largeInt = (IAdsLargeInteger)directoryEntry.Properties[propertyName].Value;
var datelong = (largeInt.HighPart << 32) + largeInt.LowPart;
var dateTime = DateTime.FromFileTimeUtc(datelong);

There's also a good article, explaining how to interpret ADSI data

like image 31
Mikhail Tumashenko Avatar answered Sep 30 '22 07:09

Mikhail Tumashenko