{"id":1689,"date":"2013-08-22T00:00:00","date_gmt":"2013-08-22T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/windows-runtime-fielding-the-obvious-questions\/"},"modified":"2021-05-17T18:36:05","modified_gmt":"2021-05-17T18:36:05","slug":"windows-runtime-fielding-the-obvious-questions","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/windows-runtime-fielding-the-obvious-questions\/","title":{"rendered":"Windows Runtime: Fielding the Obvious Questions"},"content":{"rendered":"<div class=\"article-content\">\n<h2>What is the Windows Runtime?<\/h2>\n<p class=\"start\">The Windows Runtime (&#8216;WinRT&#8217;) is the most  recent programming interface into the Windows operating system. It first  appeared in the Windows 8 family of OSs and a variant of it has since appeared  in Windows Phone 8. WinRT is easily confused with Windows RT, the name of the  version of Windows 8 that runs on ARM-based tablets and computers. In this  article, WinRT will always mean the Windows Runtime.&#160; WinRT has roots both in COM and .NET. To  understand where it came from, it helps to understand a little about where COM  and .NET themselves each came from.<\/p>\n<p>   There once was a dream: A dream of a platform  where a programmer could write a useful object (for fun, profit, or both) and  share it with other programmers. Those programmers could use that object in  their own projects, either to build more complex objects or to build whole  computer programs. And none of these programmers would need to know or care  what programming language any of the other programmers used. This was the dream  that launched COM.<\/p>\n<p>   COM is a component-based architecture that  is based on the object-oriented paradigm. It lets programs use objects without  knowing their internal implementation details. Indeed, a program doesn&#8217;t even  need to be written in the same programming language as any COM objects it uses.  To create and use a COM object , the program only needs to know&#160; the object&#8217;s interface, the interface&#8217;s id (a  GUID), and an implementing object&#8217;s class id.<\/p>\n<p>   That&#8217;s the theory, anyway. COM eventually evolved  to deliver most of what its designers intended, but in a lot of ways COM was  too far ahead of its time when it was developed. Writing simple COM code was  rarely simple, and writing complex COM code was often a tortuous task.  Programmers who could write correct COM code and successfully debug and fix  incorrect COM code were in high demand as a result. And while COM objects  could, in theory, be created and used in any programming language, in practice  they were usually written in C++ and used in either C++ or Visual Basic  programs (the original VB, before .NET).<\/p>\n<p>   Time passed, programming language design  evolved, and .NET was born..NET was able to &#160;improve on&#160;  certain aspects of COM in pursuit of its goal of seamless  interoperability between objects. It used a system of metadata in place of  COM&#8217;s complicated IDL and TLB files. This system vastly simplified the creation  of .NET objects. Where COM had many different types for the same purpose (e.g.  for strings), .NET had one (e.g. System.String).<\/p>\n<p>   But interoperability between .NET and  non-.NET code was complicated. .NET was designed for to be easy to use and not  to have the memory leaks that plagued programs at the time it was developed. It  adopted the automatic memory management model known as &#8216;garbage collection&#8217;. A  special object, the GC, does all the heap allocations for the program and  tracks all references to those objects that it has allocated. Periodically, the  GC does a collection wherein it finds all objects that are no longer reachable  by the program and destroys them, releasing their memory for future use. As a  by-product &#160;of how the GC works, it also  performs heap compactions at times in order to create larger blocks of free  memory. This compaction will result in an object&#8217;s memory address being liable  to change at any time. This doesn&#8217;t matter to .NET programs because whenever  the garbage collector&#160; does a compaction,  it patches things up where it needs to. But it matters very much to non-.NET  programming languages since they have no way of knowing that the object moved  and thus will continue to look for it at that old address. This is one of  several issues that make it is impossible to use .NET to realize the dream of  seamless interop without regard to language.<\/p>\n<p>   Thus we get to the origins of WinRT. At its  core, WinRT looks like an improved, simplified COM merged with the .NET  metadata system (in place of COM&#8217;s old system of CLSIDs, IIDs, IDL files, and  TLB files). By using COM&#8217;s interface-based design, WinRT&#8217;s designers were able  to deliver a level of language-agnostic interop that .NET could not achieve. By  using .NET&#8217;s metadata format and borrowing other design features from .NET  (such as a single string type and making it possible for an object to know its  own type at all times), they avoided the complexities that made using COM very  difficult for most programmers. Shucked of COM&#8217;s lesser used features and  burnished by a type and metadata system closely resembling .NET, WinRT delivers  all of the important aspects of the dream that launched COM.<\/p>\n<h2>How does WinRT relate to .NET?<\/h2>\n<p>WinRT is a companion to .NET. Because its  metadata system is almost the same as .NET&#8217;s metadata system and it uses <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/br205768(v=vs.85).aspx\">base  types<\/a> that map closely to .NET types, the two runtimes work together as  partners (perhaps even friends, to the extent that bits and bytes can form  friendships). (<em>Ed: maybe just a bit on  the side?<\/em>)<\/p>\n<p>   WinRT provides a sandboxed application  model. While .NET has had sandboxed apps before (Silverlight), the restrictions  needed to accomplish the sandboxing all but shut down access to the system.  Users had to decide&#160; low-level issues  such as whether or not they trusted a Silverlight app to be allowed to use  hardware graphics acceleration. Many other capabilities were simply denied to  the app altogether. While WinRT still restricts what an app can do, because it  was designed from the ground up to be an OS API with sandboxing, it was  possible for the designers to grant apps many more capabilities (e.g. sharing  data and files between apps).<\/p>\n<p>   WinRT also adopts asynchrony, which works  wonderfully with .NET 4.5&#8217;s async and await features. The wait cursor (once  represented as an hourglass and later a spinning disc, but always a source of  annoyance regardless of form) is something users of WinRT apps should rarely encounter  and even then only for a few moments. Instead, operations that could  potentially run for a long time are moved off the UI thread to keep  applications responsive to user input even as files are being loaded or data is  being streamed across a network.<\/p>\n<p>   Most often overlooked is that WinRT <em>is<\/em> a companion for .NET. It&#8217;s not a  replacement and you shouldn&#8217;t expect Microsoft to be prying WPF, ASP.NET, Win Forms,  or any other .NET technology from your hands because it&#8217;s not going to happen. While apps built for the Windows Store or  for Windows Phone 8 make use of WinRT APIs, desktop apps, web sites and  applications, WCF services, and other .NET projects will continue to play a  role for many years to come. <\/p>\n<h2>So what happened to Silverlight then?<\/h2>\n<p>When Silverlight was introduced, the idea  of developing a web application without some sort of plugin was often  laughable. Deficiencies of the time such as HTML 4&#8217;s simplicity, the loose  adherence to standards in the various browsers and the rudimentary state of  CSS, made plugins the only effective way to develop a web application with a  (relatively) uniform experience regardless of browser or platform. &#160;Over time, however, browsers complied better  with the standards, there has been a steady rollout of HTML5 technologies, and  JavaScript libraries such as jQuery have matured and multiplied. Now, it is not  only possible, but reasonable to develop a web application without plugins  (and, more and more, it&#8217;s expected). The age of the browser plugin is passing  and it&#8217;s not just Silverlight that is fading from relevance; Flash, too, is steadily  passing from being an important browser component to being a superfluous add-on.  Silverlight is not yet dead, but its raison d&#8217;&#234;tre is.<\/p>\n<h2>What is interop like with WinRT?<\/h2>\n<p>I wrote an article here on Simple-Talk  about interop entitled <a href=\"https:\/\/www.simple-talk.com\/dotnet\/.net-framework\/5-tips-for-understanding-managed-unmanaged-interoperability-in-.net\/\">5  Tips for Understanding Managed-Unmanaged Interoperability in .NET<\/a>. The good  news? You don&#8217;t need to understand any of it to use WinRT components. (The bad  news? You still need to understand interop for non-WinRT interactions between  managed and unmanaged code.)<\/p>\n<p>  A native C++ programmer can use a WinRT  component written by a VB programmer with no special magic needed. The same is  true for a C# programmer using a native C++ WinRT component and any other mix  and match of those languages (plus any other languages that programmers can  author WinRT components in).<\/p>\n<p>   A JavaScript programmer can use WinRT  components in his or her HTML5-based WinRT app too. It&#8217;s not currently possible  to author WinRT components in JavaScript and you cannot mix-and-match display  technologies (i.e. no displaying XAML or DirectX content directly in an HTML5  app). But with those limitations in mind, you can author WinRT components that can  then be used by other programmers using languages as different as C++ and  JavaScript without any need to know what language you wrote it in and without  doing anything more complicated than adding a reference to the component&#8217;s  WinMD metadata file.<\/p>\n<p>   Indeed, because developers in these  non-.NET languages can use your .NET WinRT components, if you are a component  vendor, your potential client base expands dramatically with WinRT. And if and  when other languages gain WinRT support, your components will just work in  those languages too. The same is also true for C++ WinRT component developers.<\/p>\n<h2>What pitfalls do I need to know about with WinRT and .NET?<\/h2>\n<p>Surprisingly, there aren&#8217;t &#160;many pitfalls to using WinRT with .NET. The  major one is a side-effect &#160;from WinRT using  what is called language projections.<\/p>\n<p> WinRT objects appear in each programming  language like any other object in that language. In JavaScript it even goes so  far as to project non-constructor method names to use <a href=\"http:\/\/en.wikipedia.org\/wiki\/CamelCase\">camel casing<\/a> (i.e. presenting  the first letter of a method name as being lower-case even though it&#8217;s actually  in upper-case since camel casing is how JavaScript programmers expect to see object  method names other than constructors).<\/p>\n<p>  Language projections go farther than just  appearance changes, though. WinRT types that have a similar type in the target  language are projected to appear to be that type. So the low level WinRT string  type, HSTRING, appears in .NET as being a System.String. For the most part this  causes no problems. But there are some things to note.<\/p>\n<p>   Much of the System.Type class functionality  now appears in the System.Reflection.TypeInfo class, for example. Also WinRT  has a reduced subset of exception classes compared with .NET so in places where  you might have caught, e.g., a System.UriFormatException, because that type of  exception doesn&#8217;t exist in WinRT, you instead catch its parent,  System.FormatException. Not all .NET types are supported in WinRT, and for  those that are, some of their members (especially extension methods) may not be  available (see, for example, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.string.aspx\">System.String<\/a>).  For a good overview of all of these issues, see: <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/apps\/br230302.aspx\">.NET  for Windows Store apps overview<\/a>.<\/p>\n<p>   At its core, WinRT uses reference counting  to manage the life span of most WinRT objects. If this sounds a lot like COM,  it&#8217;s because it is like COM. WinRT classes implement COM&#8217;s IUnknown interface.  Strictly speaking, they are required to implement an interface called  IInspectable, which derives from IUnknown. IInspectable was created for WinRT  in order to give objects the ability to know what type they are and what  interfaces they implement. This is all part of the machinery that enables the  .NET-like metadata and type systems. &#160;The  .NET CLR is aware of WinRT&#8217;s reference counting and handles it for you. So you  don&#8217;t need to do anything special here.<\/p>\n<p> Some WinRT types have a Dispose method. Just  like with any .NET object that implements IDisposable, it&#8217;s important to use  the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/yh598w02.aspx\">using  statement<\/a> with these types in order to ensure that any unmanaged resources  are freed deterministically (i.e. right away) rather than remaining allocated  until the GC gets around to freeing them (which can cause exceptions if you try  to re-access certain resources, such as files, before the GC has freed them).<\/p>\n<h2>Why should I learn WinRT?<\/h2>\n<p>Over 100 million Windows 8 licenses were  sold as of May 6, 2013, just a little over six months since its launch. Windows  8.1, a free update, is set to launch in Fall 2013, resolving most, if not all,  of the complaints some users have had about Windows 8.0. PC manufacturers have  been steadily rolling out great new touch-enabled tablets, convertibles,  laptops, and all-in-ones. With many improvements to the OS combined with great  hardware, holiday 2013 should see a healthy addition to the already large  install base of Windows 8 devices.<\/p>\n<p>  Windows Phone is growing slowly but  steadily. Microsoft continues to deliver great new developer education content  while working behind the scenes on updates and improvements to the platform.  Like with the original Xbox, Microsoft shows every sign of being willing to  play the long game with Windows Phone if necessary.<\/p>\n<p>   Nobody who can talk about it knows too much  about the three OSes that the Xbox One (the successor to the Xbox 360) will  run. One is said to be designed for games, one is believed to be a modified  version of Hyper-V, and the last is believed to be a customized version of the  Windows 8 kernel. The last OS is widely believed to include a variant of WinRT.<\/p>\n<p>   Given all of this, there really isn&#8217;t much  of a question left. Learning WinRT gives you access to awesome platforms with  great commercial potential. It also opens the door to the potentially lucrative  field of WinRT component development. Best of all, because of how easily WinRT  integrates into .NET, the learning curve is quite shallow. So what are you  waiting for?<\/p>\n<p>   Reference<br \/><a href=\"http:\/\/msdn.microsoft.com\/en-us\/magazine\/cc301471.aspx\">Is COM Dead?<\/a><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>WinRT, the Windows Runtime, was launched by Microsoft to some puzzlement. How does it relate to COM? Is it a replacement for .NET or SilverLight? is it the same as Windows RT? Why is it important? Michael answers these questions and more.&hellip;<\/p>\n","protected":false},"author":134536,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[4143,4229,4838],"coauthors":[],"class_list":["post-1689","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","tag-net","tag-net-framework","tag-silverlight"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/134536"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1689"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1689\/revisions"}],"predecessor-version":[{"id":54498,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1689\/revisions\/54498"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1689"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}