Some properties are returned as a Generic COM wrapper object __ComObj,
because:
1) They are not declared in the System.DirectoryServices namespace classes,
2) or there is no Typelib declaration for the corresponding to the COM
interface
You have three options to solve this issue using C#.
a) Use reflection to read the properties of the underlying COM interface,
b) Import the activeds.tlb in your project (add a COM reference).
Following illustrates how to retrieve the usnCreated attribute using option
a):
if(Marshal.IsComObject(pcoll["usnCreated"].Value)) // is __ComObj?
{
object obj = pcoll["usnCreated"].Value; // __ComObj derives from
Object
// Get LowPart property from ILargeInteger (INTEGER8 adstype)
object low = obj.GetType().InvokeMember("LowPart",
BindingFlags.GetProperty,null, obj, null);
// Get HighPart property from ILargeInteger (INTEGER8 adstype)
object high = obj.GetType().InvokeMember("HighPart",
BindingFlags.GetProperty,null, obj, null);
// Convert to long
long usn = (Convert.ToInt64(high) << 32) + Convert.ToInt64(low);
Console.WriteLine(usn);
}
And this shows how get the accountExpires property (or any other
LargeInteger property representing a date) using b)
....
using activedsImport; // Imported activds.tlb Interop assembly's
namespace
.....
LargeInteger li = pcoll["accountExpires"].Value as LargeInteger;
long date = (((long)(li.HighPart) << 32) + (long) li.LowPart);
if((li.HighPart == -1) && (li.LowPart == -1)) {
Console.WriteLine("Account never expires");
}
else {
// Valid date, convert to DateTime format
// Note that this date is one day later than the date displayd in the
Directory Users and Computers MMC
string dt = DateTime.FromFileTime(date).ToString();
Console.WriteLine("DATE = {0
}" ,dt);
}
Willy.