You are on page 1of 16

dot net

Interoperability
Defined
Presentation By:
Mohit Batra

.NET Interoperability 1
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…
Unmanaged Managed
Characteristic model model
Coding model Interface-based Object-based

Identity GUIDs Strong names

Error handling mechanism


HRESULTs Exceptions

Type compatibility Binary standard Type


standard
Type definition Type library Metadata
Type safety Not type safe Optionally
safe
Versioning Immutable Resilient

.NET Interoperability 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
.NET Interoperability 9
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

.NET Interoperability 12
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