180 likes | 474 Views
Building Windows Runtime Components in C++. Harry Pierson Program Manager, Windows Runtime Experience Team Microsoft Corporation. What is the “Windows Runtime”?. The new API surface area in Windows 8 that’s available to C++, JavaScript, C# and Visual Basic. Windows 8 Platform.
E N D
Building Windows Runtime Components in C++ Harry Pierson Program Manager, Windows Runtime Experience Team Microsoft Corporation
What is the “Windows Runtime”? The new API surface area in Windows 8 that’s available to C++, JavaScript, C# and Visual Basic.
Windows 8 Platform Metro style Apps Desktop Apps HTML JavaScript HTML / CSS XAML View JavaScript (Chakra) C C++ C# VB Model Controller C# VB C C++ WinRT APIs Devices & Printing Communication & Data Graphics & Media System Services .NET / SL Internet Explorer Win32 Application Model Windows Core OS Services Core
What is the “Windows Runtime”? The new API surface area in Windows 8 that’s available to C++, JavaScript, C# and Visual Basic. The low-level system infrastructure that enables language projection of APIs.
You can leverage the Windows Runtime in order to build your own APIs that project into C++, JavaScript, C# and VB.
WinRT Component Authoring Choices • C# / VB • C++/CX • C++ with WRL
C++ Hybrid App Scenarios • HTML/JS Front End • C#/VBFront End • C++ Front End • C++ WinRT Components • C++ WinRT Components • C#/VB WinRT Components
demo Building C++/cx WinRTComponents
WinRT is a marshaling boundary. Favor course-grained APIs over fine-grained.
Marshaling Example: Array vs. Vector //marshals entire array at once void ArraySample(constArray<int>^ ints) { for(auto x = 0u; x < ints->Length; x++) { autovalue = ints[x]; } } //marshals each call to Size and GetAt separately voidVectorSample(IVectorView<int>^ ints) { for(auto x = 0u; x < ints->Size; x++) { autovalue = ints->GetAt(x); } }
WinRT only provides async methods for all potentially long running operations.Favor async methods and use PPL to implement them.
Async Example IAsyncOperation<int>^ CalculateAnswer() { returnconcurrency::create_async([]() -> int { //do long running operation here return42; }); } IAsyncOperation<String^>^ CalculateQuestion() { returncreate_async([]() -> String^ { //do long running operation here returnref new String(L"Whatdo you get if you multiply six by nine?"); }); }
WinRT doesn’t allow UI updates from background threads.Use CoreDispatcher to fire events on the correct thread
Event Firing Example auto callback = [this](Object^ sender,InvokedHandlerArgs^ e) { this->Ticked(this->currentTick); }; //get window via CoreWindow::GetForCurrentThread window->Dispatcher->Invoke(CoreDispatcherPriority::Normal, ref new InvokedHandler(callback), this, nullptr);
Leverage the WinRT Infrastructure of Windows 8Build WinRT Components that can be used in C++, JavaScript, C# & VB apps.Be mindful of threading and marshaling boundaries.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.