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 ...
随机推荐
- 洛谷P1162 填涂颜色【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1162 题意: 有一个0和1组成的矩阵,一些1组成一个闭合圈,圈住一些0,现在要把被圈住的这些0变成2输出. ...
- 10.11JAVA作业
[实验任务一]:素数输出 1. 实验要求: (1)编写判断该数是否为素数的方法,并在主方法中对其进行调用. (2)注意编程规范:程序开头部分的目的,作者以及日期:必要的空格与缩进,适当的注释等: (3 ...
- SPOJ - TSUM 母函数+FFT+容斥
题意:n个数,任取三个加起来,问每个可能的结果的方案数. 题解:构造母函数ABC,比如现在有 1 2 3 三个数.则 其中B表示同一个数加两次,C表示用三次.然后考虑去重. A^3表示可重复地拿三个. ...
- 栈帧 2.6. Frames 虚拟机内存模型
https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-2.html#jvms-2.6 小结: 1. https://docs.oracle. ...
- 2012年蓝桥杯省赛A组c++第3题(喝断片的海盗)
/* 有一群海盗(不多于20人),在船上比拼酒量.过程如下:打开一瓶酒, 所有在场的人平分喝下,有几个人倒下了.再打开一瓶酒平分,又有倒下的, 再次重复...... 直到开了第4瓶酒,坐着的已经所剩无 ...
- fastreport好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了
好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了 可以用FASTREPORT中的分组打印,你看一下里面的DEMO,里面都有的, 高版本的有suppressRepeat ...
- SQL DATE_FORMAT() 函数
参照原文:https://blog.csdn.net/moakun/article/details/82290387 DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据 MySQL 使 ...
- 虚拟机窗口太小_设置分辨率(win8/win7)
虚拟机安装了WIN7和WIN8系统后,安装了VMware Tools后窗口还是较小,需要调整虚拟机系统中的分辨率. 桌面右键->屏幕分辨率->设置成与主机显示器分辨率相近的即可.
- springboot注入properties配置到javabean
一.再application.properties中添加 二. @Value("${field}")在字段上面加个注解
- mybatis获取批量插入的主键自增id
一.写一个实体类 public class UserInfo { private long userId; private String userAccount; private String use ...