Dart Native Access lets you deal with native libraries from Dart with zero lines of C/C++ code. Just pure Dart.

How to ...

For instance, you want to call getppid function from libc library on Linux.

  1. Find method signature in documentation:

pid_t getpid(void);

  1. Map parameters types and return type to C data types:

typedef int pid_t;

  1. Map C data types to Dart types and DNA type constants. See Type mapping.
  1. Define class Libc with annotation @Library:
class Libc {

libc.so.6 is library name

  1. Define method getppid. Annotate parameters with @Param and method with @Method with corresponding type constants:
class Libc {
  int getpid() { ... }

In this example method has no parameters.

  1. Finish with boilerplate code:

FIXME: This is temporary step for early versions

class Libc {
  dynamic lib = new DynamicLibrary<Libc>();  
  int getpid() { return lib.getpid(); }

  1. Use it
Libc libc = new Libc();
var processId = libc.getpid();

For full code see Examples.

How to define library

class Library {
    dynamic lib = new DynamicLibrary<Library>(); //It's required yet

name is the library name. For instance: libname.so or name.dll or libname.dylib.

Please see documentation for the target OS to know where dynamic linker searches for the library.

FIXME: Symlinks aren't supported yet?

How to define method

T method(@Param(C_XYZ) P1 inParam, @Param(C_XYZ) @Out() Ref<P2> outParam) 
    { return lib.method(inParam, outParam); } //It's required yet

C_XYZ is type constant. See Type mapping.

How to define structure

class Struct {
    T field;

Type mapping

| C | Dart |Constant|In/Out|Comments| |-----|-----|-----|-----|-----| |void|void|VOID|In|| |char|int|C_CHAR|In|| |short|int|C_SHORT|In|| |int|int|C_INT|In|| |long|int|C_LONG|In|| |longlong|int|C_LONGLONG|In|| |bool|bool|C_BOOL|In|| |float|double|C_FLOAT|In|| |double|double|C_DOUBLE|In|| |---|---|---|---|---| |$type |int|C_POINTER|In|Raw pointer value.| |$type |TypedData|TYPEDDATA|In\Out|TypeData must be initialized and have expected by callee size.| |---|---|---|---|---| |struct |T|C_STRUCT|In| T is strut class. See Define structure.| |struct |Ref<T>|C_STRUCT|Out|T is strut class. See Define structure.| |---|---|---|---| |char |String|C_STRING|In|| |char |Ref<String>|C_STRING|Out|| |int |List<int>|LISTINT|In|| |int |Ref<List<int>>|LISTINT|Out|| |char |List<String>|LISTSTRING|In|| |char |Ref<List<String>>|LISTSTRING|Out||

Out parameters

Out parameters must be annotate with @Out

Dart doesn't support parameters by reference. It's good but it's the causes of one challenge: callee cannot override parameter object. For instance, String parameter cannot be modified inside called method.

Therefore, it's needed to use wrapper object to emulate out parameters: Ref<T>.

FIXME: Describe pointers, in/out parameters, String parameters and TypedData parameters


I see Failed assertion: 'libraryPointer != 0

Dynamic linker cannot find the library. Check the library name.

I see Failed assertion: 'methodPointer != 0

Dynamic linker cannot find method in the library. Check the method name. Check that library exports the method.

My library is cross platform and has different name on each platform.

See example.

@Library('libname.so', 'name.dll')
class Library {
    dynamic lib = new DynamicLibrary<Library>();

My library doesn't export functions or/and uses custom logic to get function pointer

See example.

class Library {
  dynamic lib = new DynamicLibrary<Library>(getMethodPointer);

  static int getMethodPointer(DynamicLibrary that, Invocation invocation) {
    return ... //raw function pointer here

Why is it needed to define such strange method body{ return lib.name(); }?

Because I don't how to create class at runtime in Dart.



libc library

class Libc {
  dynamic lib = new DynamicLibrary<Libc>();

  int getpid() { return lib.getpid(); }      
  int getuid() { return lib.getuid(); }       
  void srand(@Param(C_UINT) int seed) { return lib.srand(seed); }      
  int rand() { return lib.rand(); }

void main(){
  Libc libc = new Libc();
  var processId = libc.getpid();
  print('pid $processId');

  var userId = libc.getuid();
  print('user id ${userId}');

  var rand = libc.rand();
  print('random $rand');    


kenel32 library

class Kernel32 {
  dynamic lib = new DynamicLibrary<Kernel32>();

  int GetCurrentProcess() { return lib.GetCurrentProcess(); }
  int GetProcessId(@Param(C_INT) int process) { return lib.GetProcessId(process); }
  int GetModuleFileNameA(@Param(C_INT) int process, @Param(C_STRING) @Out() Ref<String> imageFileName, 
    @Param(C_INT) int size) { return lib.GetModuleFileNameA(process, imageFileName, size); }
  bool GetProcessWorkingSetSize(@Param(C_INT) int process, 
    @Param(C_POINTER) Ref<int> minimumWorkingSetSize,
    @Param(C_POINTER) Ref<int> maximumWorkingSetSize) { return lib.GetProcessWorkingSetSize(process, minimumWorkingSetSize, maximumWorkingSetSize); }

  int GetLastError() { return lib.GetLastError(); }

void main(){
  Kernel32 kernel32 = new Kernel32();
  var process = kernel32.GetCurrentProcess();
  var processId = kernel32.GetProcessId(process);
  print('pid $processId = ${io.pid}');

  var min = new Ref<int>(0);
  var max = new Ref<int>(0);
  var result = kernel32.GetProcessWorkingSetSize(process, min, max);
  print('working set min ${min.value} max ${max.value}');

  var name = new Ref(new String.fromCharCodes(new List.filled(64, 0)));
  result = kernel32.GetModuleFileNameA(0, name, name.value.length);
  print('module name  ${name.value} result $result error ${kernel32.GetLastError()}');



  • Linux 64-bit
  • Windows 32-bit and 64-bit

To Do

  • Improve performance
  • A lot of things aren't supported yet
    • Platform independent types: int8, int16, int32, int64 etc.
    • Function pointers to Dart methods
    • ...
  • Get rid off template code in each library class
  • Mac OS support
  • Refactor and clean up code



Dart Native Access