Skip to content

Extract API metadata from Windows Metadata (.winmd) files.

License

Notifications You must be signed in to change notification settings

halildurmus/winmd

ci Package: winmd Publisher: halildurmus.dev Language: Dart License: BSD-3-Clause codecov

A package that provides a Dart language abstraction over Windows Metadata (WinMD) files, making it possible to load them and build Dart FFI interop libraries from the results.

It can be used to query Windows developer APIs, encompassing both unmanaged APIs like Win32 or COM, as well as modern APIs like Windows Runtime (WinRT) that include their own metadata.

Architecture

Architecture diagram

Usage

Windows Runtime (WinRT)

Load the MediaPlayer class and print out its methods.

import 'package:winmd/winmd.dart';

void main() async {
  // Load the WinRT metadata
  final scope = await MetadataStore.loadWinrtMetadata();

  // A Windows Runtime class
  const typeToGenerate = 'Windows.Media.Playback.MediaPlayer';

  // Find the TypeDef for this class
  final typeDef = scope.findTypeDef(typeToGenerate)!;

  // Create a Dart projection
  print('$typeToGenerate contains the following methods:');

  for (final method in typeDef.methods) {
    print('  ${method.name}');
  }

  MetadataStore.close();
}

Win32

Load the MessageBoxW function and print out its parameters and return type.

import 'package:winmd/winmd.dart';

void main() async {
  // Load the Win32 metadata
  final scope = await MetadataStore.loadWin32Metadata();

  // Find a namespace
  final namespace =
      scope.findTypeDef('Windows.Win32.UI.WindowsAndMessaging.Apis')!;

  // Sort the functions alphabetically
  final sortedMethods = namespace.methods
    ..sort((a, b) => a.name.compareTo(b.name));

  // Find a specific function
  const funcName = 'MessageBoxW';
  final method = sortedMethods.firstWhere((m) => m.name == funcName);

  // Print out some information about it
  print('Win32 function $funcName [token #${method.token}]');

  // Retrieve its parameters and project them into Dart FFI types
  final params = method.parameters
      .map((param) =>
          '${param.typeIdentifier.name.split('.').last} ${param.name}')
      .join(', ');
  print('The parameters are:\n  $params');

  final returnType = method.returnType.typeIdentifier.name.split('.').last;
  print('It returns type: $returnType.');

  MetadataStore.close();
}

Windows Driver Kit (WDK)

Load the NtQuerySystemInformation function and print out its parameters and return type.

import 'package:winmd/winmd.dart';

void main() async {
  // Win32 metadata also needs to be loaded to resolve references from WDK
  // metadata
  await MetadataStore.loadWin32Metadata();
  // Load the WDK metadata
  final scope = await MetadataStore.loadWdkMetadata();

  // Find a namespace
  final namespace =
      scope.findTypeDef('Windows.Wdk.System.SystemInformation.Apis')!;

  // Sort the functions alphabetically
  final sortedMethods = namespace.methods
    ..sort((a, b) => a.name.compareTo(b.name));

  // Find a specific function
  const funcName = 'NtQuerySystemInformation';
  final method = sortedMethods.firstWhere((m) => m.name == funcName);

  // Print out some information about it
  print('Win32 function $funcName [token #${method.token}]');

  // Retrieve its parameters and project them into Dart FFI types
  final params = method.parameters
      .map((param) =>
          '${param.typeIdentifier.name.split('.').last} ${param.name}')
      .join(', ');
  print('The parameters are:\n  $params');

  final returnType = method.returnType.typeIdentifier.name.split('.').last;
  print('It returns type: $returnType.');

  MetadataStore.close();
}

Packages built on winmd

  • win32: provides Dart FFI bindings to the Win32 API, allowing you to call unmanaged Windows APIs using Dart types.

Feature requests and bugs

Please file feature requests and bugs at the issue tracker.