Making GetObject work for LDAP
-
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.
-
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.
You really should be using
System.DirectoryServices
for this, and only resorting to the ADSI library as a last resort. TheActivator.GetObject
is not related to the VBGetObject
function. To replace the VB functionality, you can either use theMicrosoft.VisualBasic.Interaction.GetObject
function, or use a combination of theBindToMoniker
andGetActiveObject
functions from theSystem.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
-
You really should be using
System.DirectoryServices
for this, and only resorting to the ADSI library as a last resort. TheActivator.GetObject
is not related to the VBGetObject
function. To replace the VB functionality, you can either use theMicrosoft.VisualBasic.Interaction.GetObject
function, or use a combination of theBindToMoniker
andGetActiveObject
functions from theSystem.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
Hi Richard, Thanx a lot for that code! In fact I had tried all the possible combinations of using
Type.GetTypeFromProgID
and then calling theActivator.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.