Dart server side call dll
今天,查看文档时发现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的更多相关文章
- Windows Server 2012 安装dll到GAC
使用Windows管理员打开PowerShell: 运行以下命令: Set-location "c:\tools\gac" [System.Reflection.Assembly] ...
- 在SQL Server引用dll的流程
原文:在SQL Server引用dll的流程 在SQL Server中引用dll分为两个步骤 1.创建一个dll文件 2.把dll文件放进SQL Server的程序集中.然后定义一个Function, ...
- SQL Server群集如何在线检测
SQL Server群集知识介绍 Windows群集安装 基于iSCSI的SQL Server 2012群集测试 前言 群集的检测是调用dll资源,例如对于共享存储,ip,网络名称与DTC 这类Win ...
- c#的dllimport使用方法详解,调试找不到dll的方法
DllImport会按照顺序自动去寻找的地方: 1.exe所在目录 2.System32目录 3.环境变量目录所以只需要你把引用的DLL 拷贝到这三个目录下 就可以不用写路径了 或者可以这样serve ...
- SQL Server 内存中OLTP内部机制概述(四)
----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...
- 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 ...
- SQL SERVER 2008 R2 错误代码 17000 - 17999
错误 严重性 是否记录事件 说明(消息正文) 17000 10 否 用法: sp_autostats <table_name> [, {ON|OFF} [, <index_name& ...
- 如何有效抓取SQL Server的BLOCKING信息
原文:如何有效抓取SQL Server的BLOCKING信息 转自:微软亚太区数据库技术支持组 官方博客 http://blogs.msdn.com/b/apgcdsd/archive/2011/12 ...
- 魔兽世界服务器Trinitycore分析二:auth server的main函数
TrinityCore由生成两个运行文件authserver和world server以及一堆DLL(或so)文件的子项目组成(先忽略map_extractor等几个工具项目). authserver ...
随机推荐
- ML.NET教程之客户细分(聚类问题)
理解问题 客户细分需要解决的问题是按照客户之间的相似特征区分不同客户群体.这个问题的先决条件中没有可供使用的客户分类列表,只有客户的人物画像. 数据集 已有的数据是公司的历史商业活动记录以及客户的购买 ...
- java基础解析系列(二)---Integer
java基础解析系列(二)---Integer 前言:本系列的主题是平时容易疏忽的知识点,只有基础扎实,在编码的时候才能更注重规范和性能,在出现bug的时候,才能处理更加从容. 目录 java基础解析 ...
- 【紫书】uva133 The Dole Queue 参数偷懒技巧
题意:约瑟夫问题,从两头双向删人.N个人逆时针1~N,从1开始逆时针每数k个人出列,同时从n开始顺时针每数m个人出列.若数到同一个人,则只有一个人出列.输出每次出列的人,用逗号可开每次的数据. 题解: ...
- python面向对象高级:枚举
在数学和计算机科学理论中,一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数.这两种类型经常(但不总是)重叠. 枚举是一个被命名的整型常数的集合,枚举在日常生活中很常见,例 ...
- linux文件系统扩展属性
翻译自man手册,水平有限,有错还望不吝指出.... 扩展属性是与文件和目录相关的name:value对,用来提供文件系统的一些附加功能,例如ACL.对文件或是目录拥有读权限的用户可以看到其扩展属性. ...
- mysql表引擎myisam改为innodb
1.进入数据库 2.SELECT CONCAT('ALTER TABLE `', table_name, '` ENGINE=InnoDB;') AS sql_statements FROM ...
- “对外部(局部)变量的访问”是C语言函数指针的最大弱点
1.“对外部(局部)变量的访问”是C语言函数指针的最大弱点 . #include <stdio.h> #include <stdlib.h> /* 结构体定义 */ struc ...
- 如何辨别高潜牛人的六个方法,据说源自500强HR
如果你是一名领导,当老板派下来任务让你招人的时候,你有考虑过怎么招到合适的人么?今天,架构师米洛特意分享一篇优秀的网络文章,据说来自500强的HR,希望对你招人有所帮助. 如何识人是HR及管理者重要的 ...
- ios开发之 NSObject详解
NSObject是大部分Objective-C类继承体系的根类.这个类遵循NSObject协议,提供了一些通用的方法,对象通过继承NSObject,可以从其中继承访问运行时的接口,并让对象具备Obje ...
- [daily][mirror][daemonlogger][tc] 我想把一个网卡(port A)的流量,镜像到虚拟机的一个网卡(port VA)上去
iptables tee 模块 https://blog.gnuers.org/?p=740 http://blog.csdn.net/wesleyflagon/article/details/385 ...