Top Related Projects
Rust for Windows
Windows Implementation Library
This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
C++ Library Manager for Windows, Linux, and MacOS
The new Windows Terminal and the original Windows console host, all in the same place!
Quick Overview
C++/WinRT is a modern C++ language projection for the Windows Runtime (WinRT) APIs. It provides a standard C++17 interface for Windows APIs, allowing developers to use modern C++ features while interacting with Windows platform capabilities.
Pros
- Offers better performance compared to other language projections for WinRT
- Provides a more natural C++ programming experience for Windows development
- Supports modern C++ features and idioms
- Enables header-only consumption of Windows Runtime APIs
Cons
- Steeper learning curve for developers not familiar with modern C++ or WinRT concepts
- Limited to Windows platform development
- May require more verbose code compared to other language projections in some scenarios
- Documentation can be challenging for beginners
Code Examples
- Creating a simple Windows Runtime component:
#include <winrt/Windows.Foundation.h>
struct __declspec(uuid("...")) __declspec(novtable) IExample : winrt::Windows::Foundation::IInspectable
{
virtual int32_t __stdcall Method() = 0;
};
struct Example : winrt::implements<Example, IExample>
{
int32_t __stdcall Method() override
{
return 42;
}
};
- Using Windows Runtime APIs:
#include <winrt/Windows.UI.Xaml.Controls.h>
void CreateButton()
{
winrt::Windows::UI::Xaml::Controls::Button button;
button.Content(winrt::box_value(L"Click me!"));
button.Click([](auto&&, auto&&) { /* Handle click */ });
}
- Asynchronous operations:
#include <winrt/Windows.Web.Syndication.h>
winrt::Windows::Foundation::IAsyncAction GetFeedAsync()
{
winrt::Windows::Web::Syndication::SyndicationClient client;
auto feed = co_await client.RetrieveFeedAsync(winrt::Windows::Foundation::Uri(L"https://example.com/feed"));
// Process feed
}
Getting Started
- Install Visual Studio 2019 or later with C++ and Windows 10 SDK support.
- Create a new C++ Windows Universal project.
- Add the following to your project's
pch.h
:
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
- Start using C++/WinRT in your code:
#include "pch.h"
#include <winrt/Windows.UI.Xaml.Controls.h>
using namespace winrt;
using namespace Windows::UI::Xaml;
struct App : ApplicationT<App>
{
void OnLaunched(LaunchActivatedEventArgs const&)
{
Window window = Window::Current();
Controls::TextBlock text;
text.Text(L"Hello, C++/WinRT!");
window.Content(text);
window.Activate();
}
};
Competitor Comparisons
Rust for Windows
Pros of windows-rs
- Written in Rust, providing memory safety and modern language features
- Easier to use for Rust developers already familiar with the ecosystem
- More idiomatic Rust code and better integration with Rust tooling
Cons of windows-rs
- Less mature and potentially less stable than C++/WinRT
- May have limited support for some advanced Windows API features
- Smaller community and fewer resources compared to C++/WinRT
Code Comparison
cppwinrt:
#include <winrt/Windows.Foundation.h>
using namespace winrt::Windows::Foundation;
IAsyncAction DoWorkAsync()
{
co_await 1s;
co_return;
}
windows-rs:
use windows::Foundation::IAsyncAction;
fn do_work_async() -> IAsyncAction {
async_block! {
sleep(Duration::from_secs(1)).await;
}
}
Both examples demonstrate asynchronous programming, but windows-rs uses Rust's async/await syntax, while cppwinrt uses C++20 coroutines. The windows-rs code is more idiomatic Rust, while cppwinrt follows C++ conventions.
Windows Implementation Library
Pros of WIL
- Broader scope, offering utilities for various Windows development scenarios
- Easier integration with existing codebases, as it's header-only
- More flexible, with less opinionated abstractions
Cons of WIL
- Less focused on modern C++ idioms compared to C++/WinRT
- May require more manual management of resources and COM interfaces
- Potentially steeper learning curve due to its wider range of features
Code Comparison
WIL:
#include <wil/resource.h>
wil::unique_handle fileHandle = CreateFile(...);
THROW_IF_HANDLE_INVALID(fileHandle);
C++/WinRT:
#include <winrt/Windows.Storage.h>
auto file = co_await StorageFile::GetFileFromPathAsync(L"path");
Summary
WIL provides a broader set of utilities for Windows development, while C++/WinRT focuses on modernizing Windows Runtime APIs. WIL is more flexible and easier to integrate into existing projects, but C++/WinRT offers a more cohesive and modern C++ experience for working with Windows APIs. The choice between them depends on the specific project requirements and the developer's familiarity with modern C++ concepts.
This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Pros of DirectXShaderCompiler
- Specialized for shader compilation, offering advanced optimization techniques
- Supports multiple shader models and DirectX versions
- Provides a robust command-line interface for integration into build pipelines
Cons of DirectXShaderCompiler
- Limited to DirectX shader compilation, less versatile than CppWinRT
- Steeper learning curve for developers not familiar with shader programming
- Larger repository size and potentially longer build times
Code Comparison
DirectXShaderCompiler:
float4 PSMain(float4 position : SV_POSITION) : SV_TARGET
{
return float4(1.0, 0.0, 0.0, 1.0);
}
CppWinRT:
struct App : implements<App, IFrameworkViewSource, IFrameworkView>
{
IFrameworkView CreateView() { return *this; }
void Initialize(CoreApplicationView const&) {}
};
Summary
DirectXShaderCompiler is a specialized tool for DirectX shader compilation, offering advanced optimization and support for various shader models. It's ideal for graphics programming but has a narrower focus compared to CppWinRT. CppWinRT, on the other hand, provides a more general-purpose solution for Windows Runtime development, offering broader applicability across different types of Windows applications. The choice between the two depends on the specific needs of your project, with DirectXShaderCompiler being more suitable for graphics-intensive applications and CppWinRT for general Windows app development.
C++ Library Manager for Windows, Linux, and MacOS
Pros of vcpkg
- Broader scope: Manages dependencies for multiple languages and platforms, not just C++/WinRT
- Larger ecosystem: Supports a vast array of libraries and packages
- Cross-platform: Works on Windows, Linux, and macOS
Cons of vcpkg
- More complex setup: Requires additional configuration compared to CppWinRT
- Potential overhead: May include unnecessary dependencies for simple projects
- Learning curve: Steeper for developers unfamiliar with package management systems
Code Comparison
vcpkg:
vcpkg install boost:x64-windows
vcpkg integrate install
CppWinRT:
#include <winrt/Windows.Foundation.h>
using namespace winrt::Windows::Foundation;
Summary
vcpkg is a versatile package manager for C++ libraries, offering a wide range of dependencies across multiple platforms. It provides more flexibility but may require additional setup and learning. CppWinRT, on the other hand, is specifically designed for Windows Runtime APIs, offering a more streamlined experience for Windows development but with a narrower focus.
The new Windows Terminal and the original Windows console host, all in the same place!
Pros of Terminal
- More user-facing application, providing a modern terminal experience for Windows users
- Extensive customization options, including themes, key bindings, and profiles
- Active community with frequent updates and feature additions
Cons of Terminal
- Larger codebase and potentially more complex for contributors
- Focused on Windows platform, limiting cross-platform compatibility
- May require more system resources compared to simpler terminal emulators
Code Comparison
Terminal (C++):
void TerminalPage::_HandleOpenNewTabDropdown(const IInspectable& /*sender*/, const ActionEventArgs& args)
{
_OpenNewTab(std::nullopt);
}
CppWinRT (C++/WinRT):
IAsyncAction MainPage::ButtonClickHandler(IInspectable const&, RoutedEventArgs const&)
{
co_await _viewModel.DoSomethingAsync();
}
Key Differences
- Terminal is a full-fledged application, while CppWinRT is a language projection for Windows Runtime APIs
- Terminal focuses on user experience and customization, whereas CppWinRT aims to simplify Windows API development
- CppWinRT has broader applicability across Windows development, while Terminal is specific to command-line interfaces
Use Cases
- Choose Terminal for a modern, customizable Windows terminal experience
- Opt for CppWinRT when developing Windows applications with modern C++ and requiring Windows Runtime API access
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The C++/WinRT language projection
C++/WinRT is an entirely standard C++ language projection for Windows Runtime (WinRT) APIs, implemented as a header-file-based library, and designed to provide you with first-class access to the modern Windows API. With C++/WinRT, you can author and consume Windows Runtime APIs using any standards-compliant C++17 compiler.
- Documentation: https://aka.ms/cppwinrt
- NuGet package: http://aka.ms/cppwinrt/nuget
- Visual Studio extension: http://aka.ms/cppwinrt/vsix
- Wikipedia: https://en.wikipedia.org/wiki/C++/WinRT
Building C++/WinRT
Don't build C++/WinRT yourself - just download the latest version here: https://aka.ms/cppwinrt/nuget
Working on the compiler
If you really want to build it yourself, the simplest way to do so is to run the build_test_all.cmd
script in the root directory. Developers needing to work on the C++/WinRT compiler itself should go through the following steps to arrive at an efficient inner loop:
- Open a dev command prompt pointing at the root of the repo.
- Open the
cppwinrt.sln
solution. - Choose a configuration (x64, x86, Release, Debug) and build projects as needed.
If you are working on an ARM64 or ARM specific issue from an x64 or x86 host, you will need to instead:
- Open the
cppwinrt.sln
solution - Build the x86 version of the "cppwinrt" project first
- Switch to your preferred configuration and build the test binaries and run them in your test environment
Comparing Outputs
Comparing the output of the prior release and your current changes will help show the impact of any updates. Starting from a dev command prompt at the root of the repo after following the above build instructions:
- Run
build_projection.cmd
in the dev command prompt - Run
build_prior_projection.cmd
in the dev command prompt as well - Run
prepare_versionless_diffs.cmd
which removes version stamps on both current and prior projection - Use a directory-level differencing tool to compare
_build\$(arch)\$(flavor)\winrt
and_reference\$(arch)\$(flavor)\winrt
Top Related Projects
Rust for Windows
Windows Implementation Library
This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
C++ Library Manager for Windows, Linux, and MacOS
The new Windows Terminal and the original Windows console host, all in the same place!
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot