COM Objects

G

Guest

I'm retrieving all the properties from a DirectoryEntry object and some of the properties are __ComObjects. How can I handle these in my code? Basically, I want to display the values in HRF (human readable format). I'm pretty sure that the values I'm dealing with are date/times but I'm not sure, so I will have to check the type as well.
 
G

Guest

Ok, I've disected the objects and they appear to be of the generic Object class. When I use GetMethods/Members/Properties I get the following list for both Members & Methods while there does not appear to be any properties. My questions still stand though, how can I manipulate these objects and get some meaningful data from them? I've listed the Active Directory properties where these objects appear as well

Method/Member list
CreateObjRe
InitializeLifetimeServic
GetLifetimeServic
GetHashCod
Equal
ToStrin
GetTyp

AD Property List - (from objectClass=user)
uSNCreate
uSNChange
badPasswordTim
lastLogof
lastLogo
pwdLastSe
accountExpire
nTSecurityDescriptor
 
W

Willy Denoyette [MVP]

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:D}" ,dt);
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top