• 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-proc-dll>dart test.dart
    rand number is 430
    rand number is 19576

    Finally:

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

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

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

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

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

  • 相关阅读:
    iOS 类知乎”分页”效果的实现?
    iOS 图解弹幕功能的实现
    iOS 为何使用runtime方法交换多次后却能按照交换顺序依次执行代码逻辑?
    iOS常用算法之单链表查找倒数第n个节点(图解)
    iOS常用算法之两个有序数组合并, 要求时间复杂度为0(n)
    iOS 常用算法之设计一个算法,验证某字符是否为合法IPV4字符
    iOS .Crash文件分析处理办法 (利用symbolicatecrash工具处理)
    iOS中UIWebview中网页宽度自适应的问题
    iOS开发
    安卓应用加固之反动态调试技术总结
  • 原文地址:https://www.cnblogs.com/woodzcl/p/7642467.html
Copyright © 2020-2023  润新知