The Standard Method
There is not much more to say here. In this case, we just create a runtimecallable wrapper (RCW) interop assembly for the entire activeds.tlb type library using tlbimp.exe or Visual Studio .NET. For Visual Studio .NET, this is as simple as using Project > Add Reference and selecting activeds.tlb from the COM tab. If we are not using Visual Studio .NET, we need to use the command-line tool from the .NET SDK, called tlbimp.exe. Here is a sample command line for using this tool (watch for the wrap):
tlbimp.exe activeds.tlb /out:activedsNET.dll /namespace:ActiveDS
This will create an RCW called activedsNET.dll that we should deploy with our application. If necessary, we can also provide a strong name for the generated assembly so that we can call it from our own strong-named assemblies as well. The .NET runtime includes a security restriction that prevents strongly named assemblies from calling into assemblies that are not strongly named. For more information about code access security (CAS) policy as it relates to strong names, see Chapter 8.
Advantages
The advantages of this approach are that it is easy and the performance is good.
Disadvantages
The downsides of this approach are as follows.
- We have an extra assembly to deploy with our code all the time.
- We may not like the way the default conversion renders some of the ADSI types. For example, none of the enumerations that can be combined bitwise has the FlagsAttribute attribute applied to it, and the IDirectorySearch and IDirectoryObject interfaces are not really useful with the default conversion.
Because of the ease of use of this approach, we have tended to use it throughout the book, even though it might not be ideal for all situations.
Microsoft could actually improve on the second point by providing what is called a Primary Interop Assembly (PIA) for activeds.dll, similar to what it does with the Microsoft Office automation libraries, which is handcrafted to address these problems and includes a strong name. However, Microsoft has thus far not elected to do this for us.