dna 0.6.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 50

DNA

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.

  • Find method signature in documentation:
pid_t getpid(void);
  • Map parameters types and return type to C data types:
typedef int pid_t;
  • Map C data types to Dart types and DNA type constants. See Type mapping.
  • Define class Libc with annotation @Library:
@Library('libc.so.6')
class Libc {
  
}

libc.so.6 is library name.

  • 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.

  • Finish with boilerplate code:
class Libc extends DynamicLibrary<Libc> {
  int getpid();
}

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

For full code see Examples.

How to define library

@Library('name')
class Library DynamicLibrary<Library> {
    
}

name is the OS specific 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

How to define method

@Method(C_XYZ) T method(
  @Param(C_XYZ) T1 inParam, 
  @Param(C_XYZ, out: true) Ref<T2> outParam);

Annotation @Method is used to define C type of return value. Annotation @Param is used to define C type of paramters. C_XYZ is C type constant. See Type mapping. @Method and @Param are optional. See Default mapping.

How to define structure

@Struct()
class Struct {
    @Field(C_XYZ) T field;
}

Annotation @Struct is used to define C struct. Annotation @Field is used to define field of C struct. @Field is required. All fields without @Field annotations are ignored.

Type mapping

Basic types

CDartConstant
voidvoidVOID
charintC_CHAR
shortintC_SHORT
ucharintC_UCHAR
ushortintC_USHORT
intintC_INT
uintintC_UINT
longintC_LONG
ulongintC_ULONG
longlongintC_LONGLONG
ulonglongintC_ULONGLONG
boolboolC_BOOL
floatdoubleC_FLOAT
doubledoubleC_DOUBLE

Fixed-width types

CDartConstant
int8_tintC_INT8
uint8_tintC_UINT8
int16_tintC_INT16
uint16_tintC_UINT16
int32_tintC_INT32
uint32_tintC_UINT32
int64_tintC_INT64
uint64_tintC_UINT64

Pointers

CDartConstantIn/OutComments
char *StringC_STRINGIn
char *Ref<String>C_STRINGOutString must be initialized and have expected by callee size.
char **List<String>LISTSTRINGIn
---------------
{integer type} *List<int>C_{type}In
{integer type} *Ref<List<int>>C_{type}Out
{decimal type} *List<double>C_{type}In
{decimal type} *Ref<List<double>>C_{type}Out
---------------
{type} *intC_POINTERInRaw pointer value.
{type} *TypedDataTYPEDDATAIn/OutTypeData must be initialized and have expected by callee size.

Structs

CDartConstantIn/OutComments
struct *TC_STRUCTIn/OutT is struct class. See Define structure.

Default mapping

DartCConstant
voidvoidVOID
intintC_INT
boolboolC_BOOL
doublefloatC_FLOAT

Out parameters

If parameter modified by callee it must be marked as @Param(..., out: true)

Dart doesn't support parameters by reference. It's good but it's the cause 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

FAQ

  • 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 DynamicLibrary<Library> {

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

See example.

@Library('name.dll')
class Library DynamicLibrary<Library> {
  Library() : super(getMethodPointer);

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

Examples

Linux

libc library

@Library('libc.so.6')
class Libc extends DynamicLibrary<Libc>  {
  int getpid();

  int getuid();
  
  int getgid();

  int getgroups(int count, @Param(C_INT) Ref<List<int>> groups);
  
  int rand();
  
  int rand_r(@Param(C_INT) Ref<List<int>> seedp);
  
  void srand(@Param(C_UINT) int seed);
}

void main() {
  var libc = new Libc();

  var processId = libc.getpid();
  print('pid $processId');

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

  var groupId = libc.getgid();
  print('group id ${groupId}');

  var n = libc.getgroups(0, new Ref(new List()));
  var groups = new Ref(new List<int>.filled(n, 0));
  libc.getgroups(n, groups);
  print('groups ${groups.value}');

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

  var seed = new Ref([0xDEADBEE]);
  var srand = libc.rand_r(seed);
  print('random with seed $srand');
}

Windows

kenel32 library

@Library('Kernel32')
class Kernel32 extends DynamicLibrary<Kernel32> {

  static const int DWORD = C_ULONG;
  static const int HANDLE = C_POINTER;
  static const int HMODULE = HANDLE;  
  static const int PSIZE_T = C_POINTER;
  
  @Method(HANDLE)
  int GetCurrentProcess();

  @Method(DWORD)
  int GetProcessId(
    @Param(HANDLE) int process);

  @Method(DWORD)
  int GetModuleFileNameA(
      @Param(HMODULE) int process,
      @Param(C_STRING) Ref<String> imageFileName,
      @Param(DWORD) int size);

  bool GetProcessWorkingSetSize(
      @Param(HANDLE) int process,
      @Param(PSIZE_T) Ref<List<int>> minimumWorkingSetSize,
      @Param(PSIZE_T) Ref<List<int>> maximumWorkingSetSize);

  @Method(DWORD)
  int 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([0]);
  var max = new Ref([0]);
  kernel32.GetProcessWorkingSetSize(process, min, max);
  print('working set min ${min.value} max ${max.value} error ${kernel32.GetLastError()}');

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

Please find more examples in examples/.

Requirements

  • Linux 64-bit
  • Windows 64-bit

To Do

  • Improve performance
  • A lot of things aren't supported yet
    • Function pointers to Dart methods
    • Custom structure align
    • Unions
    • ...
  • Mac OS support
  • Refactor and clean up code

Changelog

0.6

  • Structure alignment and padding
  • Structure fields by reference
  • Nested structures by value and by reference

0.5

  • Refactoring
  • Fixed-length data types support

0.4

  • Refactoring
  • Improve performance

0.3

  • Nested structure support

0.2

  • Structure support

0.1

  • Initial version

Use this package as a library

1. Depend on it

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


dependencies:
  dna: ^0.6.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:dna/dna.dart';
  
Version Uploaded Documentation Archive
0.6.1 Aug 17, 2018 Go to the documentation of dna 0.6.1 Download dna 0.6.1 archive
0.6.0 Aug 17, 2018 Go to the documentation of dna 0.6.0 Download dna 0.6.0 archive
0.5.3 Apr 20, 2018 Go to the documentation of dna 0.5.3 Download dna 0.5.3 archive
0.5.2 Apr 8, 2018 Go to the documentation of dna 0.5.2 Download dna 0.5.2 archive
0.5.1 Apr 7, 2018 Go to the documentation of dna 0.5.1 Download dna 0.5.1 archive
0.5.0 Apr 6, 2018 Go to the documentation of dna 0.5.0 Download dna 0.5.0 archive
0.4.0 Dec 31, 2016 Go to the documentation of dna 0.4.0 Download dna 0.4.0 archive
0.3.0 Dec 29, 2016 Go to the documentation of dna 0.3.0 Download dna 0.3.0 archive
0.2.2 Dec 28, 2016 Go to the documentation of dna 0.2.2 Download dna 0.2.2 archive
0.2.1 Dec 27, 2016 Go to the documentation of dna 0.2.1 Download dna 0.2.1 archive

All 14 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
1
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Aug 17, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8

Platforms

Detected platforms: other

Primary library: package:dna/dna.dart with components: nativewrappers, mirrors.

Suggestions

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.

Format lib/dna.dart.

Run dartfmt to format lib/dna.dart.

Format lib/src/coder.dart.

Run dartfmt to format lib/src/coder.dart.

Maintain an example.

None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use dna.dart.

Fix additional 13 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/dynamic_library.dart (Run dartfmt to format lib/src/dynamic_library.dart.)
  • lib/src/log.dart (Run dartfmt to format lib/src/log.dart.)
  • lib/src/meta.dart (Run dartfmt to format lib/src/meta.dart.)
  • lib/src/model/argument.dart (Run dartfmt to format lib/src/model/argument.dart.)
  • lib/src/model/field.dart (Run dartfmt to format lib/src/model/field.dart.)
  • lib/src/model/library_meta.dart (Run dartfmt to format lib/src/model/library_meta.dart.)
  • lib/src/model/param.dart (Run dartfmt to format lib/src/model/param.dart.)
  • lib/src/model/param_meta.dart (Run dartfmt to format lib/src/model/param_meta.dart.)
  • lib/src/sizes.dart (Run dartfmt to format lib/src/sizes.dart.)
  • lib/src/types.dart (Run dartfmt to format lib/src/types.dart.)
  • lib/src/util.dart (Run dartfmt to format lib/src/util.dart.)
  • lib/src/util_c.dart (Run dartfmt to format lib/src/util_c.dart.)
  • lib/src/util_reflection.dart (Run dartfmt to format lib/src/util_reflection.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.0.0 <3.0.0
Dev dependencies
test 1.3.0