Groups | Blog | Home
all groups > dotnet interop > november 2005 >

dotnet interop : Implementing ADSI and using System.DirectoryServices


Willy Denoyette [MVP]
11/1/2005 12:00:00 AM

[quoted text, click to view]

IAds is a COM native interface, how did you manage to get your version
registered correctly?
Also, I'm not clear what language was used to implement your version? .Net
is not a language it's a ... well it's actualy a marketing term ;-).

Willy.


dumchikov NO[at]SPAM gmail.com
11/1/2005 4:57:37 AM
Hi,

I have implemented the IADs interface in .NET. And I want to use
System.DirectoryServices.DirectoryEntry class with my implementation.
The DirectoryEntry has the constructor DirectoryServices(Object obj).
So I supposed, that I can use it if I passed my implementation there.
But the ArgumentException was thrown when I called the cunstructor.
Then I used Reflector to see this constructor and found the code, which
thrown the exception. It was something like this:

this.adsObject = adsObject as UnsafeNativeMethods.IAds;
if (this.adsObject == null)
{
throw new
ArgumentException(Res.GetString("DSDoesNotImplementIADs"));
}

My question is why my implementation of the IADs couldn't be cast to
the UnsafeNativeMethods.IAds? After all, they are the same.
dumchikov NO[at]SPAM gmail.com
11/1/2005 10:50:17 PM
Willy Denoyette [MVP] =D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0):

[quoted text, click to view]

Yes, I know that IADs is a COM interface. But I can implement this
interface and other ADSI interfaces using C#, then add needed records
to the registry. As I understood, I can register my COM server written
on C# as well as it was written on unmanaged C++. During loading the
dll with managed code Windows will understand, that it is a manage
assembly and load MSCorEE.dll to perform interop tasks.
But my question is about how to use .NET implementation of ADSI in the
DirectoryServices namespace.
dumchikov NO[at]SPAM gmail.com
11/2/2005 11:15:47 PM
[quoted text, click to view]

Yes the DirectoryServices namespace is quite useful.
The thing is that I need to implement my own ADSI provider on .NET. So
I did following things:

1) Created description of IADs

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("FD8256D0-FD15-11CE-ABC4-02608C9E7553")]
public interface IADs
{
// Properties and methods
}

2) Implemented this interface
internal class ADsObject : IADs
{
// implementation
}

3) Tried to use the implementation

ADsObject obj = new ADsObject("adsPath");
DirectoryEntry entry = new DirectoryEntry(obj);

But the ArgumentException was thrown in the last line.
That's the problem.
Willy Denoyette [MVP]
11/3/2005 12:00:00 AM

[quoted text, click to view]

Let's take a close look at what you are trying to do here (if above is all
you did).
- You implemented a single interface in a managed class (IAds) and decorated
it with some COM interop attributes, the CLR will construct a CCW
representing the COM interface whenever a COM client tries to create an
instance.

- Now your managed client creates an instance of your managed class
ADsObject, what is returned is NOT a COM interface (wrapped in a RCW), it's
just a plain normal managed object reference.
Then you pass this reference as argument to to DirectoryEntry, but now the
trouble starts, your IADs "is not a"
System.DirectoryServices.Interop.UnsafeNativeMethods.IADs type, it's an
Yournamespace.IADs type. So the cast fails with a bad argument exception.

I hope you understand by now that what your are trying to do is not possible
(heaven thanks), unless you are implemeneting the provider AND your own
DirestoryServices namespace, or if you are replacing the COM provider (all
interfaces of ActiveDS.dll)) by your own native COM server, using the same
GUID's. I assume this is not something you are willing to do, right?


Willy.






Marc Scheuner [MVP ADSI]
11/3/2005 7:29:38 AM
[quoted text, click to view]

Yes, but why would you even bother to do so? System.DirectoryServices
with DirectoryEntry and DirectorySearcher is much easier to use in the
first place..... why create potential problems with interop and stuff
like that when you could use the out-of-the-box .NET components
instead?

Marc
================================================================
Marc Scheuner May The Source Be With You!
AddThis Social Bookmark Button