Thursday, December 19, 2013

Difference between .NET DLL & COM DLL

What is the Difference between .NET DLL & COM DLL?

Friends I am sharing , here is the actual and understandable solution for this question: 

When you implement a Assembly (DLL) in a .NET Language (e.g. C#, VB.NET,...) you produce a managed assembly. A manage assembly contains managed code and is executing by the .NET Run-time. (this is .NET DLL)

When you create a DLL with C++ or VB 6.0 you produce a win32 / com DLL.
(this is COM DLL)

If you use this dll in a .NET Program, the Visual Studio create automatically a INTEROP file for you, so you can call the "unmanaged" dll from manage code (.NET Runtime Code). 

Also you can call a managed DLL from unmanaged code. For that you can use the regasm-tool to create a wrapper. 

With a managed assembly (DLL) you have all advantages of the .net runtime,
 e.g.: Code Access Security (CAS), Garbage Collection (GC), ...

Thats why when you refer a dll in Visual Studio you will find separate tabs for .NET DLL's and COM DLL's(which makes me to write this post

You can even Create COM DLL with C#,but some steps,procedures must be followed,listed below:

COM DLL using C#


1. Basic Coding Requirements.
1.1 You would have to create COM-callable C# classes contained in a class library.

1.2 These C# classes would have to be decorated with the ComVisibleAttribute (with a parameter value of true).

1.3 The classes also must at least expose a public default constructor.

1.4 Other attributes which are also useful include the GuidAttribute, ProgIdAttribute and the ClassInterfaceAttribute. But the ComVisibleAttribute and the public default constructor are mandatory requirements.

1.5 Properties and methods of the C# classes which are to be callable by the VC++ client would have to be declared as public.

2. Registration For COM Usage.
2.1 The class library DLL would have to be registered for COM by using REGASM.EXE. You must use the/tlb flag which causes REGASM.EXE to produce an associated COM type library.
2.2 REGASM.EXE essentially records COM-related information about your C# classes in the registry so that these C# classes may be created at runtime as COM objects.
2.3 Your VC++ client apps would have to reference this type library (mentioned in point 2.1 above) in order to create the COM classes which have been exposed from your class library.
2.4 Note that the COM objects, which are actually C# objects, operate in unmanaged code via COM-callable-wrappers.
3. Runtime Locating Your C# Class Library.
3.1 At runtime, when the unmanaged application creates your C# COM classes, the standard CLR assembly search process is used in order to locate your class library.
3.2 Generally speaking, the client's config file is consulted, then the GAC is looked up, then the local directory of the client app is searched and finally something known as the CodeBase (a registry entry) is looked up.
3.3 For simplicity, copy the C# class library and all dependencies into the same directory as the client application.
4. Sample Codes.
4.1 For sample start up codes, pls refer to :
http://limbioliong.wordpress.com/2011/08/30/creating-a-com-server-using-c/

No comments: