Home > Back-end >  C language projection
C language projection

Time:01-27

Can you explain in your own words what it means: Standard C 17 language projection for Windows Runtime API (WinRT) (examples are welcome)?

On the microsovt website, everything is so complicatedly described that I could not understand this term

CodePudding user response:

This confusion goes all the way back to Microsoft's monumental communication disaster in explaining what the Windows Runtime is. In broad terms, it is the foundational technology that underpins the "future" of the Windows API (that "future" had arrived well over a decade ago).

The Windows Runtime1 is based on an evolution of COM, inheriting one of its fundamental properties: A strict ABI contract, enabling language-agnosticism. Either side of the ABI can be written in just about any programming language.

Functionality in the Windows Runtime is deployed by way of Windows Runtime Components. WinRT components generally consist of two parts:

  • A WinMD file that describes the provided interfaces in a machine-readable way
  • A binary that implements the functionality

While possible to communicate with WinRT components right at the ABI (using either the WRL, or straight C) this can quickly become unwieldy and error prone. This is where "language projections" come into play: Generally tool- and library-based, they "project" the raw ABI into more manageable, safer abstractions for a given programming language (e.g. C , C#, or Rust).

The C /WinRT language projection provides a large array of translations, including

  • Automatic reference counting, courtesy of the com_ptr class template
  • Translation between C exceptions and ABI-compatible HRESULT error codes
  • Exposing asynchronous operations as C 20 coroutines
  • Mapping delegates to anything that provides a function call operator (such as functions, member functions, lambda expressions, etc.)
  • Seamless translations between WinRT date and time primitives and std::chrono types
  • Exposing static instance methods as static class members (even though they are technically implemented on the type's activation factory)
  • ...

In short, a language projection makes WinRT types appear as natural as possible for any given language, hiding all aspects of the ABI, with the intention of making the Windows Runtime accessible to programmers that don't (care to) know about the technology's internals.

To this day, that plan only really panned out for C# developers (which, ironically, predominantly assume that the Windows Runtime were powered by the CLR).


1 A misnomer, sorry. It doesn't contain any sort of runtime component in the traditional sense.

  • Related