|A component of Microsoft Windows|
|Type||Application programming interface|
|Included with||Windows 8, Windows RT|
|Windows Store, Windows API|
- 1 Technology
- 2 Services
- 3 Windows Phone Runtime
- 4 See also
- 5 References
- 6 External links
WinRT is implemented in the C++ programming language and is object-oriented by design. (Its predecessor, Win32 API is written mostly in the C programming language.) It is an unmanaged application programming interface (API) based on Component Object Model (COM) that allows interfacing from multiple languages, just as COM does. The API definitions, however, are stored in ".winmd" files, which are encoded in ECMA 335 metadata format, the same format that .NET Framework uses with a few modifications.[unreliable source?] This common metadata format allows for significantly less overhead when invoking WinRT from .NET applications compared to P/Invoke, and much simpler syntax.[unreliable source?]
The new C++/CX (Component Extensions) language, which borrows some C++/CLI syntax, allows the authoring and consumption of WinRT components with less glue visible to the programmer compared to classic COM programming in C++, and at the same time imposes fewer restrictions compared to C++/CLI on the mixing of types. The Component Extensions of C++/CX are recommended to be used only at the API-boundary, not for other purposes. Regular C++ (with COM-specific discipline) can also be used to program with WinRT components, with the help of the new Windows Runtime C++ Template Library (WRL), which is similar in purpose to what Active Template Library provides for COM.
WinRT applications run within a sandbox and require explicit user approval to access critical OS features and underlying hardware. File access is restricted to several predetermined locations, such as Documents or Pictures.
WinRT applications for Windows 8 and RT are packaged in the
.appx file format; based upon Open Packaging Conventions, it uses a ZIP format with additional XML files. WinRT applications are primarily distributed through an application store known as Windows Store, where WinRT software (referred to as Windows Store apps) can be downloaded and purchased by end-users. WinRT apps can only be sideloaded from outside Windows Store on Windows 8 or RT systems that are part of a Windows domain, or equipped with a special activation key obtained from Microsoft.
The metadata describes the code written for the WinRT platform. It defines a programming model that makes it possible to write object-oriented code that can be shared across programming languages. It also enables services like reflection.
Herb Sutter, C++ expert at Microsoft, explained during his session on C++ at the 2011 BUILD conference that the WinRT metadata is CLI metadata. Native code (i.e., processor-specific machine code) cannot contain metadata and it is then stored in separate WINMD-files that can be reflected just like ordinary CLI assemblies.
Because it is CLI metadata the programmer can then use code written in native WinRT-languages from managed CLI languages.
WinRT has a rich object-oriented class-based type system that is built on the metadata. It supports constructs with corresponding constructs that are found in the .NET framework: classes, methods, properties, delegates and events.
One of the major additions to WinRT relative to COM is the cross-ABI, .NET-style generics. In C++/CX these are declared using the
generic keyword with a syntax very similar to that of the
Classes that are compiled to target the WinRT are called WinRT components. They are classes that can be written in any supported language and for any supported platform. The key is the metadata. This metadata makes it possible to interface with the component from any other WinRT language. The runtime requires WinRT components that are built with .NET Framework to use the defined interface types or .NET type interfaces, which automatically map to the first named. Inheritance is as yet not supported in managed WinRT components, except for XAML classes.
In WinRT terminology, a language binding is referred to as a language projection.
C++ (WRL, Component Extensions)
Native C++ is a "first-class citizen" of the WinRT-platform. To use WinRT from C++ two supported options are available: WRL—an ATL-style template library—and C++/CX (C++ with Component Extensions) which resembles C++/CLI. Because of the internal consumption requirements at Microsoft, WRL is exception-free, meaning its return-value discipline is HRESULT-based just like that of COM. C++/CX on the other hand wraps-up calls to WinRT with code that does error checking and throws exceptions as appropriate.
C++/CX has a number of extensions that enable integration with the platform and its type system. The syntax resembles the one of C++/CLI although it produces native code and metadata that integrates with the runtime. For example WinRT objects may be allocated with
ref new, which is the counterpart of
gcnew from C++/CLI. The hat operator (^) retains its meaning, however in the case where both the caller and callee are written in C++ and living in the same process, a hat reference is simply a pointer to a vptr to a vtable.
An addition to C++/CX relative to traditional C++ COM programming are partial classes, again inspired from .NET. These allow for instance XAML code to be translated into C++ code by tools and then combined with human-written code to produce the complete class while allowing clean separation of the machine-generated and human-edited parts of a class implementation into different files.
WinRT is a native platform and supports any native C++ code. A C++ developer can reuse existing native C/C++ libraries with the only need to use the language extensions when writing code that is interfacing with the runtime.
- We are very keen on supporting WinRT with native Delphi & C++ code. Right now, the issues surrounding the WinRT space center around the fact that many OS-supplied APIs which are required by anyone implementing their own language RTL are actually off-limits unless you're the VC++ RTL DLL. You know, little things like RtlUnwind for exception processing and VirtualAlloc (et al.) for memory management. … Any calls to those APIs from your application will automatically disqualify your application from being an "official" WinRT application capable of delivering through the MS app store.
- Right now the VC++ RTL DLL is given special dispensation since that is the library that makes the calls to those forbidden APIs and not directly from the user's app. We're currently rattling some cages at MS to find out how or if they're going to allow third-party tools to target WinRT. Until we can get past that, targeting WinRT isn't actually possible from a deliverable product sense. We are able to build WinRT applications with Delphi that work with a developer certificate, however they all fail the application qualification checks because of the aforementioned (and other) APIs.
Due to this unsolved issue recent Delphi compilers feature Metropolis instead of Metro. In order to display a live tile a Metropolis application uses additional proxy application.
The .NET Framework and the Common Language Runtime (CLR) are integrated into the WinRT as a subplatform. It also has influenced and set the standards for the ecosystem through the metadata format and libraries. The CLR provides services like JIT-compilation code and garbage collection. WinRT applications using .NET languages use the new Windows Runtime XAML Framework, and are primarily written in C#, VB.NET, and for the first time for XAML, with native code using C++/CX. Although not yet officially supported, programs can also be written in other .NET languages.
Classes defined in WinRT components that are built in managed .NET languages must be declared as
sealed, so they cannot be derived from. However, non-sealed WinRT classes defined elsewhere can be inherited from in .NET, their virtual methods overridden, and so on (but the inherited managed class must still be sealed).
Members that interface with another language must have a signature with WinRT types or a managed type that is convertible to these.
WinRT comes with an Application Programming Interface (API) in the form of a class library that exposes the features of Windows 8 for the developer, like its immersive interface API. It is accessible and consumable from any supported language.
The Windows classes are native C/C++ libraries (unmanaged) that are exposed by the WinRT. They provide access to all functionality from the XAML parser to the camera function.
The naming conventions for the components (classes and other members) in the API are heavily influenced by the .NET naming conventions which uses camel case (specifically PascalCase). Microsoft recommends users to follow these rules in case where no others are given.
Restrictions and rules
Since Windows Runtime is projected to various languages, some restrictions on fundamental data types exist in order to host all of these languages. Programmers have to be careful with the behavior of those types when used with public access (for method parameters, method return values, properties, etc.).
- Basic Types
- In .NET languages and C++, there is a rich set of data types representing various numerals.
- A null pointer passed as a string to WinRT by C++ will be converted to an empty string
- In .Net null being passed as a string to WinRT will be converted to an empty string
- In .NET and C++, structs are value types, and such a struct can contain any type in it.
- In WinRT, usage of structs is only allowed for containment of types that has value semantics, including numerals, strings, and other structs. No pointers or interface references are allowed.
- In .NET, objects are passed by reference, whereas numerals and structs are passed by value.
- In C++, all types can be passed by reference or by value.
- In WinRT, interfaces are passed by reference; all other types are passed by value.
- In WinRT, arrays are value types.
- In .NET and C++, clients subscribe to events using += operator.
- In WinRT, all languages are allowed to use their own way of subscribing to events.
- Various .NET collections map directly to WinRT collections.
- WinRT Vector type resembles arrays and the array syntax is used to consume them.
- WinRT Map type is a key/value pair collection, and is projected as Dictionary in .NET languages.
- Method Overloading
- .NET and C++ also feature overloading on type.
- In WinRT, only parameter number is used for overloading.
- All WinRT methods are designed such that any method taking longer than 50 milliseconds is an async method.
- There is an established naming pattern to distinguish asynchronous methods: <Verb>[<Noun>]Async. Through the entire runtime library, all methods that have chance to take longer than 50 ms are only implemented as asynchronous methods.
Windows Phone Runtime
Starting from Windows Phone 8 it is possible to develop apps using a version of the Windows Runtime called the Windows Phone Runtime (WPRT). Although WP8 brought limited support, the platform did eventually converge with Windows 8.1 in Windows Phone 8.1.
Windows Phone 8
Windows Phone 8 has limited support for development and consuming of Windows Runtime components through Windows Phone Runtime. Many of the Windows Runtime APIs in Windows 8 that handle core operating-system functionality have been ported to Windows Phone 8. Support for development of native games using C++/CX and DirectX has been added by request from the game development industry.
The Windows Phone XAML Framework is however still based on the same Silverlight framework as in Windows Phone 7 for backwards compatibility. XAML development is therefore not currently possible in C++/CX. Development using either HTML5 or WinJS is unsupported on Windows Phone 8.
Windows Phone 8.1
Windows Phone Runtime uses the AppX package format from Windows 8, after previously been using Silverlight XAP.
- Abel Avram (21 September 2011). "Design Details of the Windows Runtime". InfoQ.
- Brian Klug & Ryan Smith (13 September 2011). "Microsoft BUILD: Windows 8, A Pre-Beta Preview". AnandTech.
- Windows Phone API reference
- Michael, Mayberry (2012). WinRT Revealed. New York City: Apress. p. 3. ISBN 978-1-4302-4585-8.
- "Creating Win32 Applications (C++)". MSDN. Microsoft. Retrieved 12 January 2014.
- De Icaza, Miguel (15 September 2011). "WinRT demystified". Personal blog of Miguel de Icaza. Self-published. Retrieved 15 January 2014.
- "What is the COM marshaling overhead in calling the WinRT API from C# ?". MSDN forum. Self-published. 20 September 2011. Retrieved 15 January 2014.
- "Using the Windows Runtime from C++ | BUILD2011 | Channel 9". Channel9.msdn.com. 2011-09-14. Retrieved 2012-04-24.
- Sivakumar, Nish (2011-09-29). "Visual C++ and WinRT/Metro - Some fundamentals - CodeProject®". Codeproject.com. Retrieved 2012-04-24.
- "Using the Windows Runtime from C++ | BUILD2011 | Channel 9". Channel9.msdn.com. 2011-09-14. Retrieved 2012-04-24.
- "Designing a simple and secure app package – APPX". Windows 8 app developer blog. Retrieved 30 December 2013.
- "How to Add and Remove Apps". TechNet. Microsoft. 31 May 2012. Retrieved 4 October 2012. "To enable sideloading on a Windows 8 Enterprise computer that is not domain-joined or on any Windows® 8 Pro computer, you must use a sideloading product activation key. To enable sideloading on a Windows® RT device, you must use a sideloading product activation key. For more information about sideloading product activation keys, see Microsoft Volume Licensing."
- "Windows 8: The Metro Mess". PC Magazine. Retrieved 8 September 2012.
- "Microsoft now using 'Modern UI Style' to refer to Windows 8 'Metro Style' apps". Retrieved 10 August 2012.
- "What's a Windows Store app?". Windows Dev Center. Retrieved 1 October 2012.
- "Asynchronous programming (Windows Store apps)". MSDN. Microsoft. Retrieved 12 January 2014.
- "Using the Windows Runtime from C# and Visual Basic | BUILD2011 | Channel 9". Channel9.msdn.com. 2011-09-14. Retrieved 2012-04-24.
- "Inside the C++/CX Design - Visual C++ Team Blog - Site Home - MSDN Blogs". Blogs.msdn.com. 2011-10-20. Retrieved 2012-04-24.
- By: Charles (2011-10-26). "GoingNative 3: The C++/CX Episode with Marian Luparu | C9::GoingNative | Channel 9". Channel9.msdn.com. Retrieved 2012-04-24.
- Under the covers with C++ for Metro style apps with Deon Brewis at //BUILD
- Delphi and WinRT or Windows 8 "Dirty Little Secret"
Why Delphi Cannot (currently) Support WinRT
- Live Tile Support for Metropolis UI Applications
- Windows Phone Runtime API