Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

Implementing ADSI and using System.DirectoryServices

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dumchikov@gmail.com - 01 Nov 2005 13:57 GMT
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.
Willy Denoyette [MVP] - 02 Nov 2005 00:05 GMT
> Hi,
>
[quoted text clipped - 15 lines]
> My question is why my implementation of the IADs couldn't be cast to
> the UnsafeNativeMethods.IAds? After all, they are the same.

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@gmail.com - 02 Nov 2005 07:50 GMT
Willy Denoyette [MVP] ():

> 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.

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.
Marc Scheuner [MVP ADSI] - 03 Nov 2005 07:29 GMT
>Yes, I know that IADs is a COM interface. But I can implement this
>interface and other ADSI interfaces using C#,

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!
Berne, Switzerland                      m.scheuner -at- inova.ch
dumchikov@gmail.com - 03 Nov 2005 08:15 GMT
> >Yes, I know that IADs is a COM interface. But I can implement this
> >interface and other ADSI interfaces using C#,
[quoted text clipped - 9 lines]
> Marc Scheuner                        May The Source Be With You!
> Berne, Switzerland                      m.scheuner -at- inova.ch

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] - 03 Nov 2005 18:22 GMT
>> >Yes, I know that IADs is a COM interface. But I can implement this
>> >interface and other ADSI interfaces using C#,
[quoted text clipped - 37 lines]
> But the ArgumentException was thrown in the last line.
> That's the problem.

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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.