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();
  }
});
相关推荐
自进化Agent智能体27 分钟前
Hermes GitHub PR 审查 —— 自动化代码评审
前端
涛涛ing2 小时前
OpenAI Astra 泄露:零样本生成 3D 网页,前端开发者慌了吗?
前端
ssshooter3 小时前
现在网页都能提供 MCP 了?!
前端·人工智能·程序员
杉氧3 小时前
状态管理变迁史:为什么我们放弃了 Redux 选择 Zustand?
android·前端·react native
cidy_983 小时前
OpenCode 手动配置火山方舟 Agent Plan 教程
前端
鹏多多3 小时前
PC 网站接入微信登录,这 10 个坑我替你踩完了!
前端·javascript·vue.js
律宏阔3 小时前
微信小程序使用 Orval + OpenAPI 自动生成接口:Axios 兼容踩坑记录
前端·微信小程序
律宏阔3 小时前
微信小程序集成 TDesign 完整记录:解决 NPM packages not found
前端·微信小程序
律宏阔4 小时前
微信小程序 ECharts 瘦身实战:分包异步化 + componentPlaceholder 避开主包 2MB 限制
前端·微信小程序
夏天要喝冰可乐4 小时前
从 Idea 到开源插件:我用 Vibe Coding 做了「文章摆渡」
前端·ai编程·vibecoding