Windows Runtime with C++/C#: Anatomy of a WinRT Class

Sunday, December 30, 2012

The Windows Runtime (WinRT) is based on COM (I referred to it in the past as a “better COM”), which means every method and property must be part of an interface. Also, COM does not support static members (only instance members) and does not easily support parameterized constructors. Inheritance is again an issue in classic COM – the closest thing is COM aggregation, and that’s not really inheritance in the usual sense of the word.Using C++/CX or a .NET language allows creating WinRT types that support methods, properties, constructors and static members, and even events (another feature that is...

Windows Runtime: Where did TemplatePart go?

Thursday, December 13, 2012

In WPF and Silverlight, controls define their named parts (those that have them) using the TemplatePart attribute. For example, here’s the class definition for the WPF ProgressBar control:  public class ProgressBar : RangeBase{This indicates to those wanting to replace the control template which named parts are understood (and searched for) by the control. XAML based tools can also use this information to provide better user experience for authoring templates.In WinRT, the TemplatePart attribute is defined, but looking at control metadata – it...

BUILD 2012, Here We Come!

Sunday, October 28, 2012

Microsoft’s BUILD conference is starting in the coming Tuesday, at Microsoft campus in Redmond. I’m proud to say that 11 (yes, eleven!) guys from CodeValue (myself included) will be attending.If you happen to be there, feel free to approach us – we’ll be wearing distinctive shirts, with the CodeValue name and logo.I will do my best to provide blog coverage for each conference day, so stay tuned!

Windows 8 Store apps with C++/CX: thoughts & tips

Friday, October 5, 2012

I’ve been working on Windows 8 apps lately using C++, not C#. I’ve been doing a lot of C# work in the past few years, and I must admit I love the elegance of C# and the productivity of .NET, not to mention the powerful toolset bound with Visual Studio. Still, ever since WinRT was introduced, the idea of using native code only had its appeal. Even if the app does not require special libraries, such as DirectX or C++ AMP, native code has less overhead and lower memory consumption compared to a .NET app.Naturally, I was using the...

Using C++/CX in Desktop apps

Saturday, September 29, 2012

In my first and second post on using WinRT in a desktop app, we’ve used the raw API and then WRL to create and access WinRT objects. It would be easier to access WinRT using the new C++/CX extensions. Can we do that from a desktop app? Let’s give it a try.We’ll start with a regular Win32 Console application project. The first thing we need to do is to enable the C++/CX extensions. Open project properties and navigate to the C/C++ / General node and set “Consume Windows Runtime Extension” to Yes:Building the project now causes the compiler to...

Accessing WinRT From Desktop apps (Part 1)

Thursday, September 13, 2012

The Windows Runtime (WinRT) is the underlying runtime for Windows 8 Store Apps (“Metro”), but some of it can be actually used outside the Metro environment, in regular desktop apps, such as pure Win32, MFC, etc.There are several ways to go about it; most of the time we’ll use the Windows Runtime Library (WRL) to help out with some of the low level details. Or, for a true high level abstraction, we can use the C++/CX extensions to the C++ language (making our code non-standard). But, just for kicks, let’s see how we can access WinRT types with no...

Can Windows 8 Apps Share Memory?

Tuesday, August 21, 2012

Windows 8 Applications (formerly Metro) run inside a sandbox, making communicating with other application difficult at best. There is no easy way to do it, and for good reason, too. When one app is running, all other may be suspended, so what’s the point of communicating anyway? Furthermore, there is no guarantee that the other app even exists on the machine.Still, suppose we wanted to share some information (I’m not talking about the standard share contract) via (say) shared memory. Can we achieve that?Flipping to the documentation of the CreateFileMapping Win32 API shows that unfortunately this is only available...

Console Calculator with Roslyn (Part 2)

Wednesday, August 8, 2012

In the first part we created a simple enough calculator, but it lacked two features I wanted to have:1. work with degrees or radians in trigonometric functions.2. allow simple variables to be used without first declaring them.Let’s see how we can implement these features, starting with the first.Trigonometric functions work in radians, which is sometimes inconvenient.What we need is a way to change the parameter to the trigonometric functions by multiplying it by PI/180 if degrees was requested.First, we’ll create a simple state managing class for the calculator with just one property: class CalculatorOptions {     public...
no comments

Console Calculator with Roslyn (Part 1)

Monday, August 6, 2012

A while back I create a Console Calculator project (on CodePlex) that used manual expression parsing to “understand” a mathematical expression and resolve it. As parsing was involved, this wasn’t a trivial undertaking. What about implementing something similar with Roslyn?Roslyn is Microsoft’s “Compiler as a Service” initiative, now in its second CTP. It provides a powerful framework for examining and manipulating code (among other services). Roslyn can be installed on VS 2010 SP1 or VS 2012 RC. Let’s create our simple calculator.Fist, install Roslyn. Next, open Visual Studio 2010 or 2012 and create a new Roslyn Console project (snapshot...
no comments

WPF 4.5: Accessing bound collections on non UI threads

Saturday, July 21, 2012

The single threaded behavior of WPF (and other UI technologies) requires that anything tied to the user interface be manipulated on the UI thread, incuding data bound objects. There are several ways to do that, assuming the code is on a non-UI thread, such as using Dispatcher.(Begin)Invoke, capturing and using the current SynchronizationContext, etc.Specifically, if some collection is data bound, items cannot be added or removed from it from a non-UI thread. WPF 4 (and earlier) throws an exception, because the data binding mechanism expects to be notified of changes (e.g. ObservableCollection<T>) on the UI thread.One of the improvements...
no comments