electron系列(一)调用dll

用electron的目的,其实很简单。就是web架构要直接使用前端电脑的资源,但是浏览器限制了使用,所以用electron来达到这个目的。其中调用dll是一个非常基本的操作。

安装 ffi-napiref-napi 包:

javascript 复制代码
npm install ffi-napi ref-napi

main.js,并添加如下代码:

javascript 复制代码
const { app, BrowserWindow } = require('electron');
const path = require('path');
const ffi = require('ffi-napi');
const ref = require('ref-napi');

// 定义DLL中导出的函数和它们的参数、返回值类型
const exampleLibrary = ffi.Library(path.join(__dirname, 'example.dll'), {
  'Add': ['int', ['int', 'int']]  // Add函数接受两个整数作为参数,并返回一个整数
});

function createWindow() {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  });

  mainWindow.loadFile('index.html');

  // 调用DLL中的Add函数并打印结果
  const result = exampleLibrary.Add(5, 3);
  console.log(`DLL Add result: ${result}`);
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});
相关推荐
时间的情敌14 分钟前
Vue3的异步DOM更新:nextTick的正确使用方法
前端·javascript·vue.js
风语者日志21 分钟前
[LitCTF 2023]作业管理系统
前端·网络·安全·web安全·ctf
excel30 分钟前
深入解析:Vue 编译器核心工具函数源码(compiler-core/utils.ts)
前端
excel31 分钟前
第五章:辅助函数与全流程整合
前端
excel32 分钟前
🔍 深度解析:Vue 编译器中的 validateBrowserExpression 表达式校验机制
前端
excel33 分钟前
深度解析:Vue 模板编译器中的 Tokenizer 实现原理
前端
excel35 分钟前
🧩 Vue 编译核心:transform.ts 源码深度剖析
前端
excel35 分钟前
Vue Runtime Helper 常量与注册机制源码解析
前端
excel38 分钟前
Vue 模板编译器核心选项解析:从 Parser 到 Codegen 的全链路设计
前端
excel38 分钟前
第四章:表达式与循环解析函数详解
前端