binary_interop 0.0.10 copy "binary_interop: ^0.0.10" to clipboard
binary_interop: ^0.0.10 copied to clipboard

outdatedDart 1 only

Binary interop is a library that allows load shared libraries, invoke their functions and get access to their data.

binary_interop #

Binary interop is a library that allows load shared libraries, invoke their functions and get access to their data.

Version: 0.0.10

Donate to binary interop for dart

Interrelated (binary) software

Limitations

  • Binary type "long double" not supported
  • Returning the packed structures not supported
  • Passing by the value a packed structures not supported

Supportedd platforms

  • X86 Linux
  • X86 Windows
  • X86_64 Linux
  • X86_64 Windows

Mac OS X will be added soon.

Binary interop is a low-level way of interacting with dynamic loadable libraries.
It support interaction only through the low level binary types and binary objects.

Examples

import "dart:io";

import "package:binary_interop/binary_interop.dart";
import "package:unittest/unittest.dart";

import "libc.dart";

final _t = new BinaryTypes();

void main() {
  var libc = loadLibc();
  var helper = new BinaryTypeHelper(_t);

  // Strlen
  var string = "0123456789";
  var length = libc.strlen(string);
  expect(length, string.length, reason: "Call 'strlen'");

  string = "Hello Dartisans 2015\n";

  // printf
  length = libc.printf("Hello %s %i\n", ["Dartisans", 2015]);
  expect(length, string.length, reason: "Wrong length");

  // sprintf
  var buffer = alloc(_t["char[500]"]);
  length = libc.sprintf(buffer, "Hello %s %i\n", ["Dartisans", 2015]);
  var string2 = helper.readString(buffer);
  expect(length, string.length, reason: "Wrong length");
  expect(string, string2, reason: "Wrong string");
}

Libc loadLibc() {
  String libname;
  var operatingSystem = Platform.operatingSystem;
  switch (operatingSystem) {
    case "macos":
      libname = "libSystem.dylib";
      break;
    case "android":
    case "linux":
      libname = "libc.so.6";
      break;
    case "windows":
      libname = "msvcr100.dll";
      break;
    default:
      throw new UnsupportedError("Unsupported operating system: $operatingSystem");
  }

  var library = DynamicLibrary.load(libname, types: _t);
  if (library == null) {
    throw new StateError("Failed to load library: $libname");
  }

  return new Libc(library);
}

BinaryObject alloc(BinaryType type, [value]) => type.alloc(value);

Library wrapper for libc.

// This code was generated by a tool.
// Processing tool available at https://github.com/mezoni/binary_generator

import "package:binary_interop/binary_interop.dart";

class Libc {
  String _header = '''
int printf(const char *format, ...);
int sprintf(char *str, const char *format, ...);
size_t strlen(const char *s);''';    
      
  DynamicLibrary _library;
  
  /**
   *
   */
  Libc(DynamicLibrary library) {
    if (library == null) {
      throw new ArgumentError.notNull("library");
    }
  
    library.declare(_header);
    _library = library;
  }
  
  /**
   * int printf(char* format, ...)
   */
  int printf(format, [List params]) {
    var arguments = [format];
    if (params != null) {
      arguments.addAll(params);
    }
    
    return _library.invoke("printf", arguments);
  }
  
  /**
   * int sprintf(char* str, char* format, ...)
   */
  int sprintf(str, format, [List params]) {
    var arguments = [str, format];
    if (params != null) {
      arguments.addAll(params);
    }
    
    return _library.invoke("sprintf", arguments);
  }
  
  /**
   * size_t strlen(char* s)
   */
  int strlen(s) {
    return _library.invoke("strlen", [s]);
  }
  
}


0
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Binary interop is a library that allows load shared libraries, invoke their functions and get access to their data.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

binary_declarations, binary_types, file_utils, libffi_binaries, path, pub_semver, system_info, unsafe_extension

More

Packages that depend on binary_interop