今天,查看文档时发现Dart运行在服务端下可以调用本地实现(C/C++ dll)。

我想应该有大用处

拿出来分享!

一 先做Dart库

//sse.dart

library sample_synchronous_extension;

import 'dart-ext:sample_extension';

// The simplest way to call native code: top-level functions.
int systemRand() native "SystemRand";
bool systemSrand(int seed) native "SystemSrand";

二. 本地实现的C动态链接库

// sample_extension.c

#define DART_SHARED_LIB

/*#include <string.h>
#include <stdlib.h>
#include <stdio.h>*/

#include "C:\\tools\\dart-sdk\\include\\dart_native_api.h"

//#include <stdbool.h>
typedef enum{false = 0,true = 1} _bool;

#pragma comment(lib, "C:\\tools\\dart-sdk\\bin\\dart.lib")

// Forward declaration of ResolveName function.
Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool* auto_setup_scope);

// The name of the initialization function is the extension name followed
// by _Init.
DART_EXPORT Dart_Handle sample_extension_Init(Dart_Handle parent_library) {
  if (Dart_IsError(parent_library)) return parent_library;

  Dart_Handle result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL);
  if (Dart_IsError(result_code)) return result_code;

  return Dart_Null();
}

Dart_Handle HandleError(Dart_Handle handle) {
  if (Dart_IsError(handle)) Dart_PropagateError(handle);
  return handle;
}

// Native functions get their arguments in a Dart_NativeArguments structure
// and return their results with Dart_SetReturnValue.
void SystemRand(Dart_NativeArguments arguments) {
  Dart_Handle result = HandleError(Dart_NewInteger(rand()));
  Dart_SetReturnValue(arguments, result);
}

void SystemSrand(Dart_NativeArguments arguments) {
  bool success = false;
  Dart_Handle seed_object = HandleError(Dart_GetNativeArgument(arguments, 0));
  if (Dart_IsInteger(seed_object)) {
    bool fits;
    HandleError(Dart_IntegerFitsIntoInt64(seed_object, &fits));
    if (fits) {
      int64_t seed;
      HandleError(Dart_IntegerToInt64(seed_object, &seed));
      //srand(static_cast<unsigned>(seed));
      srand(seed);
      success = true;
    }
  }
  Dart_SetReturnValue(arguments, HandleError(Dart_NewBoolean(success)));
}

Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool* auto_setup_scope) {
  // If we fail, we return NULL, and Dart throws an exception.
  if (!Dart_IsString(name)) return NULL;
  Dart_NativeFunction result = NULL;
  const char* cname;
  HandleError(Dart_StringToCString(name, &cname));

  if (strcmp("SystemRand", cname) == 0) result = SystemRand;
  if (strcmp("SystemSrand", cname) == 0) result = SystemSrand;
  return result;
}

//生成库

cl -c sample_extension.c

link -dll sample_extension.obj

生成sample_extension.dll动态链接库

三 测试Dart库

//test.dart

import 'sse.dart';

void main() {
  if (systemSrand(120)){
    var i = systemRand();
    print('rand number is $i');
    i = systemRand();
    print('rand number is $i');
  }
}

我们试试效果吧:

C:\Dart-pro\c-dll>dart test.dart
rand number is 430
rand number is 19576

Finally:

我抱着也许客户端(browser)也能用的心态,勇敢的去试了一下(尝试转成javascript),遗憾的是:报native关键字错误,即,不可以在客户端用,只能在服务器的命令行模式下用哦!

Google的经验告诉我们,大公司的设计都是走正经套路的,所以,他们总是不能跟随你的节拍,因为你的节拍太定向化

研究过程中,我觉得以技术角度似乎不应该不能在客户端使用,但是从web标准看,还真不该在browser中实现。

哈哈,你们自己去应用吧,我想没人会那Dart做服务器吧!

哎,从Google全心支持WebAssembly来看,估计,以后还是得走这条路,不过是,由厂商为我们实现想用的C/C++库而已。

Dart server side call dll的更多相关文章

  1. Windows Server 2012 安装dll到GAC

    使用Windows管理员打开PowerShell: 运行以下命令: Set-location "c:\tools\gac" [System.Reflection.Assembly] ...

  2. 在SQL Server引用dll的流程

    原文:在SQL Server引用dll的流程 在SQL Server中引用dll分为两个步骤 1.创建一个dll文件 2.把dll文件放进SQL Server的程序集中.然后定义一个Function, ...

  3. SQL Server群集如何在线检测

    SQL Server群集知识介绍 Windows群集安装 基于iSCSI的SQL Server 2012群集测试 前言 群集的检测是调用dll资源,例如对于共享存储,ip,网络名称与DTC 这类Win ...

  4. c#的dllimport使用方法详解,调试找不到dll的方法

    DllImport会按照顺序自动去寻找的地方: 1.exe所在目录 2.System32目录 3.环境变量目录所以只需要你把引用的DLL 拷贝到这三个目录下 就可以不用写路径了 或者可以这样serve ...

  5. SQL Server 内存中OLTP内部机制概述(四)

    ----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...

  6. P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

    P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May ...

  7. SQL SERVER 2008 R2 错误代码 17000 - 17999

    错误 严重性 是否记录事件 说明(消息正文) 17000 10 否 用法: sp_autostats <table_name> [, {ON|OFF} [, <index_name& ...

  8. 如何有效抓取SQL Server的BLOCKING信息

    原文:如何有效抓取SQL Server的BLOCKING信息 转自:微软亚太区数据库技术支持组 官方博客 http://blogs.msdn.com/b/apgcdsd/archive/2011/12 ...

  9. 魔兽世界服务器Trinitycore分析二:auth server的main函数

    TrinityCore由生成两个运行文件authserver和world server以及一堆DLL(或so)文件的子项目组成(先忽略map_extractor等几个工具项目). authserver ...

随机推荐

  1. 省一行是一行:在if语句中使用C# 7.0的模式匹配

    C# 7.0的模式匹配(Pattern Mathing)不仅可以节省代码,而且可以让代码更流畅(Fluent),今天又在实际开发中体会了一下. 不用模式匹配的代码,需要先获取返回值,然后用if进行判断 ...

  2. ubuntu登录时出现“一闪之后回到登录界面”的现象

    ubuntu登录时出现“一闪之后回到登录界面”的现象 虚拟机vmware 12.5.6 build-5528349 操作系统ubuntu 18.04 问题:登录时出现一闪之后回到登录界面的现象 解决方 ...

  3. confd

    1.下载安装confd ]# wget https://github.com/kelseyhightower/confd/releases/download/v0.15.0/confd-0.15.0- ...

  4. algebraically closed field 代数闭域

     algebraically closed field https://en.wikipedia.org/wiki/Algebraically_closed_field As an example, ...

  5. MyCAT 在 Cobar 的基础上,完成了彻底的 NIO 通讯,并且合并了两个线程池

    研读: 1.http://www.mycat.io <Mycat权威指南> 第 2 章 Mycat 前世今生: 浏览: 深度认识 Sharding-JDBC:做最轻量级的数据库中间层 - ...

  6. Xcode编译警告Assigning to 'id<XXXDelegat> ——Nullable' from incompatible type 'XXXView *const_strong'

    编译报警告 可能是 自定义分类使用协议时出现与父类协议的冲突 解决方法如下:    

  7. 转:HashMap实现原理分析(面试问题:两个hashcode相同 的对象怎么存入hashmap的)

    原文地址:https://www.cnblogs.com/faunjoe88/p/7992319.html 主要内容: 1)put   疑问:如果两个key通过hash%Entry[].length得 ...

  8. Android SDK 环境搭建

    Android SDK(Software Development Kit,软件开发工具包)提供了 Android API 库和开发工具构建,测试和调试应用程序.简单来讲,Android SDK 可以看 ...

  9. 基于Gogs+Drone搭建的私有CI/CD平台

    请移步 基于Gogs+Drone搭建的私有CI/CD平台

  10. SQL常用语法大全

    一. Table 增加列 1.增加列:alter table tableName add columnName varchar(30) 1.2. 修改列类型:alter table tableName ...