Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Making GetObject work for LDAP

Making GetObject work for LDAP

Scheduled Pinned Locked Moved C#
csharphelpcomquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Atul Kale
    wrote on last edited by
    #1

    Hi guys
    I am wkg on a C# Component which retrieves information from the ADS on Win2k Domain. For some reason, instead of using the System.DirectoryServices namespace, I have to go for the ADS Library (a COM DLL).

    To write the code in VB.NET I can go ahead and use the GetObject Global method of VB. as following...

    Dim objLDAP as ActiveDS.IAdsOpenDSObject objLDAP = GetObject("LDAP:")
    ' Here it uses the Global GetObject method,
    ' that specifies the First param as path and...
    ' NO ProgID as it is Optional Param.

    In C#, I cannot use the same global function, hence the alternative, Activator.GetObject can be used. But it does not retrieve the reference at all.
    I even tried, Specifically giving the Type param to the Method as...

    IAdsOpenDSObject objLDAP; objLDAP = Activator.GetObject(typeof(ActiveDS.IAdsOpenDSObject), "LDAP:");

    This ends up in giving error message saying that,
    A Proper link to the Service may not have been established correctly. How can I make this work, as I must have all my Components in C# only. Porting this part in VB.NEt only for this reason would be infeasible.

    Please help me out in this. Atul Kale Sr. Software Engineer. XcelVision Technologies Ltd.

    Richard DeemingR 1 Reply Last reply
    0
    • A Atul Kale

      Hi guys
      I am wkg on a C# Component which retrieves information from the ADS on Win2k Domain. For some reason, instead of using the System.DirectoryServices namespace, I have to go for the ADS Library (a COM DLL).

      To write the code in VB.NET I can go ahead and use the GetObject Global method of VB. as following...

      Dim objLDAP as ActiveDS.IAdsOpenDSObject objLDAP = GetObject("LDAP:")
      ' Here it uses the Global GetObject method,
      ' that specifies the First param as path and...
      ' NO ProgID as it is Optional Param.

      In C#, I cannot use the same global function, hence the alternative, Activator.GetObject can be used. But it does not retrieve the reference at all.
      I even tried, Specifically giving the Type param to the Method as...

      IAdsOpenDSObject objLDAP; objLDAP = Activator.GetObject(typeof(ActiveDS.IAdsOpenDSObject), "LDAP:");

      This ends up in giving error message saying that,
      A Proper link to the Service may not have been established correctly. How can I make this work, as I must have all my Components in C# only. Porting this part in VB.NEt only for this reason would be infeasible.

      Please help me out in this. Atul Kale Sr. Software Engineer. XcelVision Technologies Ltd.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You really should be using System.DirectoryServices for this, and only resorting to the ADSI library as a last resort. The Activator.GetObject is not related to the VB GetObject function. To replace the VB functionality, you can either use the Microsoft.VisualBasic.Interaction.GetObject function, or use a combination of the BindToMoniker and GetActiveObject functions from the System.Runtime.InteropServices.Marshal class.

      using System;
      using System.Runtime.InteropServices;

      public class ComUtils
      {
      public static object GetObjectFromClass(string className)
      {
      object ret = Marshal.GetActiveObject(className);

          if (null == ret)
          {
              // **NB:** Not sure if this is needed,
              // but the GetActiveObject functions should
              // only return an existing object, not
              // create a new instance.
              Type t = Type.GetTypeFromProgID(className);
              ret = Activator.CreateInstance(t);
          }
          
          return ret;
      }
      
      public static object GetObject(string pathName)
      {
          return Marshal.BindToMoniker(pathName);
      }
      
      public static object GetObject(string pathName, string className)
      {
          if (null == pathName || 0 == pathName.Length)
          {
              return GetObjectFromClass(className);
          }
          else if (null == className || 0 == className.Length)
          {
              return Marshal.BindToMoniker(pathName);
          }
          else
          {
              // Class and path specified
              object ret = GetObjectFromClass(className);
              
              UCOMIPersistFile pf = (UCOMIPersistFile)ret;
              pf.Load(pathName, 0);
              
              return pf;
          }
      }
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      A 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        You really should be using System.DirectoryServices for this, and only resorting to the ADSI library as a last resort. The Activator.GetObject is not related to the VB GetObject function. To replace the VB functionality, you can either use the Microsoft.VisualBasic.Interaction.GetObject function, or use a combination of the BindToMoniker and GetActiveObject functions from the System.Runtime.InteropServices.Marshal class.

        using System;
        using System.Runtime.InteropServices;

        public class ComUtils
        {
        public static object GetObjectFromClass(string className)
        {
        object ret = Marshal.GetActiveObject(className);

            if (null == ret)
            {
                // **NB:** Not sure if this is needed,
                // but the GetActiveObject functions should
                // only return an existing object, not
                // create a new instance.
                Type t = Type.GetTypeFromProgID(className);
                ret = Activator.CreateInstance(t);
            }
            
            return ret;
        }
        
        public static object GetObject(string pathName)
        {
            return Marshal.BindToMoniker(pathName);
        }
        
        public static object GetObject(string pathName, string className)
        {
            if (null == pathName || 0 == pathName.Length)
            {
                return GetObjectFromClass(className);
            }
            else if (null == className || 0 == className.Length)
            {
                return Marshal.BindToMoniker(pathName);
            }
            else
            {
                // Class and path specified
                object ret = GetObjectFromClass(className);
                
                UCOMIPersistFile pf = (UCOMIPersistFile)ret;
                pf.Load(pathName, 0);
                
                return pf;
            }
        }
        

        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        A Offline
        A Offline
        Atul Kale
        wrote on last edited by
        #3

        Hi Richard, Thanx a lot for that code! In fact I had tried all the possible combinations of using Type.GetTypeFromProgID and then calling the Activator.GetObject etc. etc. But the most important thing I forgot was about the Binding part. I had been wkg on VC++ with Monikers many a times, I just never thought I would need them here too.
        regards Atul Kale MCSD, MCT Sr. Software Engineer XcelVision Technologies Ltd.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups