Windows Runtime

The Modern C++ Library provides a Standard C++ language projection for the Windows Runtime. No need to use non-standard language extensions or cryptic ABI interfaces and functions. With Modern you get a power library for the Windows Runtime, whether you want the convenience of a class-based API or the ability to go under-the-hood for more control.

Here’s a simple console app using the Windows Runtime’s Uri class. You can see how simple it is to find classes within namespaces, creating a Uri object, and calling some of its methods. Of course, this is Standard C++ so the Port, Domain, and Path properties are projected as methods:

#include <Modern.h>
#include <stdio.h>

using namespace Modern::Windows::Foundation;

int main()
{
    Modern::Initialize();

    Uri uri(L"https://moderncpp.com/about/");

    printf("port:   %d\n",  uri.Port());
    printf("domain: %ls\n", uri.Domain().Buffer());
    printf("path:   %ls\n", uri.Path().Buffer());
}

Here’s the output:

port:   80
domain: moderncpp.com
path:   /about/

How about querying for any connected mouse devices? Again, you can simply use the Windows Runtime’s MouseCapabilities class:

using namespace Modern::Windows::Devices::Input;

MouseCapabilities caps;

if (0 < caps.MousePresent()) // at least one mouse!
{
    printf("buttons: %d\n", caps.NumberOfButtons());
}

Want to register for an event? You can simply use a lambda expression:

ICoreWindow window = CoreWindow::GetForCurrentThread();

window.KeyUp([](ICoreWindow const & sender,
                IKeyEventArgs const & args)
{
    TRACE(L"key: %d\n", args.VirtualKey());
});

You might however want more control over the construction of objects and the choice of interfaces. Rather than using the class abstraction, you might prefer to activate an instance directly:

// MouseCapabilities caps;
auto caps = ActivateInstance<MouseCapabilities>();

The ActivateInstance function activates the given Windows Runtime class and returns the default interface. You could eschew the auto keyword and be more explicit:

IMouseCapabilities caps = ActivateInstance<MouseCapabilities>();

if (0 < caps.MousePresent())
{
    printf("buttons: %d\n", caps.NumberOfButtons());
}

As you can see, ActivateInstance returns the IMouseCapabilities interface. Of course, this is the Modern C++ Library so there aren’t any pointers or -> operators to contend with. Everything about the Modern library exudes value and move semantics and shields the developer from pointers and reference counting.

Not satisfied with the default interface? No problem, you can tell ActivateInstance that you’d like a specific interface:

auto caps = ActivateInstance<MouseCapabilities, IMouseCapabilities>();

And the same goes for activation factories. Consider a Windows Store app. Given an implementation of the IFrameworkViewSource interface, you must call CoreApplication’s Run method:

CoreApplication::Run(source);

Run is a static method of the Windows Runtime’s CoreApplication class. Of course this is just an abstraction and you might opt to get the activation factory for the CoreApplication class directly:

// CoreApplication::Run(source);
auto app = GetActivationFactory<CoreApplication, ICoreApplication>();
app.Run(source);

What if you’re in a bind and really need the underlying ABI interfaces? Not a problem, simply use the -> operator to access an object’s ABI methods or the GetAbi method to return an ABI pointer:

// app.Run(source);
HRESULT hr = app->Run(source.GetAbi());

Of course, you don’t need to activate Windows Runtime classes or get activation factories directly. You can simply use the class-based projection and the many conveniences provided by the Modern library. But it sure does help to know that it’s there when you need it. Writing a Windows Store app can be as simple as this:

struct View : FrameworkView<View>
{
};

struct ViewSource : FrameworkViewSource<ViewSource>
{
    IFrameworkView CreateView()
    {
        return View();
    }
}; 

int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
    Initialize();

    CoreApplication::Run(ViewSource());
}

Want to override one of IFrameworkView’s methods? No problem:

struct View : FrameworkView<View>
{
    void Initialize(ICoreApplicationView const & view)
    {
        view.Activated([](ICoreApplicationView const & sender,
                          IActivatedEventArgs const &)
        {
            sender.CoreWindow().Activate();
        });
    }
};

IFrameworkView’s Initialize methods is responsible for responding to the ICoreApplicationView’s Activated event to activate the app’s CoreWindow. Here again you see how convenient it is to use this Standard C++ language projection as you can simply use a lambda expression with a minimum of syntactic overhead.

The Modern C++ Library unleashes the Windows Runtime for Standard C++ developers with incredible convenience and the power tools you need.