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();
  }
});
相关推荐
不羁的fang少年8 分钟前
前端常见问题(vue,css,html,js等)
前端·javascript·css
change_fate14 分钟前
el-menu折叠后文字下移
前端·javascript·vue.js
yivifu16 分钟前
CSS Grid 布局详解(2025最新标准)
前端·css
o***Z4482 小时前
前端性能优化案例
前端
张拭心2 小时前
前端没有实际的必要了?结合今年工作内容,谈谈我的看法
前端·ai编程
姜太小白2 小时前
【前端】CSS媒体查询响应式设计详解:@media (max-width: 600px) {……}
前端·css·媒体
weixin_411191842 小时前
flutter中WebView的使用及JavaScript桥接的问题记录
javascript·flutter
HIT_Weston2 小时前
39、【Ubuntu】【远程开发】拉出内网 Web 服务:构建静态网页(二)
linux·前端·ubuntu
百***06012 小时前
SpringMVC 请求参数接收
前端·javascript·算法
天外天-亮2 小时前
Vue + excel下载 + 水印
前端·vue.js·excel