Search code examples
c++windows-runtimec++-winrt

How do you create a classic win32 application window with WinRT?


What's the C++ WinRT equivalent for what in the Win32 API would be registering a window class, creating a window and then keeping it alive via a message pump loop?

I'm currently looking at and reading the documentation for WinRT because I wanted to learn how to do all of the stuff I used to do in Win32 the modern C++ way.

My experience has been awful so far and I'm just gonna admit straight out that I'm not getting it.

I tried this but since I'm not running in a container it seems like the CoreWindow for the thread hasn't been created yet.

    int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
    {
        winrt::init_apartment(winrt::apartment_type::single_threaded);
        winrt::Windows::UI::Core::CoreWindow window = winrt::Windows::UI::Core::CoreWindow::GetForCurrentThread();

        window.Activate();

        auto dispatcher = window.Dispatcher();

        using DispatcherOptions = winrt::Windows::UI::Core::CoreProcessEventsOption;
        const DispatcherOptions options = DispatcherOptions::ProcessUntilQuit;

        dispatcher.ProcessEvents(options);
    }

Solution

  • C++/WinRT is the Modern C++ way of using Windows Runtime (a.k.a. WinRT) APIs. These APIs are derived from IInspectable, which itself derives from IUnknown. Other than the winrt::com_ptr for COM objects, it doesn't really offer much for classic Win32 APIs.

    You can certainly use C++/WinRT to consume Windows Runtime APIs from a Win32 classic application, but there's no such thing as a 'CoreWindow' for Win32 classic programs. All of Windows::UI::CoreWindow is related to Universal Windows Platform (UWP) apps.

    See Microsoft Docs