The namespace of IADsLargeInteger

夙愿已清 提交于 2020-01-02 06:10:50

问题


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.


回答1:


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;
}



回答2:


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



来源:https://stackoverflow.com/questions/33274162/the-namespace-of-iadslargeinteger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!