tree: ee552ae3efa125f0203dc413779cd26908bf3834 [path history] [tgz]
  1. hook/
  2. lib/
  3. src/
  4. test/
  5. tool/
  6. pubspec.yaml
  7. README.md
pkgs/code_assets/example/host_name/README.md

host_name

A cross-platform Dart package for retrieving the local machine's hostname on major desktop and mobile platforms.

This project is a minimal, cross-platform demonstration of how to use Dart's build hooks, with code assets and dart:ffi to call a simple function from a system API. This project uses gethostname from the system APIs. Note that Windows has a different system API than the Unix-based systems.

How it works

The build hook (hook/build.dart) declares how the system APIs should be called. On Unix-systems the native code should already be loaded into the process, and on Windows the system API is available in a .dll which is available on every Windows machine.

The Dart FFI bindings to the C code are generated by tool/ffigen.dart. FFIgen generates Dart FFI bindings from src/unix.h and src/windows.h into lib/src/third_party/unix.dart and lib/src/third_party/windows.dart. Note that the system APIs for Windows are not available on Unix and vice versa. Hence, tool/ffigen.dart only generates the bindings it can. If extra system APIs are added, FFIgen will need to be re-run on multiple machines. This is expected with cross-platform system APIs that differ on different platforms.

The Dart code in lib/src/host_name.dart uses the generated FFI bindings to provide a Dart API (not exposing any C types).