dot net

Presentation By: Mohit Batra

Interoperability Defined
1

.NET Interoperability

The Target…
  

.NET Interoperability with COM COM Interoperability with .NET Win32 Interoperability with .NET

.NET Interoperability

2

Agenda…
      

Why ? Challenges Design Considerations Solution Using COM with .NET Using .NET with COM Calling Win32 from .NET

.NET Interoperability

3

But Why… ?

 

Allow Managed and Unmanaged Code to Work Together Reusability of COM components Windows – COM and Native API forms Core

.NET Interoperability

4

Challenges…
   

Synchronize Memory Allocation Object Lifetime Management Security Two Way Communication

.NET Interoperability

5

Design Considerations…
Characteristic Coding model Identity Unmanaged model Interface-based GUIDs Managed model Object-based Strong names Exceptions

Error handling mechanism HRESULTs Type compatibility Type definition Type safety Versioning .NET Interoperability

Binary standard Type standard Type library Metadata Not type safe Optionally safe Immutable Resilient 6

Solution…A Bridge
 

Loose coupling approach Benefits
 

Two way communication Graceful interaction

.NET

COM

.NET Interoperability

7

Using COM with .NET…
 

Called Backward Compatibility The Bridge – Runtime Callable Wrapper (RCW)

COM Server

RCW

.NET Client

.NET Interoperability

8

Using COM with .NET…

  

Single Instance of RCW for all .NET Clients RCW manages lifetime of Object Object appears as present on Heap No Garbage Collection for COM; Collected by OS Use Type Library Importer (TLBIMP.exe) to create RCW or say Add Reference in VS
9

.NET Interoperability

Using COM with .NET…
Caution: RCW may not be correct always Solution:  Use Managed C++  Requires no Interop Code  Allows both Managed and Unmanaged Code  Expose Managed C++ Code for Interop
.NET Interoperability 10

Using .NET with COM…
Enough of Backward Compatibility  Called Forward Compatibility  The Bridge – COM Callable Wrapper (CCW)
COM Client
CCW

.NET Server

.NET Interoperability

11

Using .NET with COM…

CCW contains COM compatible Registry entries Classes in Assembly are registered with Registry Use Regasm.exe to read Assembly Metadata and add Registry entries Use Type Library Exporter (TLBEXP.exe) utility creates COM wrappers for .NET components
12

.NET Interoperability

Using .NET with COM…
Recommended: Avoid migrating current COM applications immediately to .NET Solution:  Use piecemeal approach; gradually replace COM components with equivalent .NET assemblies
.NET Interoperability 13

Calling Win32 from .NET…

Called Platform Invoke (PInvoke) Services Import Signature and start calling methods Use DllImport Attribute to Import Signatures

.NET Interoperability

14

Calling Win32 from .NET…
Caution:  Overhead of 10-30 x86 instructions per call  Overhead of Marshalling, transferring data b/w managed and unmanaged environment  Costly Data type conversion operation Solution:  Use few PInvoke calls Watch: .NET Interoperability 15

Thanks !

.NET Interoperability

16