Home

Google Chrome Browser

making the web faster, safer, and easier

Main menu

  • Home
  • Chromebook
  • Chrome OS
  • Android
  • Releases
    • Stable
    • Beta channel
    • Dev channel
  • Downloads
  • Videos
    • Top Rated
    • Most Viewed
    • Most Commented
  • Articles
    • Top Rated
    • Most Viewed
    • Most Commented
  • About Us
Home

Subscribe to Google Chrome Browser by e-mail

Syndicate

User login

Tag Cloud

Beta updates browser browsers browsing chrome chromebook chrome extensions Chrome OS chromium Dev updates Downloads extensions Firefox Google googlechrome google chrome Internet Explorer Linux open source opera release security Stable updates TC video web web browser web browsers windows
more tags

Twitter Updates

Follow us on Twitter


    How to call WinRT APIs in Windows 8 from C# Desktop Applications - WinRT Diagram

    • View
    • Track
    Submitted by admin on Fri, 10/12/2012 - 00:45
    • google chrome
    • Win8

    I was trying to access some of the sensors that are that runs Windows 8. However, while there's support for Location Sensors built into the .NET 4 libraries on Windows 7 and up, I want to access the complete that is . Those APIs are available via COM and I could call them via COM, but calling them via the WinRT layer is so much nicer. Plus, this is kind of why WinRT exists.

    This got me thinking about WinRT and what it means. I did a but I've always found all the various diagrams that how things fit together to be .

    DISCLAIMER: All diagrams are, by their nature, oversimplifications. I work on Web Stuff, not Windows Stuff, so this is all my opinion and conjecture, done on my own time. I'm not in the Windows org, I'm just a dude trying to write an .

    I figure it can't be as complicated as all these diagrams. I drew this to help myself understand.

    WinRT Diagram

    Just like the C Language has the C Runtime that provides a bunch of supporting functions and defines a calling convention for them, so the Windows Runtime (WinRT) does for Windows and its languages. These APIs and runtime includes metadata about calling conventions that make WinRT APIs easier to call than COM.

    See how in the diagram I can call any API from the .NET CLR? In the case of the Sensors APIs I want to call, while they are ultimately Win32 APIs or COM APIs, I would like to call them using the highest level calling convention available and that's the very friendly Windows RT ones.

    Calling WinRT APIs from C# Desktop Applications

    I like to test things using small Console Apps, but those aren't "Windows Store Applications," so am I allowed to call WinRT APIs from my Desktop or Console application?

    Sure. There's actually a section of the MSDN Documentation that . I can specifically and make sure it's allowed to be called from Desktop applications.

    LightSensor is allowed to be called from Desktop Apps

    There isn't super-clear but there IS documentation on how to add WinRT references to non-Windows Store applications.

    Adding a Reference to WinRT from a Desktop App

    The docs say, :

    In the desktop projects, the Core tab doesn’t appear by default. The user can choose to code against the Windows Runtime by opening the shortcut menu for the project node, choosing Unload Project, adding the following snippet, opening the shortcut menu for the project node again, and then choosing Reload Project. Now, when the user invokes the Reference Manager dialog box from the project, the Core tab will appear.

      
        8.0
      

    I'll make a .NET 4.5 C# Console Application. I'll edit the .csproj and add the TargetPlatformVersion line. I'll select Add Reference from the context menu on the References node of Solution Explorer.

    Windows Core References

    I'll add a little code to check the status of the Light Sensor on my laptop:

    LightSensor light = LightSensor.GetDefault();
    
    if (light != null)
    {
    uint minReportInterval = light.MinimumReportInterval;
    uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
    light.ReportInterval = reportInterval;

    light.ReadingChanged += light_ReadingChanged; //event hander
    }

    However, when I compile the app, I get an error on the line where I'm trying to hook up an event handler. The "+=" language sugar for adding a miulticast delegate isn't working.

    Error    1    Property, indexer, or event 
    
    'Windows.Devices.Sensors.LightSensor.ReadingChanged'
    is not supported by the language; try directly calling accessor
    methods 'Windows.Devices.Sensors.LightSensor.add_ReadingChanged
    (Windows.Foundation.TypedEventHandler
    Windows.Devices.Sensors.LightSensorReadingChangedEventArgs>)'
    or 'Windows.Devices.Sensors.LightSensor.remove_ReadingChanged
    (System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken)'

    To fix this and get the appropriate assemblies loaded within my application support calling WinRT from my Desktop Application I need to add a reference to System.Runtime and System.Runtime.InteropServices.WindowsRuntime.dll. It's in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5 on my system.

    System.Runtime.InteropServices.WindowsRuntime.dll in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5

    Now my app compiles. I'll even change out the delegate and make it a Anders lambda because that's fancy.

    light.ReadingChanged += (s, a) =>
    
    {
    Console.WriteLine(String.Format("There was light! {0}", a.Reading.IlluminanceInLux));
    };

    Now I can run my little console app, sense some light and check it out in action. Here's a screenshot showing the results of me shining a light at my laptop. You can see the Ambient LightSensor picks it up and outputs to the Console.

    The ambient light sensor reacting

    While the tooling to make non-Windows Store applications call Windows RT applications is a little manual within Visual Studio right now, the underlying ability and runtime have work very nicely for me. Hopefully these few manual setups will turn into a checkbox at some point.

    It's also nice to see the MSDN documentation includes the details about which APIs actually can be called from the Desktop and which can be called from Windows Store apps.


    This week's sponsor: Your Idea. Your App. 30 Days.




    © 2012 Scott Hanselman. All rights reserved.

    • 510 reads
    • Feed: Scott Hanselman's Computer Zen

    Post new comment

    Google Chrome Browser is a community site for users and developers of the browser.
    Google™ is a Trademark of Google Inc. All other company and product names may be trademarks of the respective companies with which they are associated.
    Google Chrome Browser site is not affiliated with or sponsored by Google Inc.
    Google Chrome Browser site is built on the Drupal open source content management system.