win32 1.6.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 91

A package that wraps some of the most common Win32 API calls using FFI to make them accessible to Dart code without requiring a C compiler or the Windows SDK.

pub package Language

Perhaps of as much use as the APIs themselves, this package provides a variety of instructive examples for more complex FFI usage scenarios.

By design, this package provides minimal modifications to the Win32 API to support Dart idioms. The goal is to provide high familiarity to an existing Win32 developer. Other plugins may build on these primitives to provide a friendly API for Dart and Flutter developers. A good example of that is filepicker_windows, which offers a common item dialog suitable for incorporation into an existing Flutter app.

Requirements #

This package assumes the Dart 32-bit or 64-bit compiler, running on Windows.

Usage #

A simple Petzoldian Hello World example can be found in the example subdirectory. This example demonstrates creating a Win32 window and responding to common messages such as WM_PAINT through a WindowProc callback function.

To run it, type:

dart example\hello.dart

This should display a window with a text message.

This can be compiled into a standalone Win32 executable by running:

dart2native example\hello.dart -o example\bin\hello.exe

Samples #

Dart samples #

The package includes a number of examples in the example subdirectory. These examples use the Win32 API for all UI display and do not require Flutter.

hello.dartBasic Petzoldian "hello world" Win32 app
paint.dartDemonstrates simple GDI drawing and min/max window sizing
scroll.dartExample of horizontal and vertical scrolling text window
console.dartShows usage of console APIs
msgbox.dartDemonstrates a MessageBox from the console
calendar.dartGets information about the calendar from a WinRT API
sendinput.dartSends keyboard and mouse input to another window
knownfolder.dartRetrieves known folders from the current user profile
window.dartEnumerates open windows and basic window manipulation
monitor.dartUses DDC and monitor-config API to get monitor caps
wallpaper.dartShows what wallpaper and background color are set
guid.dartCreates a globally unique identifier (GUID)
devices.dartUses volume management APIs to list all disk devices
modules.dartEnumerates all loaded modules on the current system
snake.dartSnake game using various GDI features
dialogshow.dartCreates a common item dialog (file picker) using COM
wmi.dartUsing WMI from COM to retrieve device/OS information
sysinfo.dartExamples of getting device information from native C APIs
winmd.dartInterrogate Windows Runtime types
dynamic_load.dartDemonstrate loading a DLL and calling it at runtime
tetris\main.dartPort of an open-source Tetris game to Dart
notepad\notepad.dartLightweight replica of the Windows notepad applet

Flutter samples #

The explorer\ subdirectory contains an example of a simple Flutter app that uses the volume management Win32 APIs to find the disk drives connected to your computer and their volume IDs and attached paths.

Features and bugs #

Let me know what APIs you're interested in seeing. I'm not planning to project the entire Windows API, but I'm particularly interested in examples that either unblock a plugin for Windows or that demonstrate more complex Windows APIs that represent an important pattern.

Please file feature requests and bugs at the issue tracker. This is a hobby project, and should not be viewed as having the same level of quality assurance as an official package from the Dart team. I'm a product manager having fun on my weekends!

Acknowledgements #

The Tetris example listed above is a fuller worked example of a reasonably complete program that uses the Dart Win32 package. It is a port of a C version of the game by Chang-Hung Liang. More information...

The C implementation of the Snake game is by David Jones, and is ported with his permission.

The original C version of the Notepad example was originally authored by Charles Petzold, and is kindly licensed by him without restriction.

1.6.8 #

  • Add font enumeration example
  • Experiment with hosting documentation on GitHub

1.6.7 #

  • Add basic registry checks
  • Add initial Bluetooth discovery support
  • Add a system information sample
  • Guard tests so that they work on Windows 7
  • Add some shell APIs and more tests

1.6.6 #

  • Add more process management APIs
  • Add high level monitor configuration API

1.6.5 #

  • Add a broader array of console APIs
  • Add a wallpaper example

1.6.4 #

  • Lots of documentation and linter cleanup

1.6.3 #

  • Add TaskDialog and dynamic library loading APIs
  • Add dynamic load and Windows Runtime metadata samples
  • Fix an annoying bug with WindowsDeleteString usage
  • Add more tests and restructure code
  • More library-level documentation
  • Add script for generating classes

1.6.2 #

  • Clean up some of the generated documentation

1.6.1 #

  • Lots of minor refactoring and tidy up
  • Some early WinMD parsing
  • Add many more unit tests

1.6.0 #

  • Add WinRT examples, including Windows.Globalization.Calendar and Windows.Storage.Pickers.FileOpenPicker
  • Add various process management and kernel APIs: CloseHandle, EnumProcesses, EnumProcessModules, GetModuleBaseName, GetModuleFileNameExt, OpenProcess, ReadProcessMemory and WriteProcessMemory.
  • Add modules.dart sample
  • Lots of refactoring and tidy up work.

1.5.1 #

  • Add GetTempPath()

1.5.0 #

  • Use automated Dart tool to generate all COM classes
  • Add IFileDialogCustomize, IShellItem2, IShellItemArray, IShellItemFilter
  • Fill out all the class methods
  • Fix some embarrassing bugs

1.4.2 #

  • Fix a few bugs
  • Add support for desktop background management with IDesktopWallpaper

1.4.1 #

  • Expand COM support to include IShellItemArray and various WMI classes

1.4.0 #

  • Add COM support
  • Add implementations for IOpenFileDialog, IFileDialog, IModalWindow, IShellItem, IUnknown
  • Add common item dialog example

1.3.2 #

  • Add Snake GDI example
  • Add PeekMessage, MoveTo, VirtualAlloc/Free, StretchDibBits, Beep
  • Tidy up code and test

1.3.1 #

  • Add RegisterWindowMessage
  • Fix bugs in ACCEL and FINDREPLACE structs
  • Fix various bugs in Notepad example

1.3.0 #

  • Add notepad example
  • Add 20+ new APIs for common dialogs, message sending, accelerators, menus, fonts and GDI object manipulation
  • Fix APIs to be 32-bit safe
  • Fix some minor bugs

1.2.6 #

  • Add window enumeration (FindWindowEx, EnumWindows, IsWindowVisible, GetWindowText, GetWindowTextLength) and example

1.2.5 #

  • Added Flutter example
  • Added common dialog example

1.2.4 #

  • Added volume management APIs

1.2.3 #

  • Add scrolling APIs and example
  • Add 'new' known folder API
  • Add some basic unit tests

1.2.2 #

  • Add known folder plus GUID classes

1.2.1 #

  • Added SendInput, Sleep and ShellExecute

1.2.0 #

  • Implemented a good sample of GDI calls
  • Added support for timers
  • Added virtual keyboard constants
  • Added a GDI paint sample
  • Added a more comprehensive sample game (Tetris)

1.1.1 #

  • Add class styles
  • Match recommended package structure per

1.1.0 #

  • Added MessageBox and console APIs

1.0.0 #

  • Initial version


// Trivial example showing Win32 common dialog box invocation.

// More sophisticated examples can be found in the `example\` subdirectory
// of this package.

import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

// Convert from Win32 0x00BBGGRR color layout to a user-friendly string
String toHexColor(int color) => '0x'
    '${GetRValue(color).toRadixString(16).padLeft(2, '0')}'
    '${GetGValue(color).toRadixString(16).padLeft(2, '0')}'
    '${GetBValue(color).toRadixString(16).padLeft(2, '0')}';

void main() {
  // Allocates memory on the native heap for the struct that will be used to
  // configure the dialog box and return values
  final cc = CHOOSECOLOR.allocate();

  // Default color is mid-gray
  cc.rgbResult = RGB(0x80, 0x80, 0x80);

  // Set custom colors to a palette of blues and purples
  // elementAt(x).value dereferences the pointer at addr+x
  for (var i = 0; i < 16; i++) {
    cc.lpCustColors.elementAt(i).value = RGB(i * 16, 0x80, 0xFF);

  // Set dialog flags:
  //   CC_RGBINIT: use rgbResult for the dialog default value
  //   CC_FULLOPEN: automatically open custom colors section of dialog

  // Call the Win32 API to show dialog, passing pointer to the config struct

  // Print the value returned from the dialog box
  print('Color chosen: ${toHexColor(cc.rgbResult)}');

  // Free the memory allocated on the native heap

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  win32: ^1.6.8

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:win32/win32.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Analysis suggestions

Package not compatible with runtime flutter-web on Web


  • package:win32/win32.dart that imports:
  • package:win32/src/generated/IWbemServices.dart that imports:
  • package:win32/src/generated/IUnknown.dart that imports:
  • package:win32/src/structs.dart that imports:
  • package:win32/src/string.dart that imports:
  • package:ffi/ffi.dart that imports:
  • package:ffi/src/allocation.dart that imports:
  • dart:io

Package not compatible with runtime js


  • package:win32/win32.dart that imports:
  • package:win32/src/generated/IWbemServices.dart that imports:
  • package:win32/src/generated/IUnknown.dart that imports:
  • package:win32/src/structs.dart that imports:
  • package:win32/src/string.dart that imports:
  • package:ffi/ffi.dart that imports:
  • package:ffi/src/allocation.dart that imports:
  • dart:io


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
ffi ^0.1.3 0.1.3
Dev dependencies
pedantic ^1.9.0
test ^1.14.2