INTEROPERABILITY (RCW and CCW)

 Interoperability:
.NET enables interoperability with unmanaged code through platform invoke services, the System.Runtime.InteropServices namespace, C++ interoperability, and COM interoperability (COM interop).

a) Code that executes under the control of the runtime is called managed code. 
b) Code that runs outside the runtime is called unmanaged code. 
c) COM components, ActiveX interfaces, and Windows API functions are examples of unmanaged code.

Runtime Callable Wrapper(RCW):
a) The common language runtime exposes COM objects through a proxy called the runtime callable wrapper (RCW). 
b) Although the RCW appears to be an ordinary object to .NET clients, its primary function is to marshal calls between a .NET client and a COM object.
c) The runtime creates exactly one RCW for each COM object, regardless of the number of references that exist on that object. 
d) If you create an RCW in one application domain or apartment, and then pass a reference to another application domain or apartment, a proxy to the first object will be used. 
e) Note that this proxy is a new managed object and not the same as the initial RCW; this means the two managed objects are not equal but do represent the same COM object.`
f) Each RCW maintains a cache of interface pointers on the COM object it wraps and releases its reference on the COM object when the RCW is no longer needed. 
g) The runtime performs garbage collection on the RCW.



Exposing COM Components to C#:
The general steps are as follows:
a) Locate a COM component to use and register it. 
Use regsvr32.exe to register or un–register a COM DLL.
b) Add to the project a reference to the COM component or type library. 
i) Visual Studio uses the Tlbimp.exe (Type Library Importer), which takes a type library as input, to output a .NET interop assembly. 
       ii) The assembly, also named a runtime callable wrapper (RCW), contains managed classes and interfaces that wrap the COM classes and interfaces that are in the type library. 
      iii) Visual Studio adds to the project a reference to the generated assembly.
      iv) Create an instance of a class defined in the RCW. 
       v) Creating an instance of that class creates an instance of the COM object.
      vi) Use the object just as other managed objects. 
     vii) When the object is reclaimed by garbage collection, the instance of the COM object is also released from memory.
  
COM Callable Wrapper(CCW):`
a) When a COM client calls a .NET object, the common language runtime creates the managed object and a COM callable wrapper (CCW) for the object. 
b) Unable to reference a .NET object directly, COM clients use the CCW as a proxy for the managed object.


  
Exposing C# to COM:
The basic steps to expose C# types are as follows:
a) Add interop attributes in the C# project. 
b) Generate a COM type library and register it for COM usage.
c) Visual Studio uses the Regasm.exe (Assembly Registration Tool), using the /tlb command-line switch, which takes a managed assembly as input, to generate a type library. 
d) This type library describes the public types in the assembly and adds registry entries so that COM clients can create managed classes.

Refer:
a) https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/interop/
b) https://learn.microsoft.com/en-us/dotnet/standard/native-interop/com-wrappers
c) https://learn.microsoft.com/en-us/dotnet/standard/native-interop/runtime-callable-wrapper
d) https://learn.microsoft.com/en-us/dotnet/standard/native-interop/com-callable-wrapper
e) https://learn.microsoft.com/en-us/dotnet/standard/native-interop/qualify-net-types-for-interoperation

Popular posts from this blog

OBJECT ORIENTED ANALYSIS AND DESIGN (OOAD)

OBJECT ORIENTED PROGRAMMING

STARTING A BUSINESS