Best Practices for Interop with Visual C++ Boris Jabes Program Manager Microsoft Corporation.

Slides:



Advertisements
Similar presentations
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Advertisements

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Identity Management - Login © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Feature: Reprint Outstanding Transactions Report © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Feature: Purchase Requisitions - Requester © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
demo Default WANGPSLookup Default WANGPS.
Feature: Payroll and HR Enhancements © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Co- location Mass Market Managed Hosting ISV Hosting.
Windows 7 Training Microsoft Confidential. Windows ® 7 Compatibility Version Checking.
Multitenant Model Request/Response General Model.
Feature: Purchase Order Prepayments II © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: OLE Notes Migration Utility
Feature: Web Client Keyboard Shortcuts © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
Feature: SmartList Usability Enhancements © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
Session 1.
Built by Developers for Developers…. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
 Rico Mariani Architect Microsoft Corporation.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Assign an Item to Multiple Sites © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
WinHEC /22/2017 © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Print Remaining Documents © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Connect with life Connect with life
NEXT: Overview – Sharing skills & code.
demo Receive Inventory Export Parse and Normalize.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Document Attachment –Replace OLE Notes © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Feature: Suggested Item Enhancements – Sales Script and Additional Information © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows.
Feature: Customer Combiner and Modifier © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
Feature: Employee Self Service Timecard Entry © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
announcing Dev Manager Do I understand what we’ve built? Developer Can I bet on using this shared component? Testers What’s changed since I last.
Ian Ellison-Taylor General Manager Microsoft Corporation PC27.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
demo Instance AInstance B Read “7” Write “8”

customer.
demo © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
demo Demo.
Feature: Void Historical/Open Transaction Updates © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
demo QueryForeign KeyInstance /sm:body()/x:Order/x:Delivery/y:TrackingId1Z
Feature: Suggested Item Enhancements – Analysis and Assignment © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and.
projekt202 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
The CLR CoreCLRCoreCLR © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks.
 Boris Jabes Program Manager Lead Microsoft Corporation TL13.
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Sr. Dir. – Systems Architecture Inlet Technologies.

IoCompleteRequest (Irp);... p = NULL; …f(p);
Ctrl-K, X Ctrl-K, S
Возможности Excel 2010, о которых следует знать
Title of Presentation 11/22/2018 3:34 PM
Office Mac /30/2018 © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Title of Presentation 12/2/2018 3:48 PM
1/3/2019 1:21 PM © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
8/04/2019 9:13 PM © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
PENSACOLA ENERGY WORK PLAN OCTOBER 10, 2016
Title of Presentation 5/12/ :53 PM
Шитманов Дархан Қаражанұлы Тарих пәнінің
Title of Presentation 5/24/2019 1:26 PM
日本初公開!? Vista の新機能を実演 とっちゃん わんくま同盟 7/23/2019 9:09 AM
Title of Presentation 7/24/2019 8:53 PM
Presentation transcript:

Best Practices for Interop with Visual C++ Boris Jabes Program Manager Microsoft Corporation

Overview What is Interop? Crossing the native (x86) and managed (IL) boundary Multiple ways to do it COM Interop, P/Invoke, C++ techniques This talk will discuss all 3 techniques and both “directions” Calling native code from managed code Calling managed code from native code Focus on C++

Calling native code via COM Wrap existing objects into COM components Can be difficult if unfamiliar with C++ / COM code Need to deal with lifetime management, identity management, error handling, correct apartment and threading transitions

Calling native code via P/Invoke Easy way to call C-style flat APIs from managed code Common candidate is Win32 API It can be pretty clunky Imagine not having pinvoke.net!

C++ Interop techniques Once again, there are many ways to do it 1. Compiling component with /clr 2. Writing a wrapper class 3. Calling native code directly from C++/CLI (aka IJW) 3 flavors of /clr /clr – produces mixed native/managed binary /clr:pure – produces a pure (not safe or verifiable) MSIL binary from C++/CLI. Fully supports ISO C++ /clr:safe – produces a verifiably safe MSIL binary (no pointers…)

Internal Adapter Method Problem How can managed implementations make use of native types without building additional managed abstractions? Important Reduce marshalling overhead for chatty methods Maximize reuse of native domain models Solution Internal methods with native types as parameters and return values Avoid per call marshalling cost Managed implementation can use native types directly

#include “NativeType.h” public ref class PublicType { NativeType* _NativePtr; public: void PublicMethod() { _NativePtr->Method(); } internal: void SetNativeType(NativeType* nativePtr); }; Holding a native object in a ref type

Native P-Impl in Managed Type Problem How can a managed API delegate implementation to an existing native C++ implementation? Important Maximize reuse of native implementation Leverage native performance features Solution Use smart pointer to manage lifetime Delegate as Function Pointer for callback

#include using Marshal; typedef void (__stdcall *CBPTR)(int); delegate void Callback ( int val ); ref class M { NativeType* _Impl; Callback^ _Cback; public: M() : _Impl(new NativeType()) { _Cback = gcnew Callback(this,&M::F); _Impl.SetCallback((CPBTR) GetFunctionPointerForDelegate(_Cback).ToPointer()); } private: void F( int val ); }; Using a managed function to respond to a native callback

Calling managed code from native C++ Visual C++ emphasizes keeping your existing code and incrementally adding managed functionality Your Application Now Built on top of native libraries Native Libraries Managed Libraries New Application with Added Functionality Compile Pieces with /clr 11

Using managed libraries in native code Simplest /clr on entire project Add reference to desired dlls (also works for COM) More granular approach /clr on the files necessary only #using in order to import library A source file can make use of several kinds of libraries in a similar fashion #using //.NET library #import // COM type library #include // Standard C++ library

Performance It’s all about reducing transitions P/Invoke vs. C++ Interop C++ is faster by default

Common Mistakes I Marshalling Blittable types do not require special handling Try to make marshalling generic and static (marshal ) Watch out for wide vs. narrow strings Too many transitions Wrappers should reduce chattiness Every transition has a cost, moving the IL boundary lower can improve performance

Common Mistakes II Forgetting to manage handles and pointers in wrapper code Use auto_gcroot Use auto_gcroot Write a native equivalent /clr on everything DON’T DO IT!!! #pragma (un)managed! E.g. good way to reduce transition in tight loops

Additional References Upcoming book: C++/CLI in action by Nishant Sivakumar.NET and COM: The Complete Interoperability Guide by Adam Nathan

© 2006 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.