返回首页

electron 怎么调用 dll 文件?

117 2023-12-22 01:56 admin

在electron中使用C#的动态库时,需要注意electron和node的版本,以下测试成功的是electron 11.1.1和node 12.16.3版本。具体配置如下:

  • 安装electron-edge-js模块,目前使用的版本为12.18.4;
  • 需要在vue.config.js中配置,如下:
module.exports = {
  pluginOptions: {
    electronBuilder: {
      nodeIntegration: true,
      ---externals: ['electron-edge-js'],---配置
    },
  },
};
electron-vue调用 C# dll 闲谈NanUIdavid zhang:electron-vue跨平台桌面应用开发实战教程(八)——edgejs调用C# dll

1.C#中编写程序暴露的接口方式

namespace electron
{
    public class Class1
    {
        //一定要使用异步的方式暴露接口
        public async Task<Object> Invoke(dynamic input)
        {
            return Helper.SayHi("Invoke1:" + (string)input.name,(int)input.age);
        }

        static class Helper
        {
            public static string SayHi(string param,int qq)
            {
                return ".NET Welcomes " + param+"qq:"+qq;
            }
        }
    }
}

2.electron中调用接口方式

const edge = require('electron-edge-js');

const EdgeExtender = edge.func({
  assemblyFile: "./lib/Polygon.dll",
  typeName: "Polygon.edge",
  methodName: "Invoke"
});

EdgeExtender({ name: "你好", age: 2000 }, (err, value) => {
  console.log(value);
});

3.C#与electron中数据对应的转换关系

使用electron-edge-js传参的问题(我需要传多个参数) · Issue #I1H51O · david_zh/electron-vue-demos - Gitee.com
//js中数据
let obj = {
    anInteger:1,
    aNumber:3.1415,
    aString:'foo',
    aBoolean:true,
    aBuffer:new Buffer(10),
    anArray:[1,'foo'],
    anObject:{a:'foo',b:12}
}

//C#中数据
dynamic obj;
int anInteger = (int)obj.anInteger;
double aNumber = (double)obj.aNumber;
string aString = (string)obj.aString;
bool aBoolean = (bool)obj.aBoolean;
byte[] aBuffer = (byte[])obj.aBuffer;
object[] anArray = (object[])obj.anArray;
dynamic anObject = (dynamic)obj.anObject;
//上述数据格式一一对应
顶一下
(0)
0%
踩一下
(0)
0%
相关评论
我要评论
用户名: 验证码:点击我更换图片

网站地图 (共14个专题16696篇文章)

返回首页