Windows Runtime

出典: フリー百科事典『ウィキペディア(Wikipedia)』
移動: 案内検索

Windows Runtime (WinRT) はマイクロソフトによる新しいプログラミングモデルであり、Metro UIスタイルのアプリケーション (Immersiveとして知られる) を作成するバックボーンである。Windows 8で採用される。[1][2] WinRTはC++C#VB.NETJavaScriptによる開発に対応する。

概要[編集]

WinRTは本質的にCOMのAPIである。そして、enhanced COMにも依拠している。 COM-like basisのおかげで、WinRTは様々な言語からインターフェイスを結ぶことを可能としており、それはCOMに他ならず、しかし本質的にはアンマネージドなネイティブAPIである。このAPIの定義はしかしながら".winmd"ファイルに格納されており、ECMA 335メタフォーマットでエンコードされている。同じフォーマットはいくらかの改変を加えた形で.NETでも用いられている。[3]

この共通のメタフォーマットによりP/Invokeと比較して非常に小さなオーバーヘッドでWinRTを.NETアプリケーションから呼び出すことが可能になる。文法もよりシンプルになる。[4] 新しいC++/CX (Component Extensions) 言語はいくらかのC++/CLIの文法を借用しており、WinRTのコンポーネントの作成や組入が可能である。C++によるクラシックなCOMプログラミングと比較してより少ない糊付け作業ですみ、C++/CLIと比較して課される制約がほとんどない (on the mixing of types)。通常のC++(COM準拠の規約つきの)も新しいtemplateライブラリの呼び出しによってWinRTコンポーネントのプログラミングが可能である。[5] Windows Runtime C++ Template Library (WRL) はATLが提供する機能のCOM版である。[6] しかしながらMSDNでは WRLの利用ではなくC++/CX によるプログラミングを推奨している。[7]

歴史[編集]

WinRTは、2011年9月12日の Microsoft BUILD-conference で明らかになり、Windows 8の開発者プレビューに含まれている。C++/CX と WinRT のサポートは、Visual Studio 11 に含まれている。

技術[編集]

WinRTコンポーネントは、ネイティブ、マネージコード、スクリプティングを含む複数の言語とABIの間の相互運用性を視野に入れて設計されている。例えばC++/CXのコンポーネントの拡張はABI境界でのみ使用されることが勧められている[8]

WinRTアプリケーションは数秒でインストールし、サンドボックス内で実行される。ランタイムライブラリの検討は、それらがWin32 APIの上に構築されていることを明らかにした[9]。これは .NET で使われているのと同じアプローチである。

サービス[編集]

メタデータ[編集]

WinRTメタデータはWinRTに対して書かれたコードを補足する。プログラミング言語にかかわらずオブジェクト指向的なプログラミングモデルを定義し、リフレクションなどの機能も提供する。 マイクロソフトのC++専門家であるハーブ・サッター英語版のBUILDカンファレンスにおいての説明によると、WinRTメタデータは.NETメタデータと同じ物である[10]ネイティブコードにはメタデータを含めることはできず、別にWINMDファイルとして保存される。その場合でもリフレクションは.NETアセンブリ同様に動作する[11]

.NETメタデータと同じものなので、WinRTに対してネイティブ言語で開発されたコードは.NET上のプログラミング言語からも利用することができる。

型システム[編集]

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 コンポーネント[編集]

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]

プログラミングインターフェイス[編集]

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, コンポーネント拡張)[編集]

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) is 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 supports XAML-based .NET Metro-style applications which are primarily written in C# and VB.NET. 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 to not be able to be derived from as a limitation. However, non-sealed WinRT classes defined elsewhere can be inherited from in .NET, their virtual methods overriden, and so on (but the inherited managed class must still be sealed).

Members that interfaces with other language must have a signature with WinRT types or a managed type that is convertible to these.[16]

JavaScript[編集]

Microsoft initially announced that HTML5 and JavaScript would be integrated as part of the development experience for Windows 8. The "Chakra" JavaScript engine which was developed for Internet Explorer 9 integrates with WinRT. That makes JavaScript a first-class citizen in the WinRT ecosystem.

The JavaScript environment does some tricks under the hood. It adapts WinRT features to follow the JavaScript naming conventions to make the developer get the same experience as on the Web-platform. Namespaces are mapped to JavaScript objects and some special members are also added in some cases to make the developer experience similar to any web scenario.

Metro interfaces can be built with HTML5 and CSS and JavaScript in code-behind.

The current version is JavaScript 5.

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 the Metro 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 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. 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.

参照[編集]

外部リンク[編集]