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

http://feeds.feedblitz.com/~/34645669/0/scotthanselman~How-to-call-WinRT-APIs-in-Windows-from-C-Desktop-Applications-WinRT-Diagram.aspx

“I was trying to access some of the sensors that are built into this Intel Ultrabook 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 Sensor and Location Platform that is built into Windows 8 itself. 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 podcast a few months ago that really cleared things up but I’ve always found all the various diagrams that attempted to explain how things fit together WAY TO COMPLEX.

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 app for babies.

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

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 lists out all the WinRT APIs for Windows 8 that are able to be called from the Desktop. I can specifically check the LightSensor class itself within the documentation and make sure it’s allowed to be called from Desktop applications.

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, somewhat obscurely:

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.

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)’

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.

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.

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. Begin your 30-day journey to creating a Windows Store app or game for Windows 8 or Windows Phone today.

© 2012 Scott Hanselman. All rights reserved.”

-Sent from Weave for Windows Phone 7

Posted in Uncategorized | Leave a comment

C++ for C# Developers with “C++ Succinctly!

http://channel9.msdn.com/coding4fun/blog/C-for-C-Developers-with-C-Succinctly

“Today’s topic comes via a suggestion via Andy Dunn. I’ve been following and blogging about these eBooks on my personal blog, but this one I skipped. I mean, I wasn’t a C++ dev, and so why would I care about a C++ book? sigh… Sometimes I hate being stupid.

When Andy’s email came in I took another look at this book and quickly realized what a mistake I’d made in ignoring it. Why? Well if you’re like me you’ve been seeing a good deal of C++ chatter coming out of Microsoft (and I admit, this blog too).

So what’s a C# dev to do in this C++ resurgence? For starters get this free, (reg-ware) eBook!C++ Succinctly now available!

Over the summer I worked with SyncFusion to create an eBook based off of my C# to C++ guide for their free Succinctly Series of eBooks. Today the result, C++ Succinctly, was published for download. It is a free (registration required; they make tools and libraries for .NET development so you might get an occasional email from them – I’ve been signed up for a few months and have had maybe 3 emails total so it’s not horrible super spam or anything ) and you can download it as a PDF or a Kindle .MOBI file (or both).

I’m excited with how it turned out and enjoyed working with the people at SyncFusion. The book contains a total of 20 code samples, which you can download from BitBucket (there’s a link very early in the book). Almost all of the code is also inline in the book itself so that you don’t need to worry about flipping back and forth between your dev machine and your eReader (but if you want to try to understand a concept better, you can easily download the code, open it up in VS 2012, and play around with it to see what happens when you tinker with things).

The code does require Visual Studio 2012 because of its expanded support for C++11 features and since I wrote all of the samples as Console programs for clarity and compactness, you will need a version that supports C++ desktop development (currently VS 2012 Pro, Premium, or Ultimate). Sometime this Fall, Microsoft will be releasing Visual Studio 2012 Express for Windows Desktop which should provide a free way to use the samples.

That said, I tested all of the samples with MinGW and only the StorageDurationSample will not compile with it due to the thread-local storage code…C++ Succinctly

C++ for C# Developers.

C++ Succinctly was written to help professional C# developers learn modern C++ programming. The aim of this book is to leverage your existing C# knowledge in order to expand your skills. Whether you need to use C++ in an upcoming project, or simply want to learn a new language (or reacquaint yourself with it), this book will help you learn all of the fundamental pieces of C++ so you can begin writing your own C++ programs.

Table of ContentsTypes Namespaces Functions and Classes Storage Duration Constructors, Destructors, and Operators Resource Acquisition is Initialization Pointers, References, and Const-Correctness Casting in C++ Strings C++ Language Usages and Idioms Templates Lambda Expressions C++ Standard Library Visual Studio and C++

As you can kind of see in this snap of the page thumbnails, this isn’t some kind marketing brochure.

123 pages of help for the C# dev in what looks like could be a new C++ world…

While you’re there you HAVE to check out the other awesome Succinctly books SyncFusion is offering. Head over to their Technology Resource Portal and get your Succinctly fix fixed!

Here’s the Succinctly list as it stands today;Knockout.js Succinctly C++ Succinctly ASP.NET MVC 4 Mobile Websites Succinctly LightSwitch Succinctly JavaScript Succinctly HTTP Succinctly PDF Succinctly Git Succinctly jQuery Succinctly

That should be enough to keep you busy for a few days at least.”

-Sent from Weave for Windows Phone 7

Posted in Uncategorized | Leave a comment

Instapaper delivered to your Kindle changes how you consume web content – Plus IFTTT, blogs and more !

http://feeds.feedblitz.com/~/34869023/0/scotthanselman~Instapaper-delivered-to-your-Kindle-changes-how-you-consume-web-content-Plus-IFTTT-blogs-and-more.aspx

“I’ve talked about Instapaper before when I tease folks of having 42 tabs open.

Remember that “open in new tab” rarely means “read it later.” It usually means “use up memory and let this page run in the background until eventually declare tab-bankruptcy and close them all.”

So if Open In New Tab doesn’t mean Read Later, what does? Why, READ LATER does! This gets even better when you combine a Read Later tool like Instapaper with an Amazon Kindle like my new Kindle Paperwhite (I reviewed the Paperwhite last week.) Inserting a Kindle into your Life’s Workflow

Here’s the idea. You get a bunch of links that flow through your life all week long. These are often in the form of what I call “long-form reading.” Hackernews links, NYTimes studys, academic papers, etc. Some folks make bookmarks, have folders called “Links” on their desktops, or email themselves links.

I have these websites, papers and interesting links rolled up and delivered automatically to my Kindle every week. Think about how amazing that is and how it can change your relationship with content on the web. The stress and urgency (and open tabs) are gone. I am naturally and organically creating a personalized book for weekend reading.

I have a bookmarklet from Instapaper that says “Read Later” on my browser toolbar. I’ve put it in every browser I use, even Mobile Safari. I’ve also logged into Instapaper from all my social apps so that I can Read Later from my iPhone Twitter Client for example. You’d be surprised how many apps support Instapaper once you start looking for this.

What this means it is that Instapaper is ready and waiting for me in every location where an interesting piece of long-form reading could present itself. I don’t stress, I click Read Later and the document is shipped off to Instapaper.

I even made a flowchart a few years back. You can get more details on that in my blog post Two Must-Have Tools for a More Readable Web. Instapaper delivered to your Kindle

So you’re building a queue of links that is sent to Instapaper. Perhaps you’ve tried this before but then never visited the Instapaper App or Website. This is a common complaint and why I like document delivery to the Kindle. I use my Kindle all the time so I appreciate a “no clicks required” workflow. If books show up on my Kindle I’ll read them.

Just visit http://www.instapaper.com/user/kindle once you’ve got an Instapaper account and put in your Kindle’s email address. Did you know every Kindle has one? It’s either something@free.kindle.com for free WiFi delivery or just something@kindle.com for 3G delivery with a small fee. I use the free one. You can find out your Kindle’s Email Address here under Personal Document Settings.

The key is to allow your Kindle to receive email from the unique Instapaper email address. It’s a whitelist.

Then, back over in the Instapaper Settings, I set a delivery time if at least 5 things are in the “book”:

Pulling Links and Content from other Locations with IFTTT

Perhaps you pull your content from elsewhere, or you Like things on Facebook, put them in Dropbox, email them to a special address or something else. You can use If This Then That as the social workflow glue to route those links to Instapaper – and ultimately to your Kindle!

For example, I also use the Delicious social bookmarking service to hold things I want to save. But, I also want to read them and I don’t want to stop using Delicious just because I use Instapaper. Instead, I use an IFTTT Recipe to take newly bookmarked things and send them to Instapaper (and my Kindle!) as well.

Here’s my Delicious to Kindle Recipe. You can make any recipe you want to pull links from wherever you find them and send them into your long-form reading queue.

You can even have blogs – like this one! – sent automatically to your Kindle via Instapaper with an IFTTT recipe like this:

The possibilities are endless. Conclusion

It can’t be overstated how useful this is if you have a Kindle. Rather than opening “guilt-tabs” that you’ll never read, have them delivered to yourself in a way that will encourage you TO READ THEM!

If your system isn’t working for you, change it. If you already have a system that works, well, great job making it all the way to the end of this blog post!

For more personal productivity ideas watch my video on Scaling Yourself and visit the Productivity section of this blog.

This Week’s Sponsor: Your Idea. Your App. 30 Days. Begin your 30-day journey to creating a Windows Store app or game for Windows 8 or Windows Phone today.

© 2012 Scott Hanselman. All rights reserved.”

-Sent from Weave for Windows Phone 7

Posted in Uncategorized | Leave a comment

Moving From Coding To Model-Driven Development: Hands-On with MetaEdit , Part 2

http://www.infoq.com/presentations/MDD-MetaEdit-2?utm_source=infoqEmail

Posted in Uncategorized | Leave a comment

Contre Jour

http://www.contrejour.ie/#fbid=c5fmSBzMsMG

Posted in Uncategorized | Leave a comment

IdeaBlade DevForce Website – Home Page

http://www.ideablade.com/

Posted in Uncategorized | Leave a comment

Windows Azure Mobile Services: New support for iOS apps, Facebook/Twitter/Google identity, Emails, SMS, Blobs, Service Bus and more – ScottGu’s Blog

http://weblogs.asp.net/scottgu/archive/2012/10/16/windows-azure-mobile-services-new-support-for-ios-apps-facebook-twitter-google-identity-emails-sms-blobs-service-bus-and-more.aspx

Posted in Uncategorized | Leave a comment

TalentLMS – Cloud based, Lean and Complete LMS with an emphasis on Usability and easy Course creation

http://talentlms.com/tour

Posted in Uncategorized | Leave a comment

MCQ Results – Instructions

http://medprog.fmhs.auckland.ac.nz/mcqresults/index.php/site/page?view=about

Posted in Uncategorized | Leave a comment

WinRT XAML Toolkit – Home

http://winrtxamltoolkit.codeplex.com/

Posted in Uncategorized | Leave a comment