Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microsoft.NET Framework Interop Brian Long Master Consultant Falafel Software.

Similar presentations


Presentation on theme: "Microsoft.NET Framework Interop Brian Long Master Consultant Falafel Software."— Presentation transcript:

1 Microsoft.NET Framework Interop Brian Long Master Consultant Falafel Software

2 .NET Interoperability Why Interoperability? The.NET platform is new The Win32 platform is well established No one wants to start from scratch Use of existing code in.NET applications is essential Interoperability goes both ways

3 .NET clients can use: Win32 COM server objects (RCW) Win32 DLL exports (P/Invoke) Win32 COM clients can use:.NET objects (CCW) Win32 clients can use:.NET method exports (Inverse P/Invoke) Interoperability Options

4 COM is dead!!! COM code equals legacy code If you don’t know COM, don’t start learning it now Er,.... That’s not practical There is a massive investment in COM that we still need to use COM

5 COM ↔.NET Interoperability is usually called Com Interop COM/Win32 ↔.NET requires marshaling of parameters COM Interop requires some reconciliation of COM reference counting and.NET GC mechanisms Interoperability requires some proxy / thunk / wrapper to be in place (automated) General Points

6 RCW – Runtime Callable Wrappers:.NET wrapper around COM object Type library importer (TlbImp.exe) generates an Interop Assembly Delphi 8 & “Diamondback” IDEs do it just as well Interop Assemblies have a common naming convention: Interop.LibraryName.dll (LibraryName is the type library name, not the COM server name).NET → COM (RCW)

7 Let’s make an Interop Assembly.NET → COM (RCW)

8 Use Primary Interop Assembly if available Primary Interop Assemblies are provided and signed by the COM component’s creator E.g. adodb.dll for MDAC objects Microsoft Office XP Primary Interop Assemblies available from MSDN web site.NET → COM (RCW)

9 RCW manages COM object reference count COM object is released during RCW garbage collection RCW turns HRESULTs into.NET exceptions.NET → COM (RCW)

10 A coclass Foo becomes an RCW FooClass Interfaces keep the same name An additional interface Foo is generated, combining the coclass’ default interface and a helper interface for the default event interface An event method Bar from an event interface IEvents gets turned into a delegate type IEvents_BarEventHandler. These COM events can be hooked up like normal.NET events.NET → COM (RCW)

11 Early binding: Straightforward - get an interface reference from the construction of the RCW Call methods or access properties Exposed events can be set up just like normal.NET events.NET → COM (RCW)

12 Let’s see some RCW early binding.NET → COM (RCW)

13 If the type library importer does not provide appropriate parameter type marshaling you can tweak it using creative round tripping Little other choice exists.NET → COM (RCW)

14 Late binding: This is possible without the Interop Assembly Uses reflection to operate New instance ( CreateOleObject ) through: –System.Type.GetTypeFromProgID –Activator.CreateInstance Current instance ( GetActiveOleObject ) through: –Marshal.GetActiveObject Note System.Reflection.Missing and System.Type.Missing (for EmptyParam ).NET → COM (RCW)

15 Late binding: Methods invoked through –System.Type.InvokeMember Parameters passed in an object array.NET → COM (RCW)

16 Let’s see some RCW late binding.NET → COM (RCW)

17 Late binding: Reference parameters are fiddly Overloaded InvokeMember requires single element array of ParameterModifier ParameterModifier is an array of Boolean flags Flag is True for reference parameter Flag is False for value parameter.NET → COM (RCW)

18 Let’s see some more RCW late binding.NET → COM (RCW)

19 Let’s see RCW early binding with events.NET → COM (RCW)

20 CCW – COM Callable Wrappers: COM wrapper around.NET object Assembly registration utility (RegAsm.exe) COM →.NET (CCW)

21 CCW ensures it will be marked for garbage collection when external reference count reaches 0 CCW turns.NET exceptions into HRESULTs Assembly must be accessible to CLR: –installed in GAC –resident in application directory (or available for probing) COM →.NET (CCW)

22 Late binding simply requires the assembly to be registered Late binding uses a ProgID registered by RegAsm.exe: AssemblyName.ClassName (e.g. MyAssembly.MyClass) COM →.NET (CCW)

23 Let’s see some CCW late binding COM →.NET (CCW)

24 Early binding relies on an Interop Type Library: –use the /tlb option with RegAsm –use the import wizard in “Diamondback”.NET objects may choose to implement a defined interface or not The Guid attribute can be used to give a.NET interface an IID (traditional Delphi syntax should also work*) *And does in “Diamondback”, but not in Delphi 8 COM →.NET (CCW)

25 The ClassInterface attribute controls whether and how an interface will be manufactured to expose the class: –AutoDispatch - dispinterface for late binding (the default) –AutoDual – for early binding (versioning issues) interface is class name with _ prefix –None – IDispatch access only Use AutoDual if you have no interface Use None if you implement an interface (the suggested approach to avoid interface versioning issues) COM →.NET (CCW)

26 Importing a Delphi assembly’s Interop Type Library requires some forethought, due to the symbols exposed by default Use [assembly: ComVisible(False)] and [ComVisible(True)] to control default visibility Early binding from Win32 uses the creator class in the type library import unit, as usual, or any of the other standard options COM →.NET (CCW)

27 Let’s see some CCW early binding COM →.NET (CCW)

28 .NET → Win32 (P/Invoke) Platform Invocation Service, usually referred to as Platform Invoke, or simply P/Invoke (or even PInvoke): DllImport attribute (from System.Runtime.InteropServices) is needed for routines with text parameters Standard Delphi DLL import syntax works otherwise Uses DllImport behind the scenes Caveat is string parameters

29 .NET → Win32 (P/Invoke) Let’s see a traditional import

30 .NET → Win32 (P/Invoke) //Win32 procedure FooA(Msg: PChar); cdecl; begin MessageBox(0, Msg, 'Foo', MB_OK or MB_ICONQUESTION); end; //.NET procedure Foo(const Msg: String);... [DllImport('bar.dll', EntryPoint = 'FooA', CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] procedure Foo(const Msg: String); external;

31 .NET → Win32 (P/Invoke) The big issue with P/Invoke is ensuring the parameters are marshaled across correctly. String parameters are generally catered for with DllImport.CharSet: –Ansi –None –Unicode –Auto * * uses Ansi on Win9x and Unicode on NT platforms

32 .NET → Win32 (P/Invoke) Let’s see some P/Invoke imports

33 .NET → Win32 (P/Invoke) Use Windows.pas and Delphi.Vcl.Windows.pas as guidelines for parameter type translation MarshalAs parameter attribute from System.Runtime.InteropServices Used to fix parameter marshaling when the default marshaling is inappropriate

34 .NET → Win32 (P/Invoke) Let’s see P/Invoke imports in use

35 .NET → Win32 (P/Invoke) Other issues surround Win32 error codes: –DllImport.SetLastError –Marshal.GetLastWin32Error –GetLastError HResult values: –safecall (Win32 COM) –DllImport.PreserveSig (.NET)

36 .NET → Win32 (P/Invoke) Let’s use P/Invoke attribute fields

37 .NET → Win32 (P/Invoke) Performance: P/Invoke calls cost ~10 machine instructions Cost rises for each extra job (marshaling etc.) By default security is on UnmanagedCode permission SuppressUnmanagedCodeSecurity attribute omits security check stack walk

38 .NET → Win32 (P/Invoke) Let’s see more P/Invoke code

39 .NET → Win32 (P/Invoke) New in Delphi “Diamondback” Virtual Library Interfaces (VLI) aka Dynamic PInvoke Makes a set of functions implemented in a DLL look like an interface implemented by an object Uses new overload of Supports

40 .NET → Win32 (P/Invoke) Let’s see some VLI

41 Win32 →.NET methods Little known mechanism (Inverse P/Invoke), primarily discussed in: –Inside Microsoft.NET IL Assembler, Serge Lidin, Microsoft Press Uses method transition thunks Only supported by Managed C++ and IL Oh, and Delphi for.NET

42 Win32 →.NET methods Very trivial mechanism in Delphi: managed exports Simply use an exports clause as you do in Win32 when exporting functions from DLLs Caveats: –Must mark the project source as containing unsafe code: {$UNSAFECODE ON} –Can only export “global” routines –Can not export static class methods this way

43 Win32 →.NET methods Can also be accomplished in other languages Much more involved (as indeed it is when exposing Delphi static class methods) Involves creative round-tripping to expose assembly methods

44 Win32 →.NET methods Round-tripping: Disassemble a compiled assembly to an IL source file with the.NET disassembler: ildasm.exe Modify the IL code, or add additional IL files, possibly to include features not supported by the original compiler Reassemble the IL code with the.NET assembler: ilasm.exe

45 Win32 →.NET methods Creative round-tripping: Disassemble a compiled assembly to an IL source file with the.NET disassembler: ildasm.exe Modify the IL code, or add additional IL files, possibly to include features not supported by the original compiler Reassemble the IL code with the.NET assembler: ilasm.exe

46 Win32 →.NET methods Let’s see some round tripping

47 Win32 →.NET methods IL modifications to export.NET methods: Modify IL manifest: –Modify.corflags directive to cater for XP issue –Declare a v-table fixup table –Declare data space for the v-table fixup table Modify implementations of methods to be exported: –Mark each method with the.vtentry and.export directives

48 Win32 →.NET methods IL file assembly manifest (original):.module dotNetAssembly.dll.imagebase 0x00400000.subsystem 0x00000002.file alignment 512.corflags 0x00000001

49 Win32 →.NET methods IL file assembly manifest (modified):.module dotNetAssembly.dll.imagebase 0x00400000.subsystem 0x00000002.file alignment 512.corflags 0x00000002.data VT_01 = int32[2].vtfixup [2] int32 fromunmanaged at VT_01

50 Win32 →.NET methods Two IL methods (original):.method public static void DoSomething( int32 I) cil managed {.maxstack 1 IL_0000: ldarg.0 // rest of code omitted for brevity } // end of method Unit::DoSomething.method public static void DoSomethingElse( [in] string Msg) cil managed {.maxstack 1 IL_0000: ldarg.0 // rest of code omitted for brevity } // end of method Unit::DoSomethingElse

51 Win32 →.NET methods Two IL methods (exported):.method public static void DoSomething( int32 I) cil managed {.vtentry 1:1.export [1] as DoSomething.maxstack 1 IL_0000: ldarg.0 // rest of code omitted for brevity } // end of method Unit::DoSomething.method public static void DoSomethingElse( [in] string Msg) cil managed {.vtentry 1:2.export [2] as DoSomethingElse.maxstack 1 IL_0000: ldarg.0 // rest of code omitted for brevity } // end of method Unit::DoSomethingElse

52 Win32 →.NET methods Let’s see some creative round tripping

53 Win32 →.NET methods Potential maintenance issue: must modify IL generated from disassembling every built executable Workaround is a utility to automate the process (perhaps a command-line utility) One such utility (almost) is mme.exe (Managed Method Exporter) * mme.exe is actually a simple GUI app * supplied with source in the files that accompany this session

54 Everything you ever wanted to know about COM Interop (and lots you didn’t): References

55 The book to have in order to learn about Delphi 8 and the Microsoft.NET Framework: References

56 Full coverage of CIL (or MSIL) by the author of ILASM, ILDASM & the CLR Metadata validation engine

57 Questions?

58 Thank You 3222 Microsoft.NET Framework Interop Please fill out the speaker evaluation You can contact me further at brian@falafel.com


Download ppt "Microsoft.NET Framework Interop Brian Long Master Consultant Falafel Software."

Similar presentations


Ads by Google