Jump to content

Windows Runtime

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 87.149.179.68 (talk) at 08:38, 31 August 2012 (→‎.NET). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Windows Runtime, or WinRT, is a cross-platform application architecture on the Windows 8 operating system. WinRT supports development in C++/CX (Component Extensions, a language based on C++) and the managed languages C# and VB.NET, as well as JavaScript. WinRT applications natively support both the x86 and ARM architectures, and also run inside a sandboxed environment to allow for greater security and stability.[1][2] WinRT will also be part of the upcoming Windows Phone 8 operating systems.

WinRT is essentially a COM-based API, although relying on an enhanced COM. Because of its COM-like basis, WinRT allows relatively easy[need quotation to verify] interfacing from multiple languages, just as COM does, but it's essentially an unmanaged, native API. The API definitions are, however, stored in ".winmd" files, which are encoded in ECMA 335 metadata format, the same format that .NET uses with a few modifications.[3] This common metadata format allows for significantly less overhead when invoking WinRT from .NET applications compared to a P/Invoke, and much simpler syntax.[4] 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. Regular C++ (with COM-specific discipline) can also be used to program with WinRT components,[5] with the help of new template library called Windows Runtime C++ Template Library (WRL), which is similar in purpose to what Active Template Library provides for COM.[6] The MSDN Library, however, recommends using C++/CX instead of WRL.[7]

Applications developed using WinRT for Windows 8 were previously referred to as "Metro-style apps", connecting it to Windows 8's use of a interface following the Metro design language as its primary desktop. However, reports surfaced that due to potential trademark issues with the German company Metro AG, Microsoft began to advise its Windows developers to stop using the term, although a Microsoft spokesperson denied these reports and stated that "Metro-style" was merely a codename for the new application platform.[8]

Technology

WinRT components are designed with an eye to interoperability between multiple languages and APIs, including native, managed and scripting. For example the Component Extensions of C++/CX are recommended to be used only at the API-boundary, not for other purposes.[9]

WinRT applications will run within a sandbox. Examination of the runtime libraries reveals that they are built upon Win32 API.[10] This is the same approach used by .NET.

Services

Metadata

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 BUILD-conference that the WinRT metadata is CLI metadata.[9] Native code (processor specific) cannot contain metadata and it is then stored in separate WINMD-files that can be reflected just like ordinary CLI assemblies.[11]

Because it is CLI metadata the programmer can then use code written in native WinRT-languages from managed CLI languages.

Type system

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 template keyword. WinRT classes (ref classes) can also be genericized using C++ templates but only template instantiations can be exported to .winmd metadata (with some name mangling), unlike WinRT generics which preserve their genericity in the metadata. WinRT also provides a library of generic containers that parallel those from the C++ standard library, as well as some back-and-forth conversion functions. The consumption of WinRT collections in .NET languages (e.g. C# and VB) as well as in JavaScript is more transparent than in the case of C++, with automated mappings into their natural equivalents taking place behind the scenes. When authoring a WinRT component in a managed language, some extra, COM-style rules need to be followed, e.g. .NET framework collection types cannot be declared as return types, but only the WinRT interfaces that they implement can be used at the component boundary.

WinRT components

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.[12]

Programming interfaces

Programs and libraries targeted for the WinRT runtime can be created and consumed from a number of platforms and programming languages. Notably C/C++ (either with language extensions offering first-class support for WinRT concepts, or with a lower-level template library allowing to write code in standard C++), .NET (C# and VB.NET) and JavaScript. This is made possible by the metadata.

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.[13] 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.[14] C++/CX on the other hand wraps-up calls to WinRT with code that does error checking and throws exceptions as appropriate.[15]

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.[15]

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.

.NET

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 Oxygene (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.

Limitations
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.[12]

JavaScript

WinRT applications can also be coded using HTML5 with JavaScript in code-behind. The "Chakra" JavaScript engine which was developed for Internet Explorer 9 integrates with WinRT; WinRT's features were adapted to follow JavaScript naming conventions, and namespaces were also mapped to JavaScript objects.

API

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.

Windows classes

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.

Naming conventions

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.

These conventions are projected differently in some languages, like JavaScript, which converts it to its conventions and the other way around. This is to give a native and consistent experience regardless of the programming language.

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.).[16]

  • Basic Types
    • In .NET languages and C++, there is a rich set of data types representing various numerals.
    • In JavaScript, a Number can only represent up to 53 bits of precision.
    • In WinRT, the only lacking numeral data type is 8-bit signed integer when compared to .NET and C++. JavaScript developers have to pay attention when dealing with big numbers while coding against WinRT.
  • Strings
    • The strings in .NET and JavaScript are immutable, whereas they are mutable in C++.
    • The null string value is represented by the null object in JavaScript. .NET uses a distinguished value for null for all reference types including string. C++ strings has no null semantics.
    • In WinRT, strings are immutable and null value is not allowed. If null value is passed to a string in WinRT by a language that has null semantics, the value is converted to empty string.
  • Structs
    • In .NET and C++, structs are value types, and such a struct can contain any type in it.
    • JavaScript does not directly support structs.
    • 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.
  • References
    • 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.
  • Arrays
    • In .NET, C++, and JavaScript arrays are reference type.
    • In WinRT, arrays are value type.
  • Events
    • In .NET and C++, clients subscribe to events using += operator.
    • In JavaScript, addEventListener function or setting on<EventName> property is used to subscribe to events.
    • In WinRT, all languages are allowed to use their own way of subscribing to events.
  • Collections
    • 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
    • All WinRT languages feature overloading on parameters (.NET, C++, JavaScript)
    • .NET and C++ also feature overloading on type.
    • In WinRT, only parameter number is used for overloading.
  • Asynchrony
    • 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.

See also

References

  1. ^ Abel Avram (21 September 2011). "Design Details of the Windows Runtime". InfoQ.
  2. ^ Brian Klug & Ryan Smith (13 September 2011). "Microsoft BUILD: Windows 8, A Pre-Beta Preview". AnandTech.
  3. ^ de Icaza, Miguel (2011-09-15). WinRT demystified. Personal blog of Miguel de Icaza, 15 September 2011. Retrieved from http://tirania.org/blog/archive/2011/Sep-15.html.
  4. ^ "What is the COM marshaling overhead in calling the WinRT API from C# ?". Social.msdn.microsoft.com. Retrieved 2012-04-24.
  5. ^ Sivakumar, Nish (2011-09-29). "Visual C++ and WinRT/Metro - Some fundamentals - CodeProject®". Codeproject.com. Retrieved 2012-04-24.
  6. ^ "Using the Windows Runtime from C++ | BUILD2011 | Channel 9". Channel9.msdn.com. 2011-09-14. Retrieved 2012-04-24.
  7. ^ "Windows Runtime C++ Template Library". Msdn.microsoft.com. Retrieved 2012-04-24.
  8. ^ "Microsoft advises developers to stop using 'Metro' name in apps following possible trademark dispute". The Verge. Retrieved 3 August 2012.
  9. ^ a b "Using the Windows Runtime from C++ | BUILD2011 | Channel 9". Channel9.msdn.com. 2011-09-14. Retrieved 2012-04-24.
  10. ^ "WinRT and .NET in Windows 8 (Paragraph 5)".
  11. ^ http://www.ciprianjichici.ro/blog/post/NET-Gets-a-New-Lease-of-Life.aspx
  12. ^ a b "Using the Windows Runtime from C# and Visual Basic | BUILD2011 | Channel 9". Channel9.msdn.com. 2011-09-14. Retrieved 2012-04-24.
  13. ^ "Inside the C++/CX Design - Visual C++ Team Blog - Site Home - MSDN Blogs". Blogs.msdn.com. 2011-10-20. Retrieved 2012-04-24.
  14. ^ 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.
  15. ^ a b Under the covers with C++ for Metro style apps with Deon Brewis at //BUILD
  16. ^ http://channel9.msdn.com/events/BUILD/BUILD2011/PLAT-876T