News

Microsoft Opens Up Old Win32 APIs to C# and Rust, More Languages to Come

Microsoft is opening up old Win32 APIs long used for 32-bit Windows programming, letting coders use languages of their choice instead of the default C/C++ option or individual workarounds.

As its name bespeaks, Win32 is the 32-bit API for versions of Windows from 95 and later.

Although Win32 APIs could previously be used in other languages besides C/C++, that required workarounds -- wrappers or bindings -- such as the familiar P/Invoke scheme for C#. However, using P/Invoke and its Rust counterpart, winapi-rs, is cumbersome because the individual offerings must be manually maintained, making it hard to keep up sustained coverage. What's more, such a scheme doesn't translate to other languages.

To simplify and automate the process, Microsoft today (Jan. 21) introduced its win32metadata project, along with a couple of initial Win32 language projections. A programming language projection is a subsystem -- variously described as a set of wrappers or an adapter -- that fosters development using the APIs of a platform (Win32 in this case) in a natural and familiar way for the target language.

The metadata project eases the creation of language projections by providing a complete description of the Win32 API surface so it can be automatically projected to any language, which Microsoft said improves correctness and minimizes maintenance. Thus these metadata descriptions aren't meant to be consumed directly by developers, who instead will use the language projections that in turn consume the metadata and project the APIs into the natural patterns of specific languages.

Calling CreateFile via PInvoke Static Class with C#/Win32 in Animated Action
[Click on image for larger, animated GIF view.] Calling CreateFile via PInvoke Static Class with C#/Win32 in Animated Action (source: Microsoft).

"Win32 APIs have existed for a long time, so accurately describing all of them will take some iteration," Microsoft said in a blog post. "We will be developing this tooling in the open and welcome community contributions to ensure an accurate representation of the Win32 API surface that will benefit all languages."

In the meantime, Microsoft introduced two initial language projections in preview:

  • C#/Win32: "Built in partnership with Andrew Arnott, the owner of the PInvoke project for .NET, C#/Win32 parses the metadata and generates the P/Invoke wrappers required to call the APIs you care about."
  • Rust: "The Rust language projection follows in the tradition established by C++/WinRT of building language projections for Windows using standard languages and compilers, providing a natural and idiomatic way for Rust developers to call Windows APIs. The windows crate lets you call any Windows API using code generated on the fly directly from the metadata, enabling you to call the APIs as if they were just another Rust module."

Going forward, Microsoft said it's already working on a Modern C++ projection. The company said, "True, the Windows SDK already releases headers that can be consumed from C++, but those headers have some severe historical and compatibility constraints that hamper C++ developers that wish to enjoy modern language features."

In creating more language projections, the company said it will follow these principles:

  • Language projections should provide maximum API coverage
  • Language projections should provide friendly adaptations of APIs and types where appropriate
  • Language projections should provide IntelliSense sourced from official documentation
  • Language projections should support filtering available APIs based on the SDK version that a project targets
  • Language projections should enable developers to target a specific set of APIs and link or include only the artifacts necessary to provide support for those APIs

In addition to providing C#, C++ and Rust projections in preview, the project's roadmap indicates that Win32 metadata package will be published to NuGet.org in preview for the most recent Windows SDK version during Microsoft's //BUILD 2021 developer conference set for mid May.

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

Subscribe on YouTube