session 1
TRANSCRIPT
COM Interoperability 2 of 27
Session Objectives
Explain .NET components evolution from COM
Use COM components from .NET Explain the Runtime Callable Wrapper Explain the COM Callable Wrapper Discuss the Type Library conversion into
Metadata Use .NET components from COM
COM Interoperability 3 of 27
Introduction In today’s world Business Logic resides
in COM
With the evolution of .NET , COM poses a problem of compatibility
The .NET runtime simplifies the complexities involved in compatibility
COM Interoperability 4 of 27
COM COM facilitates language independent
code re-use COM components provided additional
features like IUnknown interface COM component needs to be identified
uniquely using a 128 bit GUID (Globally Unique Identifier)
COM Interoperability 5 of 27
COM The COM component needs to be
registered in the Windows Registry It turned out to be a great problem
because Cross Language Inheritance is not possible using COM
COM Interoperability 6 of 27
.NET -1 .NET is a component technology like
COM Issues like cross language inheritance
are taken care of, in .NET
COM Interoperability 7 of 27
.NET -2
.NET code executes in an environment entirely managed by the CLR
This includes Garbage collection and Security for a .NET application
COM Interoperability 8 of 27
Component handling in COM and .NET -1
COM exposes its classes and methods through interfaces
.NET exposes its classes and methods in an assembly using metadata and reflection
The type information of a COM component resides in an external Type Library file
COM Interoperability 9 of 27
Component handling in COM .NET-2 Metadata is embedded within an assembly Interoperating between these two is a
challenge .NET achieves this by creating a proxy
class for every COM component that the .NET client tries to access
It is the proxy that talks directly to the component
COM Interoperability 10 of 27
Adding a reference To make use of a COM component in an
application, we just add a reference to the Project
COM Interoperability 11 of 27
Using the COM component After adding the reference, we can start
using the classes within the COM component as if it were a .NET component
COM Interoperability 12 of 27
Runtime Callable Wrapper The proxy that talks to the component
is known as the RCW (Runtime Callable Wrapper)
It is a .NET assembly The RCW takes care of type casting of
data types apart from various other activities
COM Interoperability 14 of 27
Runtime Callable Wrapper The RCW assembly is automatically
created by Visual Studio.NET in the applications directory
The RCW does not contain any form of business logic
It has the type information of the elements contained within the COM component
COM Interoperability 15 of 27
Runtime Callable Wrapper The type information is Marshaled from
COM types into .NET types and then written into the assembly
This type information is stored in the form of Metadata as it is a .NET assembly
This helps Intellisense in Visual Studio.NET
COM Interoperability 16 of 27
COM vs. .NET COM components manage their lifetime
whereas the CLR manages the lifetime in .NET
COM components use interfaces whereas .NET clients use reflection to learn about the functionality of the object
COM Interoperability 17 of 27
COM vs. .NET COM components return a memory pointer to
an object, hence they must reside in the same memory location throughout the lifetime of the object
.NET objects can shift between virtual and physical memory freely as either becomes more available, making optimum utilization of memory space
COM Interoperability 18 of 27
COM vs. .NET COM components manage the lifetime
by using reference counting When this count reaches zero the
object of that component is destroyed In .NET, the CLR manages the lifetime
of the object
COM Interoperability 19 of 27
COM vs. .NET The COM component created using
.NET are destroyed by the CLR when the Reference count reaches zero
Regarding the lifetime of RCW, only one copy of the RCW object is created no matter how many clients request the same COM component
COM Interoperability 21 of 27
The TlbImp Utility Is used to convert a COM Type library
into a .NET assembly Syntax –
TlbImp <COM Dll Name> / out:<Proxy Name>
COM Interoperability 22 of 27
Accessing a .NET component from COM .NET provides a mechanism where COM
clients can call .NET components Information of the .NET component
needs to be registered in the Windows Registry
To register or unregister a .NET class with COM you must run a command line tool
COM Interoperability 23 of 27
Assembly Registration Tool
The command line tool to register or unregister is –
RegAsm.exe Syntax –RegAsm/install <dotNETAssembly.dll>
COM Interoperability 24 of 27
COM Callable Wrapper The runtime generates another wrapper
object It is known as the CCW ( COM Callable
Wrapper) The CCW is same as the RCW, it
bridges the gap between two environments
COM Interoperability 25 of 27
COM Callable Wrapper The CCW acts as a proxy between the
COM & .NET components
COM Interoperability 26 of 27
Lifetime of CCW The runtime creates exactly one CCW
for a managed .NET component