Thursday, September 18, 2014

How to Actually Get the Active Directory Date Information out of __ComObject in .NET

If you're familiar with the .NET LDAP library (System.DirectoryServices), you may have noticed that some entries in the Properties collection for a given DirectoryEntry are instances of System.__ComObject. This is really inconvenient because there's no way to tell what type of thing such an object really is. Even when you know what it should be, it's kind of a pain to extract the data.

For instance, the pwdLastSet and lastLogon properties of user objects show up as __ComObject despite being date-time things. Specifically, they (and most AD objects that you expect to be date-times) are IADsLargeInteger. To convert them into normal DateTime objects, you need a reference to a COM interop DLL in your project, specifically the "Active DS Type Library".

Let's say you have a __ComObject that is supposed to be a date-time, I'll call it p. Use the following code to get it as a nice .NET DateTime:

                Dim li As ActiveDs.IADsLargeInteger = p.Value
                Return DateTime.FromFileTimeUtc((CLng(li.HighPart) << 32) + li.LowPart)

No comments:

Post a Comment