今天,查看文档时发现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. ASP.NET Core 的Windows和IIS宿主(自动翻译记录)

    https://docs.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=aspnetcore2x 支持的操作系统 以下操作系统的支持: Win ...

  2. io 流操作hdfs

    hdfs 文件上传 本地   -------->    文件系统对象   -------->    hdfs 文件系统 输入流                                ...

  3. hdu6397 Character Encoding 母函数解约束条件下多重集

    http://acm.hdu.edu.cn/showproblem.php?pid=6397 原问题的本质是问m个元素的多重集S,每一种类型的对象至多出现n-1次的S的k组合的个数是多少? 等价于 x ...

  4. 查询大数据表的效率对比:Linq to SQL、Entity Framework、企业库存储过程、ADO.Net

    最近因为要开发大数据量网站,特作比较. Linq to SQL 查询 记录数:399997Linq to SQL 查询 Milliseconds:1910视图查询 记录数:399997视图查询 Mil ...

  5. OpenGL开发学习指南二(glfw+glad)

    版权声明:本文为博主原创文章,未经博主允许不得转载.blog.liujunliang.com.cn https://blog.csdn.net/qq_33747722/article/details/ ...

  6. SQL row_number() over(partition by函数

    1)row_number() over(partition by 列名1 order by 列名2 desc)的使用 表示根据 列名1 分组,然后在分组内部根据 列名2 排序,而此函数计算的值就表示每 ...

  7. jetty各个版本对应的jdk版本

    1:jetty各个版本信息 版本号 发布及维护年份 托管平台 JVM版本 支持的协议 servlet版本 JSP版本 目前状态 9.3 2015 Eclipse 1.8 HTTP/1.1 (RFC 7 ...

  8. 20165336 2017-2018-2 《Java程序设计》第2周学习总结

    学号 2017-2018-2 20165336 <Java程序设计>第2周学习总结 教材学习内容总结 第二章 标识符第一个字符不能是数字 标识符不能是关键字 byte型变量的取值范围是-2 ...

  9. java 网络编程(一)InetAddress

    package cn.sasa.net; import java.net.InetAddress; import java.net.UnknownHostException; public class ...

  10. byte数组存储到mysql

    public int AddVeinMessage(byte[] data)//插入数据库 { using (BCSSqlConnection = new MySqlConnection(strCon ...