3

I'm trying to retrieve some obsure Active Directory Attributes:

  1. msexchmailboxsecuritydescriptor, and
  2. terminalservicesprofilepath (in userparameters)

I am having trouble getting to both of them.

For example, for msexchmailboxsecuritydescriptor, if I have code similar to the following:

DirectoryEntry deresult = result.GetDirectoryEntry();
byte[] bteMailACL =(byte[])deresult.Properties["msexchmailboxsecuritydescriptor"].Value; 

It complains that I cannot cast System.__ComObject to System.Byte[], but I have seen several example that use code similar to the above.

How do I understand these blobs of information?

1 Answer 1

5

I think your problem is in .Value part of the statement. Not sure how the examples have been doing it but I've noticed that whenever I call an AD Property like that, I always get an array back of which I get index 0 in case of single result items.

just changing the last statment to:

    byte[] btwMailACL = (byte[])deresult.Properties["msexchmailboxsecuritydescriptor"][0];

solves your problem.

Edit: for production code, please do remember that this can throw a NullReferenceException so do check if the property actually returned a value before calling on the index.

(Tested on my machine and working as above)

2
  • +1. I noticed that too. Some properties usually return several values (e.g. 'member' which lists the members in an AD group). I guess that for consistency reasons, the designers of the API decided that all the values should be an array, even if most of them contain only 1 item. Jun 7, 2011 at 9:13
  • I actually like this design (once i got the hang of it) because now I can just use LINQ on it for most of my operations. Thanks for the +1 :)
    – Maverik
    Jun 7, 2011 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.