Windows fun with Dart FFI

Native Win32 API support for Flutter and Dart

As a product manager for a developer framework and programming language, it’s not always easy to find time during the workday to write code. But I consider it a vital task in order to empathize with my customers’ needs. So I dabble here and there with various projects that pique my interest; and over the last couple of months, I’ve been exploring a project that combines my many years of working on Windows with my current focus on Flutter and Dart, culminating in a package that wraps a good portion of the Windows API for consumption from Dart and Flutter apps. But the journey itself is also quite a fun story.

Small steps: Console APIs

This all started with a small text editor. Via HackerNews, I came across Kilo, a UNIX-style terminal text editor written in less than 1,000 lines of C, and a very well-written tutorial that you can follow along to build it from scratch. I decided to give it a go, but porting the code to Dart as I went. This was a ton of fun.

Introducing FFI

Dart includes dart:ffi, a library for making Foreign Function Interface calls to C-style APIs. Using FFI, you can declare a prototype for a C-based API and call it from your Dart code. As an example, the Win32 API function SetConsoleCursorPosition can be called with the following lines of Dart code:

kilo.dart: A console text editor in ~500 lines of code that runs on Windows, macOS and Linux.

From the console to a graphical UI

ANSI extended 256-color support in dart_console
The most unimaginative app of all time.
The world needed another implementation of Tetris. This time, in Dart.
Most programmers have one text editor in them. Apparently I had two.

COM and Dart

By now I was getting more confident. In building (or translating) these more advanced apps with Dart, I’d wrapped a hundred or more Win32 APIs, including some more painstaking work to bring across the various constants and structs needed to use them, adding as many tests as I could, and starting to build out documentation for the embryonic package that I had released for pub.dev, the Dart package manager.

My debugging approach, visualized.
My first COM success: the modern Windows file open dialog with IFileDialogOpen.
A basic Windows-only app written with Flutter that sets the desktop background from a user-selected file. Pretty useless, but a good end-to-end test of Flutter and Win32 APIs working in harmony.

The Win32 Package

Over the last few months, I’ve been gradually refining and improving the package, building out samples and adding documentation. The package now supports hundreds of APIs, and a wide variety of COM APIs, with code generators doing much of the heavy lifting. More recently, I’ve been working to provide a projection for the latest Windows Runtime APIs as used in UWP apps, which opens up some other intriguing possibilities. But that’s a story for another time.

“The road goes ever on and on… and I must follow if I can…”

Product Manager for Flutter (a framework for building mobile apps) and Dart (a modern, client-optimized programming language) at Google.