Category Archives: Uncategorized

My Rust adventure begins

I have come to the point with C++/WinRT where I am largely satisfied with how it works and leverages C++ to the best of its ability. There is always room for improvement and I will continue to evolve and optimize C++/WinRT as the C++ language itself advances. But as a technology, the Windows Runtime has always been about more than just one language and we have started working on a few different projects to add support for various languages. None of these efforts could however draw me away from C++… that is until Rust showed up on my radar.

Rust is an intriguing language for me. It closely resembles C++ in many ways, hitting all the right notes when it comes to compilation and runtime model, type system and deterministic finalization, that I could not help but get a little excited about this fresh new take on language design. I have spent almost every waking moment over the last few months (that I’m not hanging out with my family) exploring, studying, and experimenting with Rust. I looked for the usual signs of a language that is not really geared for a systems programmer like myself, but found none. To the contrary, I found that while it has its own unique and dramatic learning curve, it also has the potential to solve some of the most vexing issues with C++’s relationship to WinRT. Imagine C++/WinRT without any need for IDL, faster build times, and a simple and integrated build system.

And so it is that I have started building the WinRT language projection for Rust. I’m just getting started and have much to learn, but the plan is to build complete and deep support for WinRT in a way that is natural and familiar for the Rust developer. This is not going to look very much like C++/WinRT because idiomatic Rust does not look and feel like C++, but I plan to apply the same level of rigor in producing WinRT support for Rust that is both very efficient and a joy to use.

I’ll be sharing more about my adventures with Rust right here on kennykerr.ca but if you’d like to follow along more closely, take a look at the Rust winmd parser I wrote to get things started:

https://github.com/microsoft/winmd-rs

This is largely based on the C++ winmd parser library. While certainly not complete, it has just enough in place to allow me to now spend some time exploring and laying the groundwork for the WinRT support. The plan is to turn this Rust crate into a complete winmd parser for both reading and generating winmd files. A separate Rust crate will then provide the actual support for consuming and producing WinRT APIs.

But I’m getting ahead of myself. Do let me know what you think. I’d love to hear from you. And don’t forget to check back soon as I will probably start writing about the adventures of a C++ developer learning Rust. 🙂

C++/WinRT and xlang repos

If you follow along on GitHub, you may have noticed a few changes in the C++/WinRT and xlang world. It became clear that having one repo for a variety of projects and languages just wasn’t practical. Developers interested in working on one language or library inevitably had to deal with all of it, creating an unnecessarily steep learning curve. And while we have a lot of ambitions for the xlang project, its clear that C++/WinRT remains our flagship project. To that end, and to make it easier to work with the two most popular projects under the xlang umbrella, we’ve split the projects up as follows:

C++/WinRT
Repository: https://github.com/microsoft/cppwinrt
Documentation: https://aka.ms/cppwinrt
NuGet package: http://aka.ms/cppwinrt/nuget
Visual Studio extension: http://aka.ms/cppwinrt/vsix

C++ winmd parser library
Repository: https://github.com/microsoft/winmd
NuGet package: http://aka.ms/winmd/nuget

Existing projects related to cross-platform support
Repository: https://github.com/microsoft/xlang

You may also have noticed some new GitHub repos for Java and C# language support. Obviously, we’d love to add support for every popular language, but our resources are limited. We have experimented with adding support for both. The C# project might seem a little curious given that C# currently supports WinRT directly, but we have discovered through our experience with C++/WinRT that we can provide a far better experience by separating the WinRT support from the compiler itself. On a personal note, I’m spending a lot of my time working with the Rust language and can’t wait to share more about that eventually. 😉

The Old New Thing on C++/WinRT

In case you haven’t noticed, Raymond Chen has joined me in writing about C++/WinRT. He has published a great collection of tips and tricks internally and plans to publish them all publicly as time allows. Here are a few to get you started:

Detecting whether the -opt flag was passed to cppwinrt.exe: Using __has_include

How can I determine in a C++ header file whether C++/CX is enabled? How about C++/WinRT?

Why does my C++/WinRT project get errors of the form “Unresolved external symbol void* __cdecl winrt_make_YourNamespace_YourClass(void)“?

Why does my C++/WinRT project get errors of the form ‘winrt::impl::produce‘: cannot instantiate abstract class, missing method GetBindingConnector

Why does my C++/WinRT project get errors of the form “consume_Something: function that returns ‘auto’ cannot be used before it is defined”?

Why does my C++/WinRT project get errors of the form “unresolved external symbol … consume_Something”?

Windows Runtime delegates and object lifetime in C++/WinRT

Meet C++/WinRT 2.0: resume_foreground Improvements

It turns out that resume_foreground was being a little too clever and could introduce deadlocks in some scenarios because it only suspended if not already on the dispatcher thread. This seemed like a good idea at the time, but being able to depend on stack unwinding and re-queuing turns out to be quite important for system stability especially in OS code. Consider this simple example:

fire_and_forget UpdateAsync(TextBlock block)
{
    co_await resume_background();
    hstring message = L"Hello developers!";

    co_await resume_foreground(block.Dispatcher());
    block.Text(message);
}

Here we’re performing some complex calculation on a background thread and then naturally switch to the appropriate UI thread before updating some UI control. The resume_foreground function had some cleverness that looked something like this:

auto resume_foreground(...) noexcept
{
    struct awaitable
    {
        bool await_ready() const
        {
            return m_dispatcher.HasThreadAccess(); // <-- Cleverness...
        }
        void await_resume() const {}
        void await_suspend(coroutine_handle<> handle) const { ... }
    };
    return awaitable{ ... };
};

This has been updated as follows:

auto resume_foreground(...) noexcept
{
    struct awaitable
    {
        bool await_ready() const
        {
            return false; // <-- Queue without waiting
        }
        void await_resume() const {}
        void await_suspend(coroutine_handle<> handle) const { ... }
    };
    return awaitable{ ... };
};

This is analogous to the difference between SendMessage and PostMessage in classic desktop app development. The latter will queue the work and then unwind the stack without waiting for it to complete. This unwinding the stack can be essential.

The resume_foreground function also initially only supported the CoreDispatcher tied to a CoreWindow that was originally introduced with Windows 8. A more flexible and efficient dispatcher has since been introduced. The DispatcherQueue is nice in that you can create them for your own purposes. Consider a simple console app:

using namespace Windows::System;

fire_and_forget RunAsync(DispatcherQueue queue);

int main()
{
    auto controller = DispatcherQueueController::CreateOnDedicatedThread();
    RunAsync(controller.DispatcherQueue());
    getchar();
}

Here I’m creating a private queue thread and then passing this queue object to the coroutine. The coroutine can then presumably use it to await – suspend and resume on this private thread. Another common use of the DispatcherQueue is to create a queue on the current UI thread for a traditional desktop or Win32 app.

DispatcherQueueController CreateDispatcherQueueController()
{
    DispatcherQueueOptions options
    {
        sizeof(DispatcherQueueOptions),
        DQTYPE_THREAD_CURRENT,
        DQTAT_COM_STA
    };

    ABI::Windows::System::IDispatcherQueueController* ptr{};
    check_hresult(CreateDispatcherQueueController(options, &ptr));
    return { ptr, take_ownership_from_abi };
}

Not only does this illustrate how Win32 functions may be called and incorporated into C++/WinRT projects, by simply calling the Win32-style CreateDispatcherQueueController function to create the controller and then transferring ownership of the resulting queue controller to the caller as a WinRT object, but this is precisely how you can support efficient and seamless queuing on your existing Petzold-style Win32 desktop app:

fire_and_forget RunAsync(DispatcherQueue queue);

int main()
{
    Window window;
    auto controller = CreateDispatcherQueueController();
    RunAsync(controller.DispatcherQueue());
    MSG message;

    while (GetMessage(&message, nullptr, 0, 0))
    {
        DispatchMessage(&message);
    }
}

This simple main function starts by creating a window. You can imagine this registers a window class and calls CreateWindow to create the top-level desktop window. The CreateDispatcherQueueController function is then called to create the queue controller before calling some coroutine with the dispatcher queue owned by this controller. A traditional message pump is then entered where resumption of the coroutine naturally occurs on this thread. Having done so, you can return to the elegant world of coroutines for your async or message based workflow within your app:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ... // Start on the calling thread

    co_await resume_foreground(queue);

    ... // Resume on the dispatcher thread
}

The resume_foreground will always “queue” and then unwind the stack. You can also optionally set the resumption priority:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ...

    co_await resume_foreground(queue, DispatcherQueuePriority::High);

    ...
}

But if you only care about default queuing order then you can even await the queue itself and save yourself a few keystrokes:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ...

    co_await queue;

    ...
}

For the control freaks out there, you can even detect queue shutdown and handle that gracefully:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ...

    if (co_await queue)
    {
        ... // Resume on dispatcher thread
    }
    else
    {
        ... // Still on calling thread
    }
}

The co_await expression will return true, indicating that resumption will occur on the dispatcher thread. In other words, queuing was successful. Conversely, it will return false to indicate that execution remains on the calling thread because the queue’s controller is shutting down and is no longer serving queue requests.

As you can see, you have a great deal of power at your fingertips when you combine C++/WinRT with coroutines and especially when you do some old-school Petzold style desktop app development.

And that’s all for today. I hope you enjoy using C++/WinRT!

Meet C++/WinRT 2.0: Fewer Dependencies

I’ve always loved tools like Sysinternals where there is a single executable that you can simply copy onto your dev box and run. No need for an installer or a carefully managed tree of DLLs. It just works. Well cppwinrt.exe is like that as well. From the start, you could simply copy it onto any Windows 10 machine and it would just work. Still, there’s always room for improvement. Have a look at the dependencies reported by dumpbin for version 1 of cppwinrt:

> dumpbin /dependents cppwinrt.exe

ADVAPI32.dll
SHELL32.dll
api-ms-win-core-file-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-0.dll
XmlLite.dll
api-ms-win-core-libraryloader-l1-2-0.dll
api-ms-win-core-processenvironment-l1-1-0.dll
RoMetadata.dll
SHLWAPI.dll
KERNEL32.dll
api-ms-win-core-rtlsupport-l1-1-0.dll
api-ms-win-core-heap-l1-1-0.dll
api-ms-win-core-localization-l1-2-0.dll
api-ms-win-core-timezone-l1-1-0.dll
api-ms-win-core-console-l1-1-0.dll
OLEAUT32.dll
api-ms-win-core-winrt-error-l1-1-0.dll
api-ms-win-core-winrt-error-l1-1-1.dll
api-ms-win-core-winrt-l1-1-0.dll
api-ms-win-core-winrt-string-l1-1-0.dll
api-ms-win-core-synch-l1-1-0.dll
api-ms-win-core-threadpool-l1-2-0.dll
api-ms-win-core-com-l1-1-0.dll
api-ms-win-core-com-l1-1-1.dll
api-ms-win-core-synch-l1-2-0.dll

In my defense, that’s not as bad as it looks. All of those DLLs are shipped with Windows 10 and those api-ms-win-core-xxx entries are really forwarding DLLs that support API sets. Still, there was one DLL in that list that caused a bit of trouble. RoMetadata.dll provides the implementation of the metadata parser shipped with the operating system. This is the implementation that practically everyone uses either directly or indirectly. We first hit a snag with this because the rather locked down server SKU that the build engineers at Microsoft wanted to use didn’t include this DLL. That turned out to be a Windows setup bug, but it got me thinking more about dependencies.

With C++/WinRT 2.0 I finally started writing a completely independent metadata parser in standard C++ to avoid this dependency and solve all kinds of trouble with this clunky old parser. A few guys on the team chipped in and this parser is now the foundation for all of our modern tooling. I then also ditched the forwarding DLLs to the point where dumpbin now reports a slightly smaller set of dependencies for version 2 of cppwinrt:

> dumpbin /dependents cppwinrt.exe

KERNEL32.dll
ADVAPI32.dll
XmlLite.dll
SHLWAPI.dll

The fun thing about this is that all of those DLLs are available, not only on Windows 10, but all the way down to Windows 7 and even Windows Vista. That means if you happen to have some crazy old build server running Windows 7, well then you can still run cppwinrt to generate the C++ headers for your project. And if you actually want to run C++/WinRT on Windows 7 you can even do that with a bit of work as well.

And that’s all for today. I hope you enjoy using C++/WinRT!

Meet C++/WinRT 2.0: Async Timeouts Made Easy

C++/WinRT took a big bet on C++ coroutines and that bet has paid off. Coroutines are in C++20 and the effect on writing concurrency code in C++ has been transformational. C++/WinRT was also the primary driver for the adoption of coroutines within Windows. Still, there are times when the fact that some API call is async is completely irrelevant and all you want is the result here and now. For that reason, C++/WinRT’s implementation of the various WinRT async interfaces has always sported a get function, similar to what std::function provides:

int main()
{
    IAsyncAction async = ...
    async.get();
    puts("done!");
}

This get function will block indefinitely for the async object to complete. Async objects tend to be very short-lived so this is often all you need. There are however times when this really doesn’t cut it and you need to abandon the wait after some time has elapsed. Writing this has always been possible, thanks to the building blocks provided by WinRT, but it has never been easy. Well C++/WinRT now makes it trivial by providing a wait_for function, again similar to what std::function provides:

int main()
{
    IAsyncAction async = ...

    if (async.wait_for(5s) == AsyncStatus::Completed)
    {
        puts("done");
    }
}

The wait_for in this example (using std::literals) will wait around 5 seconds before checking completion. If the comparison is favorable then you know that the async object completed successfully and you’re done. If you are waiting for some result, then you can simply follow that with a call to the get function to retrieve the result:

int main()
{
    IAsyncOperation<int> async = ...

    if (async.wait_for(5s) == AsyncStatus::Completed)
    {
        printf("result %d\n", async.get());
    }
}

Since the async object has already completed, the get function will return the result immediately without any further wait. As you can see, the wait_for function returns the state of the async object. You can thus use this for more fine-grained control:

switch (async.wait_for(5s))
{
case AsyncStatus::Completed:
    printf("result %d\n", async.get());
    break;
case AsyncStatus::Canceled:
    puts("canceled");
    break;
case AsyncStatus::Error:
    puts("failed");
    break;
case AsyncStatus::Started:
    puts("still running");
    break;
}

As I mentioned, AsyncStatus::Completed means the async object completed successfully and you may call the get function for any result.

AsyncStatus::Canceled means the async object was canceled. Note that the cancellation is typically requested by the caller, so it would be rare to handle this state. Typically, a cancelled async object is simply discarded.

AsyncStatus::Error means the async object has failed in some way. You may call the get function to rethrow the exception if so desired.

Finally, AsyncStatus::Started means that the async object is still running. This is where it gets tricky. The WinRT async pattern does not allow multiple waits or waiters. That means that you cannot call wait_for in a loop. If the wait has effectively timed-out, you are left with a few choices. You may abandon the object or you may poll its status before calling get to retrieve any result, but it’s best just to discard the object at this point.

And that’s all for today. I hope you enjoy using C++/WinRT!