Title: Interoperating With Existing Code
1InteroperatingWith Existing Code
- Peter Ty
- MCDBA, MCSE, MCPSBTechnology Specialist
- Microsoft Hong Kong Ltd.
2Common Language Runtime Type System
- Compilers use the runtime type system to produce
type compatible components
Runtime Components
PlatformCompilers
C
VB
C
Runtime Type System
3Interoperability Services
Type SystemStandard
BinaryStandard
Runtime Execution Sys
COM Binary Std
C
VB
MFC/ATL
MSVCRT
C
Runtime Type Sys
4Why Interop
- Preserve/Utilize your investment
- No need to start over
- Continue to use exist code
- Incremental migration path
- Migrate your application step by step
- Reality some things never change
- Need to interop with code that cant change
5Interop
- COM/DLL
- Binary standard
- Type Libraries
- Immutable
- Type unsafe
- Interface based
- HResults
- Guids
- .NET Framework
- Type Standard
- Assemblies
- Resilient Bind
- Type Safe
- Class and Interface based
- Exceptions
- Strong Names
6Model Consistency
- Programming model remains consistent
- COM developers use COM model
- .NET Framework developers use .NET Framework
model - Model Transparency
- .NET Framework
- new operator
- Cast operator
- Memory mgmt
- Exceptions
- Static Methods
- COM/DLL Model
- CoCreateInstance
- QueryInterface
- Reference Counting
- Hresults
- GetProcAddress
7Interop Services
- COM Interop
- Use COM components from the runtime
- Use .NET Framework components from COM
- Platform Invoke
- Call static Entry Points in unmanaged DLLs
- Pass function pointers to unmanaged code for
callback
8COM Interoperability
9COM Interop
- Provides a bridge between .NET Framework and COM
and vice versa - Maintains programming model consistency on both
sides - Abstracts the inconsistencies between the two
models - Different data types
- Method signatures
- Exception/Hresults
- Interop is for backward compatibility, build .NET
clients for real simplicity
10Bi-Directional COM Interop
- .Net to COM
- Allows .Net types to access COM types
- COM to .Net
- Allows COM types to access .Net types
.Net
COM
COM
.Net
11.Net To COM Interop
Common Language Runtime
COM Server
Object
Runtime CallableWrapper
ReferenceCounted
Client
Traced Reference
12Using COM Types From The .NET Framework
- Create type definitions for COM types
- By adding references in VS
- By using type library importer (tlbimp)
- By defining types manually
- Use the types as managed types
- Create instance with new
- Cast between interfaces
- Extend them
- No reference counting
- Catch Exceptions
13Helpful Information
- Dont think of the types as COM types
- Catch exceptions
- Use reflection
- Use delegates based events
- COM types must be registered (of course)
- Assembly with COM type definitions must be
resolvable at runtime - Late bind to COM types using reflection
- Threading model must be compatible
- Application must have full trust in order to use
COM types
14Under the Covers
- The runtime wraps all COM objects with a Runtime
Callable Wrapper (RCW) - The wrapper handles
- Reference counting
- Signature conversion
- Object identity
- Exception mapping
- More
- Runtime marshals calls between the wrapper and
the COM object
15Using COM Types From .NET Framework Code Walk
16COM To .Net Interop
Traced Reference
Common Language Runtime
COM Client
COM CallableWrapper
IFoo
Object
ReferenceCounted
Client
17Using .NET Framework Types From COM
- Create manage type in the usual way
- Only public are exposed to com
- Classes must have default constructor to be
creatable - Use TlbExp to create COM type library
- Register the assembly with RegAsm
- Only needed for interop
- Reference the typelib as necessary
- Use the types as normal COM types
- Create instance with CoCreateInstance
- Use QueryInterface to type coercion
- Use AddRef/Release to reference count
- Examine Hresults on return
18Helpful Information
- Guids generated automatically at export time.
- Based on assembly and type name
- Based on complete interface definition
- Typelib file may be needed at runtime
- Runtime will produce one automatically
- Prepackage along with assembly
- Assembly must be resolvable at runtime to
activate from COM - Installed in application directory or
- Installed in global assembly store
19Under The Covers
- The runtime wraps all .NET Framework objects with
a COM Callable Wrapper (CCW) - The wrapper implements
- IUnknown
- IDispatch
- ITypeInfo
- IProvideClassInfo
- IConnectionPoint, etc.
- The runtime marshals calls between the COM client
and the wrapper - Wrapper also manages identity, handles
exceptions, signature conversion, etc
CCW
.Net Class
20Using .NET Types From COM Code Walk Thru
21Building COM Compatible Types
.NET Framework
COM
IFooBaz
IFooBaz
CCW
22Building COM Compatible Types
- COM Compatible types provide compatible
implementations of existing COM interfaces - Interfaces must have compatible layout, IID,
DispID, method signature - Start by importing interface definitions with
TlbImp - Implement necessary interfaces, export with
TlbExp and register with RegAsm
23Signature TranslationPerformed by TlbImp/TlbExp
- .NET Framework Method Signature
- int FormatDate(String s, DateTime d)
- HRESULT FormatDate(BSTR s, DATE d, out, retval
int retval) - COM Method Signature
TlbImp
TlbExp
24Custom Attributes
Custom Attributes can be applied to types,
methods, properties, fields or parameters to
effect the COM type definitions produced by TlbExp
- Using System.Runtime.InteropServices
- Guid(), InterfaceType(ComInterfaceType.IsIUnknow
n) - Interface IFooBar
- DispId(64) int Format( MarshalAs(LPSTR)
String s)
Custom Attributes are also used for other
purposes
25Platform Invoke (P/Invoke)
- Provides access to static entry points in
unmanaged DLLs - Similar to
- VB Declare statement
- Load library / GetProcAddress
- Requires method definition with custom attribute
- Uses same underlying marshaling service as COM
Interop
26(No Transcript)