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();
  }
});
相关推荐
小村儿5 分钟前
给 AI Agent 装上"长期记忆":Karpathy 的 LLM Wiki 思想,我做成了工具
前端·后端·ai编程
竹林81810 分钟前
用ethers.js连接MetaMask实现Web3钱包登录:从踩坑到稳定运行的完整记录
前端·javascript
heyCHEEMS13 分钟前
如何用 Recast 实现静态配置文件源码级读写
前端·node.js
心连欣14 分钟前
从零开始,学习所有指令!
前端·javascript·vue.js
review4454317 分钟前
大模型和function calling分别是如何工作的
前端
东东同学18 分钟前
耗时一个月,我把 Nuxt 首屏性能排障经验做成了一个 AI Skill
前端·agent
冴羽1 小时前
超越 Vibe Coding —— AI 辅助编程指南
前端·ai编程·vibecoding
梦想的颜色2 小时前
一天一个SKILL——前端最佳自动化测试 webapp-testing
前端·web app
SoaringHeart2 小时前
Flutter进阶:放弃 MediaQuery.of(context) 使用 NScreenManager
前端·flutter
openKaka_2 小时前
从 scheduleUpdateOnFiber 到 Root 微任务调度:React 如何把更新交给调度系统
开发语言·前端·javascript