Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Deep Dive into WinRT

Deep Dive into WinRT - discover how the Windows Runtime is based on COM, how asynchronous operations work, how language projections enable access from a variety of languages, and what performance considerations are relevant for interoperability.

  • Be the first to comment

Deep Dive into WinRT

  1. 1. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  2. 2. Introduction to Windows 8 COM Refreshment Windows Runtime Objects and API Broker Process WinRT Types, Threading Model, Asynchronous Programming Metadata, Language Projections, and WinRT Components Interoperability with WinRTSome slides taken from Microsoftpresentations, //build/ 2011
  3. 3. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  4. 4. Full application compatibility with Windows 7 New immersive UI for slate form factors and touchNew application framework for immersive apps
  5. 5. Windows 8• “Standard” x86/x64 edition, including tablets Windows 8 Pro• Domain connectivity, encryption, Hyper-V Windows 8 Enterprise• Available only through volume licensing Windows 8 RT [Windows on ARM]• ARM only, mostly tablets, OEM installs
  6. 6. Windows 7 Developer ConsumerRTM Preview Preview• September 2009 • //build, September • February 2012 Release 2011 RTM, GA Candidate
  7. 7. Metro style Apps Desktop Apps XAML HTML / CSS JavaScript C/C++ C#, VB HTML C C# (Chakra) JavaScript C++ VB Windows Runtime APIsCommunication Devices & Graphics & Media & Data Printing Application Model Internet .NET Explorer Win32 SL Windows Kernel Services
  8. 8. …are …are …can’t …loosely …can use a …are thedistributed subject to access coupled to restricted only ones through strict sensitive other apps subset of to run on the sandbox resources and the Windows Windows 8 Windows restrictions without system and .NET RT Store permission APIs s
  9. 9. Myth Reality WinRT uses COM referenceWinRT objects are subject to counting; managed Metro appsgarbage collection use GC for managed objects.NET Metro apps are compiled to .NET Metro apps use a subset ofnative code and don’t require the .NET Framework and rely onthe CLR or the .NET Framework the CLR and JIT to runThere’s a separate CLR for .NET The desktop CLR and the MetroMetro apps CLR are one and the same
  10. 10. User InterfaceDevices Communications & DataMedia Fundamentals
  11. 11. App gets 5 seconds to App is not notified handle suspend before termination Apps are notified whenthey have been resumed
  12. 12. Push Audio Background Backgroundnotifications playback transfer tasks Download files Run in a separate Update live tile from a remote process server Play music in the background Triggered by Create Upload files to a time, push, netw badges, multiple remote server ork, system tiles events
  13. 13. AppContainer – Signed + Validated Filtered to declared capabilities in the package manifest
  14. 14. File access • Music, pictures, videos, documents, removable capabilities storage Device access • Webcam, microphone, location, SMS, proximity capabilities Network • Internet client, Internet client/server, private capabilities network client/serverPrivate storage, simple sensors, settings, and muchmore available without any declaration
  15. 15. <Package> ... <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="HelloWorldMetroApp.App"> ... </Application> </Applications> <Capabilities> <Capability Name="internetClient" /> <DeviceCapability Name="location" /> <DeviceCapability Name="microphone" /> <DeviceCapability Name="webcam" /> </Capabilities></Package>
  16. 16. • Packages are compressed and signed, including manifest Files / Assets• Block map allows partial downloads when you issue an update AppXManifest.xml BlockMap Signature Zip Central Directory .appx package
  17. 17. • Windows App Certification Kit helps ensure you pass Store certification steps – UI application and command-line tool• Certification steps: Package Use of Launch and No crashes or manifest supported suspend Security tests hangs during compliance APIs only performance the run
  18. 18. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  19. 19. • COM (Component Object Model) interfaces is a binary standard for authoring components IUnknown – Specifies how classes implement IStream interfaces, how interfaces are discovered, and how methods are dispatched at runtime• Most Windows languages can consume and create COM objects – C++, C#, VB6, Delphi, VBScript, …
  20. 20. • Coclasses and interfaces have IDs (GUIDs) – Can also have human-readable ProgIDs• COM objects are registered in the registry – HKEY_CLASSES_ROOTCLSID, point to DLL location• Metadata provided in type libraries (.tlb files)• The COM binary exposes entry points for activation – DllGetClassFactory  IClassFactory  CreateInstance – Activation can be in-process, out-of-process, or remote (DCOM through port 135)
  21. 21. COM objects are reference-counted Every operation that duplicates a reference must call AddRef Every operation that removes a reference must call ReleaseIMyInterface* instance;HRESULT hr = CoCreateInstance(CLSID_MyClass, NULL, CLSCTX_INPROC_SERVER, IID_IMyInterface, (void**)&instance);instance->AddRef(); COM ObjectIMyInterface* anotherReference = instance;instance->Release(); RCanotherReference->Release();
  22. 22. COM objects can be consumed from .NET by using their type library to generate an interop assembly Or add a reference to a vendor-supplied PIAtlbimp.exe ComLibrary.tlb /out:ComLibraryInterop.dll Using COM objects is then very similar to managed classes—new it up, and you’re good to go Lifetime management is automatic, discussed laterComLibrary.ComClass cc = new ComLibrary.ComClass();cc.TransferFunds(“Joe”, “Mike”, 1400.0f);
  23. 23. • COM objects live in an apartment STA – A single-threaded apartment STA (STA) has a single thread Thread Message Queue responsible for making all calls COM Object on its objects – A multi-threaded apartment (MTA) allows any thread to make calls on its objects• Data passed between apartments is marshaled
  24. 24. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  25. 25. shell32.dllActivation Store (Registry) Windows Metadata (Disk)
  26. 26. AddRef, Release IUnknown QueryInterface GetIids GetRuntimeIInspectable ClassName GetTrustLevel
  27. 27. • WinRT classes are registered in the registry – The extensions catalog maps concrete WinRT classes to contracts (launch, share, search, …) – The class catalog describes WinRT classes• Most WinRT objects are activated by name – "MyComponent.MyNamespace.MyClass" – Can work with activation factories (IActivationFactory) to customize object construction• Some WinRT objects are activated by contract – “Share operation required for data of type image/png”
  28. 28. Extension Catalog Class Catalog Extension 1 Runtime Class “A”LaunchContract Extension 2 Runtime Class “B”Search Extension 3 Runtime Class “C”Contract
  29. 29. RoActivateInstance creates a raw WinRT component by name The code below omits IUnknown::Release callsHSTRING className;LPCWSTR pszClassName = L"MyComponent.MyNamespace.MyClass";IInspectable* instance;IMyInterface* interface;HRESULT hr;hr = WindowsCreateString(pszClassName, wcslen(pszClassName), &className);hr = RoActivateInstance(className, &instance);WindowsDeleteString(className);hr = instance->QueryInterface(IID_IMyInterface, (void**)&interface);
  30. 30. Language Wrapper bound to projection createsRoActivateInstance object and a wrapper using returned to app metadata Object createdCatalog finds and internally, returns loads the DLL IInspectable DllGetActivation Factory-> Factory ActivateInstance()
  31. 31. • WinRT object reference = pointer to a pointer to a vtable, which is an array of pointers to functions – IUnknown methods first, then IInspectable methods – Metadata, discussed next, aids in on-the-fly discovery• Parameters are passed using the stdcall convention
  32. 32. IInspectable* instance = ...;typedef HRESULT (__stdcall *MatrixMultiply)( void* pThis, int* A, int m, int w, int* B, int n, int* C);MatrixMultiply pfn = (MatrixMultiply)((void**)*((void**)instance))[6];int* A = new int[1048576];int* B = new int[1048576];int* C = new int[1048576];pfn(instance, A, 1024, 1024, B, 1024, C);
  33. 33. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  34. 34. WinRT Type [Partial] .NET EquivalentBoolean BooleanByte Byte NoteDateTime DateTimeOffset • The object model is very similarGuid Guid • Reference types (classes, derivedInt64 Long from Object) and value typesObject ObjectString StringTimeSpan TimeSpan
  35. 35. WinRT Interface .NET ProjectionIMap<K,V> IDictionary<K,V>IMapView<K,V> IReadOnlyDictionary<K,V> TipIVector<T> IList<T> • You shouldn’t use the original WinRTIVectorView<T> IReadOnlyList<T> interfaces in .NET code—use only the projectionsIIterable<T> IEnumerable<T>IIterator<T> IEnumerator<T>IBindableVector IListIBindableIterable IEnumerable
  36. 36. • Most WinRT objects are MTA, except GUI objects – Some objects are hosted out-of-process (brokered) RuntimeBroker.exe App Projection Proxy Windows Runtime Object
  37. 37. Synchronous: Wait for result, then continue Asynchronous: Post callback, do something else NOTE: Parallel/concurrent does not mean asynchronous: Parallel.For is blocking, from its caller’s perspective!string data = ObtainDataFromServer(uri);DisplayData(data);ObtainDataFromServer(uri, data => DisplayData(data));Task.Factory.StartNew(() => ObtainDataFromServer()) .ContinueWith(t => DisplayData(t.Result));
  38. 38. • User interaction—do asynchronous work, the UI remains responsive – Challenge: marshal the continuation (callback) back to the UI thread, due to UI controls’ thread affinity• Scalability—some threads are scarce, some aren’t – Reuse thread for other requests while the work is in progress – Can differentiate CPU and I/O threads
  39. 39. • All APIs that can take longer than 50ms are asynchronous – No synchronous (blocking) versions – In many cases, asynchrony improves scalability• APIs return IAsync... interfaces – IAsyncAction, IAsyncActionWithProgress<TProgress> – IAsyncOperation<TResult>, IAsyncOperationWithProgress<T Result,TProgress>
  40. 40. Working with the async APIs directly is cumbersome We will see better alternatives laterGeolocator locator = new Geolocator();IAsyncOperation<Geoposition> asyncOp = locator.GetGeopositionAsync();asyncOp.Completed = new AsyncOperationCompletedHandler<Geoposition>( (aop, status) => { MessageDialog message = new MessageDialog("Location retrieved"); message.ShowAsync(); });
  41. 41. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  42. 42. • WinRT components ship metadata in .winmd files – ECMA-335 format, same as .NET assemblies – Windows metadata in Windows.winmd – Enable various languages to bind to the object, support IntelliSense – How to bind is up to the language—COM defines the ABI
  43. 43. • WinRT components are DLLs accompanied by .winmd files that contain WinRT classes• Cannot be distributed through the store stand-alone• Cannot be shared between applications• Can be sold through traditional software licensing – UI control frameworks, graphics libraries, math, etc.
  44. 44. • WinRT component = class library – Compile to generate a .winmd file (metadata)• There are some limitations on what can be exported – Only sealed classes are allowed – Only WinRT types can be passed across the interop boundary – Take care of passing collections aroundCan use the resulting library from JavaScript, C++, or C#Metro-style apps
  45. 45. “WinRT Component DLL” project type Use C++/CX extensions to export typesCan use the resulting library from JavaScript, C#, or C++Metro-style appspublic ref class WinRTComponent sealed {public: WinRTComponent() {} Platform::String^ GetString(int number) { return number.ToString(); }};
  46. 46. WinRT Pattern C# Pattern JavaScript PatternAsyncOperation awaitable, Task Promise Event, Event Event, += addEventListener Delegate Delegate function() … Collections System.Collections.Generic Array, hash
  47. 47. C++ App Projection C#/VB App Projection CLRObject HTML App Projection Windows Metadata Chakra
  48. 48. All WinRT APIs are projected automatically to all languages: C#, C++, JavaScript Projections behave according to the target language’s rulesGeolocator locator = new Geolocator();Geoposition pos = await locator.GetGeopositionAsync();MessageDialog dialog = new MessageDialog(pos.CivicAddress.ToString());dialog.ShowAsync();document.querySelector("#locationButton").onclick = function () { var locator = new Windows.Devices.Geolocation.Geolocator(); locator.getGeopositionAsync().then(function (pos) { resultText.textContent = pos.civicAddress; });};
  49. 49. • WinRT asynchronous operations are projected to first- class concurrency libraries in C#, C++, JavaScript – Can be awaited in C# – Can be converted to concurrency::task in C++ – Act like a promise in JavaScript• Similarly, language concurrency libraries can produce WinRT asynchronous operations
  50. 50. async void buttonUpdate_Click() { status.Text = "Downloading updates..."; runs on UI thread int count = await GetUpdatesAsync(clientID); returns immediately status.Text = "Got " + count + "updates"; callback, runs on UI thread}Task<int> GetUpdatesAsync(string clientID) { return Task.Run(() => GetUpdates(clientID)); runs on ThreadPool thread}async void buttonDownload_Click() { //downloads are parallel! Task<byte[]> first = DownloadPartAsync(1); Task<byte[]> second = DownloadPartAsync(2); DisplayTwoParts(await first, await second);}
  51. 51. Async methods can use arbitrary control flow The C# 5 compiler translates everything Example: Cancellationasync DownloadLargeFile(string url) { _cts = new CancellationTokenSource(); try { byte[] result = await DownloadFileAsync(url, cts.Token); PlayMovie(result); } catch (OperationCanceledException) { UpdateStatus("Download canceled."); }}void CancelDownload() { _cts.Cancel(); }
  52. 52. WindowsRuntimeSystemExtensions contains extension methods for asynchronous operations Supports GetAwaiter() for await pattern!CameraCaptureUI capture = new CameraCaptureUI();capture.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.MediumXga;var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);//Compiled to:WindowsRuntimeSystemExtensions.GetAwaiter<StorageFile>( capture.CaptureFileAsync(CameraCaptureUIMode.Photo));...
  53. 53. WindowsRuntimeSystemExtensions contains extension methods for asynchronous operations Can produce WinRT async operations!public sealed class HttpRetriever { public IAsyncOperation<string> GetUrl(string uri) { return Task.Run(async () => { HttpClient httpClient = new HttpClient(); string result = await httpClient.GetStringAsync(uri); return result; }).AsAsyncOperation(); }}
  54. 54. ConcRT’s task class can wrap a WinRT async operation and create a continuation chain Somewhat similar to C#’s await, only manualCameraCaptureUI^ capture = ref new CameraCaptureUI();task<StorageFile^> captureTask( capture->CaptureFileAsync(CameraCaptureUIMode::Photo));captureTask.then([] (StorageFile^ result) { MessageDialog^ dialog = ref new MessageDialog(result->Name); dialog->ShowAsync();});
  55. 55. ConcRT ships with a create_async function that generates a WinRT async operation implementation Progress reporting and cancellation also supportedIAsyncOperationWithProgress<int,int>^ Math::CountPrimes( int start, int end){ return create_async([=](progress_reporter<int> progress) { int count = 0; for (int i = start; i < end; ++i) { if (IsPrime(i)) ++count; if (i % 100 == 0); } return count; });}
  56. 56. WinRT async operations are converted to promises in JavaScript, which offer .then() for continuationsvar retriever = new ManagedComponent.HttpRetriever();retriever.getUrl("").then( function (result) { ... }, function (error) { ... });
  57. 57. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  58. 58. • Interop mainly has to deal with object lifetime inconsistency – COM implements reference counting – .NET uses a tracing GC• RCW = Runtime Callable Wrapper – References a COM object, +1 to the RC – Implements a finalizer that decrements the RC – Marshal.ReleaseComObject is the Dispose equivalent• CCW = COM Callable Wrapper – References a managed object as a COM interface
  59. 59. • Reference cycles between RCWs and CCWs may cause memory leaks Managed Object The GC can’t see CCW RCW below this line COM Object
  60. 60. The biggest performance problems are chatty interfaces and data copies Most data copies can be eliminated by careful API design (e.g. accept a buffer for filling instead of returning it) Design chunky interfaces//Chatty interface:void SetElement(int index, int value);//Chunky interface:void SetElementRange(int begin, int end, int[] values);
  61. 61. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
  62. 62. • The Visual Studio Profiler partially supports Metro applications in full—managed, native, and JavaScript – VSPerf.exe tool can do command-line profiling, too Sampling Instrumentation Allocations Concurrency • CPU-bound apps, very • I/O-bound apps, CPU- • Details on who • Only desktop apps low overhead bound apps, higher allocated and what • Full program stacks overhead • Only managed code (including all system • More detailed timing • Only desktop apps DLLs) data, limited stacks • Tier interactions • Only JavaScript apps
  63. 63. Introduction to Windows 8COM RefreshmentWindows Runtime Objects and APIBroker ProcessWinRT Types, ThreadingModel, Asynchronous ProgrammingMetadata, LanguageProjections, and WinRT ComponentsInteroperability with WinRT
  64. 64. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel