Presentation is loading. Please wait.

Presentation is loading. Please wait.

DEV394.NET Framework: Migrating To Managed Code Adam Nathan QA Lead Richard Lander Program Manager Microsoft Corporation.

Similar presentations


Presentation on theme: "DEV394.NET Framework: Migrating To Managed Code Adam Nathan QA Lead Richard Lander Program Manager Microsoft Corporation."— Presentation transcript:

1 DEV394.NET Framework: Migrating To Managed Code Adam Nathan QA Lead Richard Lander Program Manager Microsoft Corporation

2 Agenda Adding managed code to unmanaged code C++ Interop COM Interop Adding unmanaged code to managed code PInvoke COM Interop Hosting the CLR Q & A

3 Using C++ Interop Adam Nathan QA Lead Common Language Runtime

4 C++ Interop Extremely flexible Mixing MSIL and unmanaged code Mixing managed and existing C/C++ types Access managed types via #using Access C/C++/COM types via #include User manages “impedance mismatch” (with library support) Data types – System::String vs. BSTR Object lifetime – GC vs. ref counting

5 C++ Marshaling Templates Support conversions between COM Automation Types (BSTR) Standard C++ types (std::string) MFC/ATL types (CString) Extensible by adding new conversions void Func(BSTR bstrName) { String^ name = marshal_to ( bstrName ) ; String^ name = marshal_to ( bstrName ) ;}

6 C++ Templates for COM Interfaces ref class MyType { // Thread agnostic pointer // Thread agnostic pointer com_handle m_ipStorage; com_handle m_ipStorage; MyType(String^ name) { // Thread 1 MyType(String^ name) { // Thread 1 CComPtr ipStorage; CComPtr ipStorage; StgCreateStorageEx(…, (void**)&ipStorage); StgCreateStorageEx(…, (void**)&ipStorage); m_ipStorage = ipStorage; m_ipStorage = ipStorage; } void Stat() { // Thread 2 void Stat() { // Thread 2 m_ipStorage->Stat(…); m_ipStorage->Stat(…); }};

7 Related Talks For More Information DEV331 - Visual C++: Using the.NET Framework in Win32/MFC Applications DEV330 - Visual C++ Under the Covers: Targeting the CLR DEV333 - C++ 2005: The Language of Choice for Native and.NET

8 Agenda Adding managed code to unmanaged code C++ Interop COM Interop Adding unmanaged code to managed code PInvoke COM Interop Hosting the CLR Q & A

9 Exposing A Managed Object Is Easy… 1. Write a managed class 2. Select “Register for COM Interop” option in Visual Studio.NET Or use TlbExp.exe to produce a type library and RegAsm.exe to register the class Or use TlbExp.exe to produce a type library and RegAsm.exe to register the class

10 … But Not Without Limits Not all features of managed objects are exposable to COM Static (shared) methods not exposed Parameterized constructors not exposed Overloaded methods are renamed

11 Windows Media Player Visualization Adam Nathan QA Lead Common Language Runtime

12 What Does COM Interop Do? Every managed object is a COM object Has a class factory Implements IUnknown Implements IDispatch COM callable wrapper (CCW) takes care of all this during run time

13 Agenda Adding managed code to unmanaged code C++ Interop COM Interop Adding unmanaged code to managed code PInvoke COM Interop Hosting the CLR Q & A

14 Help For “Rewriting” Code Visual Basic Upgrade Wizard

15 Platform Invoke (PInvoke) CLR service for using flat APIs Requires manual re-declaration of API’s method signatures No automatic wrapper creation like for COM APIs Similar to Declare statement in Visual Basic 6.0 Language-agnostic

16 Using PInvoke Adam Nathan QA Lead Common Language Runtime

17 www.pinvoke.net A place to find & share PInvoke signatures & types

18 Managed Debugging Assistants Adam Nathan QA Lead Common Language Runtime

19 More MDAs In Whidbey Now cover more than just Interop Modulo Object Hashcode Jit Compilation Start Reflection Creating MemberInfo Cache DllMain Returned False Early-Bound Call On AutoDispatch Class Interface Loader Lock Report AV On COM Release Marshal Cleanup Error Invalid Constrained Execution Region Call Virtual Constrained Execution Region Call Open Generic Constrained Execution Region Call Illegal Prepare Constrained Region SafeHandle Critical Failure …

20 Agenda Adding managed code to unmanaged code C++ Interop COM Interop Adding unmanaged code to managed code PInvoke COM Interop Hosting the CLR Q & A

21 Using COM APIs From Managed Code Adam Nathan QA Lead Common Language Runtime

22 Choosing An Interop Technology C++ Interop Already using C++ Using IDL-Based COM components Using complex or changing flat APIs COM Interop Using TLB-Based COM components COM Interop PInvoke Using simple non-changing flat APIs

23 Agenda Adding managed code to unmanaged code C++ Interop COM Interop Adding unmanaged code to managed code PInvoke COM Interop Hosting the CLR Q & A

24 Hosting The CLR From Unmanaged Code Richard Lander Program Manager Common Language Runtime

25 How It Works Unmanaged host loads CLR From C++ Call CorBindToRuntimeEx() which returns ICorRuntimeHost Configure CLR as needed via interfaces and/or CorBindToRuntimeEx API From VB6 Co-create CorRuntimeHost from VB6 Load managed host into default AppDomain Bulk of hosting logic should be here Load and execute managed code from host This can be your code or random add-ins Host can optionally create separate ADs for isolation of components

26 Benefits And Costs Benefits Provides a great deal of control over CLR Choose CLR version AppDomain usage Garbage Collection mode Loader Optimization Load assemblies via your host Managed code executes natively -- no need for interop registration Assemblies can run against your managed and/or COM object models (via RCWs or PIAs) Great model for hosting managed add-ins and virtual apps Used by ASP.NET, SQL Server and the managed service host in Longhorn Costs Can be complicated

27 Next Steps Evaluate where & how to integrate managed code Try it out, and let us know the results! anathan@microsoft.comrlander@microsoft.com

28 .NET and COM: The Complete.NET and COM: The Complete Interoperability Guide – Adam Nathan Interoperability Guide – Adam Nathan Blogs: Blogs: http://blogs.msdn.com/cbrumme http://blogs.msdn.com/cbrumme http://blogs.msdn.com/adam_nathan http://blogs.msdn.com/adam_nathan http://blogs.msdn.com/suzcook http://blogs.msdn.com/suzcook Newsgroups: Newsgroups: microsoft.public.dotnet.framework.interop microsoft.public.dotnet.framework.interop Web sites: Web sites: http://gotdotnet.com http://gotdotnet.com CLR SPY & pinvoke.net Add-In http://pinvoke.net http://pinvoke.net

29 © 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.


Download ppt "DEV394.NET Framework: Migrating To Managed Code Adam Nathan QA Lead Richard Lander Program Manager Microsoft Corporation."

Similar presentations


Ads by Google