Metro Apps Start, Suspend… Apps do not get notified when they are getting terminated User Launches App Splash screen kind launch shareTarget search Metro Style Apps connected App VoIP IM Mail Network Trigger System Trigger Time Trigger Background Task Infrastructure OS Longer Battery Life Always Reachable Apps Recap WinRT APIs for Metro style apps User Interface Devices Communications & Data Media Fundamentals User Interface Devices Communications & Data Media Fundamentals HTTP APIs Web services Data APIs Information APIs Background APIs Socket APIs What is the Windows Runtime? Metro style Apps Kernel System Services Model Controller View XAML C/C++ Desktop Apps HTML / CSS C#, VB JavaScript (Chakra) HTML C C++ C# VB Internet Explorer Win32 .NET SL JavaScript Windows Runtime APIs Communication & Data Graphics & Media Application Model Devices & Printing Windows Kernel Services Metro style app Language Support (CLR, WinJS, CRT) Language Projection Windows Metadata & Namespace UI Pickers Controls Media XAML Storage Network … Windows Runtime Core Windows Core Web Host (HTML, CSS, JavaScript)) Runtime Broker Strings HSTRING Avoids copying in multiple languages Basic Types INT32, UINT64 * Pointers allowed in limited cases Enumerations enum AsyncStatus Flag or non-flag styles Structures struct Rect; Can contain strings, but not interfaces Simple Arrays INT32 [] For very basic collections Interfaces IInspectable Methods are defined in interfaces Generic Interfaces IVector<T> Type-generic interface Runtime Class Windows.Storage. StorageFile Binds interfaces to make a class Shell32.dll Object Activation Store (Registry) Windows Metadata (Disk) Projection C++ App Chakra Projection Windows Metadata CLR Projection Object C#/VB App HTML App Start IInspectable returned Projection creates wrapper (using metadata) App App asks to create object Object created by implementation code Object bound to wrapper Projection Pass Name to RoActivateInstance Find DLL using Catalog Call DllGetActivationFactory Load DLL Wrapper returned to App End WinRT Object Manager WinRT Object Object App Windows Metadata v9 Projection Projection Windows Metadata v8 Object App Collections IVector<T>, IVectorView<T>, IMap<T> Iterators, Collections and events cross-language Delegates delegate AsyncActionCompletedHandler Encapsulate the context to call back to an object Events IApplicationLayout::LayoutChanged Lists of callback recipients PropertySet interface IPropertySet Collection of items with varying types Async Interface ReceivePropertiesOperation A way to get a delayed result without blocking Contracts Windows.ApplicationModel. DataTransferManager Connect Apps to Windows Extension Points C++ App STL-style Projection Array Associative Collection Chakra JavaScript Projection CLR IEnumerable (T) style Projection C#/VB App HTML App App Windows UI Object App Code Windows Object App Code Windows Object App Code Projection Windows Runtime Object Proxy RuntimeBroker.exe App Inside WinRT Extension Catalog Launch Contract Search Contract Class Catalog Extension 1 Runtime Class “A” Extension 2 Runtime Class “B” Extension 3 Runtime Class “C” Extension Registrations • Several OS supported contracts • Extensions are contract implementation • Extensions map the abstract to the concrete Class Registrations • Classes are concrete implementations • Contains information to activate and run code Extension Catalog Deployment Engine Class Catalog Explorer.exe Query Activation System Extension Catalog Investigate Activate Extension Registration Extension Catalog Class Catalog Explorer.exe Activation System RPCSS Activate Application.exe Extension Registration DCOM Launch Class Catalog Application.exe Register MTA main() { ... } Run Core Application Object RPCSS Query Class Catalog Explorer.exe Activation System Complete Activation Activate App Extension Registration RPCSS Application.exe App Code Instance Instance App Callback Core Application Object Application.exe MTA STA #1 Application Code Application Code Run CoreApplication Activate PLM Integration STA #2 ICoreApplication ICoreApplicationExit Private Interfaces Register Run Activation Router ICoreApplicationInitialization Application Code WinRT APIs Foundations Windows Runtime APIs Available to all programming languages JavaScript Requires a language neutral type system Windows Runtime C++ C#/VB 50 .NET parallel programming Overview Dataflow Networks data TransformBlock<byte[],byte[]> Compress compressed TransformBlock<byte[],byte[]> Encrypt and encrypted C++ and XAML Using Windows Runtime from C# C# and Visual Basic influenced the Windows Runtime You can write your own Windows Runtime components in C# or Visual Basic You should build a Windows Runtime component when you want your code to be used from JS, C++, C# and VB Influenced by C# and VB Feels natural and familiar from C# and Visual Basic Build your own managed Windows Runtime components Using Windows Runtime with C++ BINDINGS TO FOREIGN TYPE SYSTEMS Key Bindings Feature Summary 1. Data Types ref class Reference type value class Value type interface class Interface property Property with get/set event “Delegate property” with add/remove/raise delegate Type-safe function pointer generic Type-safe generics gcnew Garbage-collected allocation ref new Reference-counted allocation ^ Strong pointer (“hat” or “handle”) % Strong reference 2. Allocation 3. Pointer & Reference Person^ p; { Person^ p2 = ref new Person(); p2->Name = “John”; p = p2; } p = nullptr; // refcount = 1 // refcount = 1 // refcount = 2 // refcount = 1 // refcount = 0; ~Person() public ref class Person { public: Person(String^ name, String^ email); void Greet(Person^ other); internal: ~Person(); void SetPassword(const std::wstring& passwd); }; Person^ p = ref new Person(“John Surname”); p->Greet(ref new Person(“Jim Surename”); public interface class IAnimal { public interface class IFeline : IAnimal { void Play(); void Scratch(); }; }; IAnimal^ animal = ref new Cat(); animal->Play(); ref class Cat : IFeline { public: virtual void Play(); virtual void Scratch(); }; public ref class DatabaseConnection { public: ~ DatabaseConnection(); }; { DatabaseConnection db(); db.SetName( “Employees”); // … // … lots of queries, updates, etc. … // … } // ~DatabaseConnection() public: property String^ Name; public: property Person^ Sibling { Person^ get() { InitSiblings(); return _sibling; } void set(Person^ value) { _sibling = value; NotifySibling(); } } private: Person^ _sibling; Person^ p = ref new Person(“John”); p->Sibling = ref new Person(p->Name); public delegate void PropertyChanged( String^ propName, String^ propValue ); auto p = ref new PropertyChanged( [](String^ pn, String^ pv) { cout << pn << ” = “ << pv; } ); auto p = ref new PropertyChanged( UIPropertyChanged ); auto p = ref new PropertyChanged( this, MainPage::OnPropertyChanged ); p( “Visible”, false ); public: event PropertyChanged^ OnPropertyChanged; public: event PropertyChanged^ OnNetworkChanged { EventRegistrationToken add(PropertyChanged^); void remove(EventRegistrationToken t); void raise(String^, String^); } person->OnPropertyChanged += propertyChangedDelegate; auto token = person->OnPropertyChanged::add(propertyChangedDelegate); person->OnPropertyChanged -= token; person->OnPropertyChanged::remove(token); • • throw ref new InvalidArgumentException(); throw ref new COMException(E_*); • try { … } catch (OutOfMemoryException^ ex) { … } ex->HResult • catch (Platform::Exception^) HRESULT Exception E_OUTOFMEMORY OutOfMemoryException E_INVALIDARG InvalidArgumentException E_NOINTERFACE InvalidCastException E_POINTER NullReferenceException E_NOTIMPL NotImplementedException E_ACCESSDENIED AccessDeniedException E_FAIL FailureException E_BOUNDS OutOfBoundsException E_CHANGED_STATE ChangedStateException REGDB_E_CLASSNOTREG ClassNotRegisteredException E_DISCONNECTED DisconnectedException E_ABORT OperationCanceledException generic<typename T, typename U> public interface class IPair { property T First; property U Second; }; IPair<String^, Uri^>^ uri = GetUri(); auto first = uri->First; // type is String^ auto second = uri->Second; // type is Uri^ ref class PairStringUri: IPair<String^, Uri^> { public: property String^ First; property Uri^ Second; }; template<typename T, typename U> ref class Pair: IPair<T, U> { public: property T First; property U Second; Pair(T first, U second) { First = first; Second = second; } }; IPair<String^, Uri^>^ pair = ref new Pair<String^, Uri^>( “//BUILD/”, ref new Uri(“http://www.buildwindows.com”)); • #using <Company.Component.winmd> public private partial ref class MainPage: UserControl, IComponentConnector { public: void InitializeComponent(); void Connect() { btn1->Click += ref new EventHandler(this, &MainPage::Button_Click); } }; ref class MainPage { public: MainPage() { InitializeComponent(); } void Button_Click(Object^ sender, RoutedEventArgs^ e); }; Libraries using namespace Platform; Vector<String^>^ items = ref new Vector<String^>(); items->Append(“Hello”); • Returning a read-only view of the vector IVectorView<String^>^ GetItems () { return items->GetView(); } items->VectorChanged += ref new VectorChangedEventHandler<String^> (this, &MyClass::VectorChanged); using namespace Platform; Map<String^, Uri^> favorites = ref new Map<String^, Uri^>(); favorites->Insert(“MSDN”, ref new Uri(“http://msdn.com”)); if (favorites->HasKey(“MSDN”)) favorites->Remove(“MSDN”); Begin()/End() begin() end() IVector<int>^ v = GetItems(); int sum = 0; std::for_each( begin(v), end(v), [&sum](int element) { sum += element; } ); std::vector<int> v; v.push_back(10); auto items = ref new Vector<int>(v); Vector<int>^ items = …; std::vector<int> v = to_vector(items); WinRT and JavaScript Basics // C# namespace CustomWinRTComponent { public interface IMoreMath { } public sealed class MoreMath : IMoreMath { public static double Sinh(double x) { return Math.Sinh(x); } } } // JavaScript var MoreMath = CustomWinRTComponent.MoreMath; var y = MoreMath.sinh(0.7); C++ and Metro Style Apps HRESULT __stdcall Add( Calculator* this, int i, int j, int* result ) try { int i = Add(41, 1); } catch (OverflowException^ e) { printf("Too big"); } inline int Add(int i, int j) { int res; HRESULT hr = __cli_Add(this, i, j, &res); if (hr != 0) { __throw_hr_as_exception(hr); // switch on hr & throw } return res; }; int Add(int i, int j) { if (i+j < i) { throw OverflowException(); } return i + j; } HRESULT __stdcall __cli_Add(Calculator* calc, int i, int j, int* r) { try { *r = calc->Add(i, j); } catch (Exception^ e) { return e->HResult; } return S_OK; }; H^T “It’s not okay to call me a carrot” Calculator::ICalculator vtable ref class Calculator IStorageCell vtable * vfptr1 ICalculator^ calc ICalculator vtable * vfptr2 int _storage int _refcount &Calculator::QueryInterface &Calculator::AddRef &Calculator::Release &Calculator::GetIids &Calculator::GetRuntimeClassName &Calculator::GetTrustLevel &Calculator::Add H^T “It’s not okay to call me a carrot” Brings existing C++ code into Metro Style Apps Metro style apps Core System Services Model Controller View XAML C C++ Desktop apps HTML / CSS C# VB JavaScript (Chakra) HTML C C++ C# VB Internet Explorer Win32 .NET / SL JavaScript WinRT APIs Communication & Data Graphics & Media Application Model Devices & Printing Windows Core OS Services http://go.microsoft.com/fwlink/?LinkId=228532 Microsoft Libraries Desktop apps Metro style apps Standard C++ Libs good ppl (parallel patterns lib) good Win32 WinRT + Win32 subset ATL ATL subset MFC no 3rd party libs (boost, etc.) depends your code depends // c:\Program Files (x86)\Windows Kits\8.0\Include\um\mmdeviceapi.h // Metro style SDK APIs // Desktop SDK APIs, not available for Metro style apps .NET Kernel Entity Framework ASP. NET Base Class Libraries JIT & NGEN Garbage Collector WCF WPF Win Forms The CLR Security Model Work Flow And more! Profiling& Debugging APIs Exception Handling Loader & Binder Improvements to the Garbage Collector Generation 1 • • • • Generation 0 New objects allocated as Generation 0 Accessible References Keep Objects Alive GC Compacts Referenced Objects Objects Promoted to Older Generation Small Object Heap Large Object Heap Client GC One One Server GC One per logical processor One per logical processor Gen0/Gen1 Gen2 Client GC Always blocking Can be non-blocking Server GC Always blocking Can be non-blocking LEARN MORE http://blogs.msdn.com/b/clrteam/ “Enhancements to the CLR GC in .NET 4.5” C# Next C# 5.0 + VB 11.0 C# 4.0 + VB 10.0 Windows Runtime + Asynchrony Dynamic + Language Parity C# 3.0 + VB 9.0 Language Integrated Query C# 2.0 + VB 8.0 Generics C# 1.0 + VB 7.0 Managed Code Class Meta-programming public Language Object Model Read-Eval-Print Loop Foo Field private DSL Embedding X string Source File code Source Source code Compiler .NET Assembly Source code Source code Language Service Compiler APIs Compiler Pipeline Metadata Import Quick Recap • WinRT • Visual C++ Component Extensions Windows Core OS Services HRESULT E_OUTOFMEMORY E_INVALIDARG E_NOINTERFACE E_POINTER E_NOTIMPL E_ACCESSDENIED E_FAIL E_BOUNDS E_CHANGED_STATE REGDB_E_CLASSNOTREG E_DISCONNECTED E_ABORT Exception OutOfMemoryException InvalidArgumentException InvalidCastException NullReferenceException NotImplementedException AccessDeniedException FailureException OutOfBoundsException ChangedStateException ClassNotRegisteredException DisconnectedException OperationCanceledException Collections IIterable IIterator • STL containers are the backing store Async pattern http://go.microsoft.com/fwlink/?LinkId=228286 Win32 • • • • COM Core Kernel Accessibility Audio • • • • • Direct2D Direct3D DirectComposition DirectManipulation DirectWrite • • • • File Systems Globalization Media Foundation Windows and Messages C++ renaissance Visual C++: The power and performance tool for Windows. Recap Related sessions • • • • • PLAT-874T - Lap around the Windows Runtime APP-409T Fundamentals of Metro style apps: how and when your app will run TOOL-531T Using the Windows Runtime from C# and Visual Basic TOOL-532T Using the Windows Runtime from C++ TOOL-533T Using the Windows Runtime from JavaScript Documentation & articles • C++ Language extension summary • The Windows Runtime • Windows Runtime Design