1 / 37

Managed And Native Code Interoperability: Best Practices

TL34. Managed And Native Code Interoperability: Best Practices.  Jesse Kaplan Program Manager Microsoft Corporation. Session Objectives And Takeaways. Session Objectives When is Interop appropriate? Considerations for your interop architecture Choose an Interop Technology

milagro
Download Presentation

Managed And Native Code Interoperability: Best Practices

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TL34 Managed And Native Code Interoperability: Best Practices  Jesse Kaplan Program Manager Microsoft Corporation

  2. Session Objectives And Takeaways • Session Objectives • When is Interop appropriate? • Considerations for your interop architecture • Choose an Interop Technology • Preview of what’s next in interop • Takeaways • Interop success starts with knowing when and how to interoperate, and with choosing the right interop technology

  3. Why This Talk? • What’s already out there • API level docs • Tool descriptions • 700 page tomes • The need: High-level architectural guidance that ties together the technology-oriented guidance we have today

  4. Interop Success Stories • Office • VSTO Plug-in model • Expression Design • MFC/WPF integration • Media Center

  5. Path To Interop Success • Decide if Interop is right for you • Architect your solution carefully • Choose the right Interop technology

  6. Is Interop Right For You? • Interop is for you when you need • To use new managed technologies in your native app • To leverage existing native code in a managed app • Provide a managed extensibility model • Don’t rewrite your application just to enable to managed code • But, if you are going to re-write anyways, you should strongly consider using managed code

  7. Path To Interop Success • Decide if Interop is right for you • Architect your solution carefully • Choose the right Interop technology

  8. Architectural Considerations3 high level points • API Design and Developer Experience • Native-managed transitions and performance • Lifetime management

  9. Interop API DesignDeveloper Experience • Who is going to be coding against your interop layer? • What is their sophistication level? • Do you want to optimize for their experience or the cost of building that layer?

  10. Interop API Design Strategies • Thin Layer • Rich Layer Managed code Interop Native Code Managed code Mgd Object Model Interop Native Code

  11. demo P/Invoke: Developer Experience

  12. Interop Performance Strategies • Location of the interop boundary can have significant effects on performance Managed Application Interop Native Library Chatty or high-bandwidth interface • Abstraction layers can batch calls and aggregate data to reduce boundary crossing Managed Application Interop Native Abstraction Layer Native Library Pushing chatty interface into native layer

  13. Sample: Processing XMLNative App Consuming Managed Library • Native app wants to use System.Xml • System.Xml is a relatively chatty interface • Where do we put the boundary? • First stab • Between the app logic and the managed library • Can we do better? Native App Interop System.Xml Engine XML Proc

  14. Sample: Processing XMLMoving the Interop Boundary • Add a batching layer Native App Interop System.Xml Engine XML Proc Batching Layer • Rewrite XML processing module in managed code Native App Interop System.Xml Engine XML Processor

  15. demo C++ /CLI: Moving theBoundary

  16. Lifetime ManagementCommon issues • Lingering objects • Stubborn GC • Unloading

  17. Office ExtensibilityGoing Beyond PIAs • Originally there were only PIAs • Over time we built up a much richer layer Native Code Managed Code Office Application Interop COM Extensibility Interface PIA Add-In Manager Add-In Managed Wrapper PIA

  18. demo COM Interop: Lifetime Managment

  19. Path To Interop Success • Decide if Interop is right for you • Architect your solution carefully • Choose the right Interop technology

  20. Interop Technologies • P/Invokes • Primarily a managed->native technology • Create an Interop layer that invokes simple C-style APIs • C++/CLI • Create an Interop layer with /clr that bridges native and managed code • COM Interop • Generate an assembly with tlbimp that exposes COM types to managed code • Generate a tlb with tlbexp that exposes managed types to COM

  21. P/Invoke • Wraps C-style functions only • Can be good if • You have few APIs to wrap • APIs are simple: Signatures aren’t complex • May not be good if • APIs use variable length structures, void*, etc. • lots of APIs, to wrap but resources available • http://www.pinvoke.net • Pinvoke Signature tool • Example: The .NET Framework BCL • Wraps Win32’s flat C-style API and exposes it to managed clients

  22. C++/CLI • Designed to be a bridge between managed and native • Can be good if • Static type-checking is needed • Strict performance is a requirement • More predictable finalization is necessary • May not be good if • Other (simpler) Interop technologies meet requirements • Developers are not familiar with C++ programming paradigms

  23. C++/CLIUsing /clr • Don’t use C++/CLI and just recompile your whole codebase with /clr • C++/CLI works best as a bridge • Performance of your code will decrease when compiled with /clr • Programmer productivity will be better in either pure C++ or a pure managed language • Compile exactly the files you need /clrand no more • E.g., your Interop layer written in C++/CLI • Minimizes managed surface exposed to native

  24. Using C++/CLIExample: Processing XML • Native application utilizes C++ classes • Good fit for C++/CLI • If app utilized COM, then CCWs could also be viable • Concerned with performance from chatty interfaces that marshal large amounts of data

  25. COM Interop • Wraps COM interfaces • Can be good if • You already use COM to componentizeyour application • You need full fidelity COM semantics • May not be good if • Your native application does not use COM internally • COM Interop does not meet your performance requirements • Many tools are available to automate the process

  26. COM InteropExample: Microsoft Office • Large Native application that wants managed add-ins • Already utilized COM for extensibility • Performance characteristics of COM interopare acceptable • For most applications Interop performance is not a limiting factor • Overhead of COM Interop is acceptable • You can use different strategies to improve performance

  27. Path To Interop SuccessRecap • Decide if Interop is right for you • Interop can provide the bridge • Interop can help avoid a rewrite • Take the time to architect your solution and consider • Developer Experience • Performance • Lifetime Management • Choose the right interop technology • P\Invoke • C++/CLI • COM

  28. What's Next In InteropMaking things easier and giving you more control • This started with Codeplex and will continue in .NetFx 4.0, Dev10, and beyond • Making things easier without hiding the details • Bridging the gap between the automagic of COM interop and the complete control of C++/CLI

  29. What's New/Next On Codeplex • P\Invoke Signature Generating Tool • Generates managed [DllImport] declarations based on native header files • Generates unmanaged definitions for native delegates • Managed TlbImp • Now: A managed version of the existing tlbimp • Next: Managed tlbimp with new features and extensibility options • Better Diagnostics • Keep your eyes peeled when the betas hit • Or you can just subscribe to our RSS feed…

  30. demo P\Invoke Signature Generator

  31. What's New/Next In The .NetFX 4.0 • Multiple runtimes in the same process • Managed COM components run against the version of the CLR they were built/configured against • No More PIAs • IAs (optionally) move from runtime to design-time • VB and C# compilers embed the portion of the IA an assembly uses in that assembly • The runtime makes these embedded types equivalent between assemblies • Override QueryInterface • ICustomQueriable allows you to take control • Provide your own implementation for IDispatch, IMarshal, or any other COM interface • Aggregate managed COM components from within managed code

  32. Relevant TalksCheck out the Videos… • TL02 Under the Hood: Advances in the .NET Type System • PC49 Microsoft .NET Framework: CLR Futures

  33. For More Information About Specific Technologies • Codeplex Site: http://codeplex.com/clrinterop • P/Invokes • http://www.pinvoke.net • COM Interop • .NET and COM: The Complete Interoperability Guide – Adam Nathan • C++/CLI • http://blogs.msdn.com/vcblog • http://www.marshal-as.net • C++/CLI: The Visual C++ Language for .NET – Gordon Hogenson • C++/CLI in Action – NishantSivakumar • Expert Visual C++/CLI: .NET for Visual C++ Programmers – Marcus Heege

  34. Q & A

  35. Evals & Recordings Please fill out your evaluation for this session at: This session will be available as a recording at: www.microsoftpdc.com

  36. © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

More Related