-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Suresh[MS MVP]
Message 2 in Discussion

Hi Rupreet,                  The exception your geting may be because of the 
marshaling problem ,Type cast your object properly."ActiveDs" is COM object so u need 
to be careful to handle it.   Have a look on the following link to create new use on 
your AD. 
http://support.microsoft.com/default.aspx?scid=kb;%5BLN%5D;313114&product=vcSnet   
Here is some code i found on internet,Have a look on it how it handles the COM 
Exception.   using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using System.Reflection;
using activeds; // Import activeds.tlb (%windir%\system32\activeds.tlb)
class Test {
 static string bindUser = "administrator";    // binding user with sufficient 
privileges
 static string bindPwd = "hispwd";    // password of the binding user
 static void ListUserAndGroups(string machineName)
 {
  DirectoryEntry  _compContainer = new DirectoryEntry("WinNT://" + machineName + 
",computer", bindUser, bindPwd);
  try
  {
   foreach(DirectoryEntry de in _compContainer.Children)
   {
    switch (de.SchemaClassName.ToLower())
    {
     case "group":
      Console.WriteLine("---------- group - {0} ---------", de.Name);
      ListMembersInGroup(de.Path);
     break;
     case "user":
      Console.WriteLine("---------- user - {0} ---------", de.Name);
      ListUserProp(de.Path);
     break;
     default:
      break;
    }
   }
  }
  finally {
   _compContainer.Dispose();
  }
 }


private static void ListMembersInGroup(string dirPath) {
 IADsMembers MembersCollection = null;
 DirectoryEntry _groupEntry = new DirectoryEntry(dirPath ,bindUser, bindPwd);
 try {
 // call  native method "members" on the IADsGroup COM interface exposed by 
activeds.dll
  IADsGroup gr = _groupEntry.NativeObject as IADsGroup;
  MembersCollection = gr.Members();
  // or call Invoke on the DirectoryEntry object passing the Method to call as arg.
  // cast the retruned object to IADsMembers
//  MembersCollection = _groupEntry.Invoke("Members") as IADsMembers;
  object[] filter = {"user"};
  MembersCollection.Filter = filter;
  // enumerate members of collection object that supports the IADsMembers interface
  // ADSI provider doesn't support count property!!
  try {
   foreach (IADsUser member in MembersCollection) {
    Console.WriteLine("[{0}]", member.Name);
    ListUserProp(member.ADsPath);
   }
  }
  catch (COMException e) {
   Console.WriteLine("Error: {0}",e.Message);
  }
 }
 catch (COMException e) {
  Console.WriteLine(e.Message);
 }
 finally {
  _groupEntry.Dispose();
 }
}
 private static void ListUserProp(string dirPath) {
  DirectoryEntry userEntry = null;
  try {
   userEntry = new DirectoryEntry(dirPath,bindUser, bindPwd);
   PropertyCollection pcoll = userEntry.Properties;
   foreach(string sc in pcoll.PropertyNames)
    Console.WriteLine("\t" + sc + "\t" + pcoll[sc].Value);
  }
  catch (COMException e) {
   Console.WriteLine(e.Message);
  }
  finally
  {
   userEntry.Dispose();
  }
 }

 public static void  Main() {
  ListUserAndGroups("scenic");
 }
}
 Hopes this will help.   Thanks and regards,   
_____________________________________________________________________________________________
Suresh Ch Behera
Microsoft MVP.Net | MCAD.Net (CM) | MCSD.Net (EA)    

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to