今天,查看文档时发现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的User Secrets特性

    昨天在一个集成测试项目中实际使用 ASP.NET Core 的 user secrets 保存敏感配置信息,避免了直接保存在 appsettings.json 中,在这篇随笔中记录一下. 使用 use ...

  2. LDAP - 轻量目录访问协议

    LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP

  3. Hive:解决Hive创建文件数过多的问题

    今天将临时表里面的数据按照天分区插入到线上的表中去,出现了Hive创建的文件数大于100000个的情况,我的SQL如下: hive> insert overwrite table test pa ...

  4. 2016年蓝桥杯省赛A组c++第2题(暴力求解)

    /* 某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛. 现在算起来,他一共吹熄了236根蜡烛. 请问,他从多少岁开始过生日party的? 请填写他开始过生日party ...

  5. xcode工程编译错误:error: Couldn’t materialize

    错误信息: error: Couldn't materialize: couldn't get the value of variable amount: variable not available ...

  6. BZOJ3613 南园满地堆轻絮 二分/贪心

    正解:贪心 解题报告: 传送门! 这题似乎是可以二分水过的,,,但数据可以加强一下就能简单把二分卡住了,或者修改下空间限制什么的反正就很容易能卡住 所以这里介绍一个优秀的贪心做法,O(n)的时间复杂度 ...

  7. ef 数据库创建失败

    在ef之前创建了一个数据库,然后删除后,再用ef创建,失败! USE [master] GO /****** Object: Database [DBname] ******/ ALTER DATAB ...

  8. 计算机网络 0.初识Internet与TCP/IP协议

    互联网,即因特网,Internet.互联网是一个世界范围的计算机网络.连接了世界上无数的计算设备,这些计算设备为PC.基于Linux的工作站,serverservers等等. 这些设备依据其作用不同可 ...

  9. 结合python+selenium使用AutoIt V3实现文件、图片上传

    1.下载.安装AutoIt V3 下载官网:https://www.autoitscript.com/site/autoit/downloads/ 2.AutoIt V3组件介绍 AutoIt Win ...

  10. 014-通过JDB调试,通过HSDB来查看HotSpot VM的运行时数据

    一.JDB调试        在预发环境下进行debug时,时常因为工具和环境的限制,导致debug体验非常差,那么有什么方法能够简化我们进行debug的体验吗?JDB就是一种.        JDB ...