Electron + vue 调用 dll 动态链接库
一、环境
node 14.16.0
electron 12.2.3
darwin x64
二、安装
yarn add ffi-napi
yarn add -D @types/ffi-napi
npm install -g node-gyp
# python
c:/python3/python.exe
# 如果 报错 Could not install Visual Studio Build Tools.
# 则到 C:\Users\xx\.windows-build-tools 目录下 手工进行安装,安装成功后在执行上面的命令。
# windows 编译工具,需要用管理员身份运行 PowerShell
npm i -g --production windows-build-tools
cd node_modules/ref-napi node-gyp configure node-gyp build cd node_modules/ffi-napi yarn install
三、使用
import ffi from 'ffi-napi' const ros = new ffi.Library('../dll/SDKlib_x64', { GetVersion: ['int', ['char']], Init: ['init', ['void']] module.exports = { init() { return ros.Init() getVersion(ver: number) { return ros.GetVersion(ver)
<template> <div id="app"> <div id="nav"> <router-link to="/">Home</router-link> | <router-link to="/about">About</router-link> </div> <router-view /> </div> </template> <script> import { init } from './assets/utils/sdk.ts' export default { name: 'App', created() { window.logger.info('init') init() </script> <style lang="less"> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; #nav { padding: 30px; font-weight: bold; color: #2c3e50; &.router-link-exact-active { color: #42b983; </style>
四、遇到的各种报错报错报错
No native build was found for platform=darwin arch=x64 runtime=electron abi=87 uv=1 libc=glibc node=14.16.0 electron=12.2.3 webpack=true
module.exports = { pluginOptions: { electronBuilder: { // node环境 nodeIntegration: true, // webpack打包忽略 ffi-napi externals: ['ffi-napi', 'ref-napi']
could not find any vs
install vs npm config set msvs_version 2022
Dynamic Linking Error: dlopen(../dll/IRSDKlib_x64.dylib, 2): image not found
不同系统下使用的动态链接库不同,mac 下是 .dylib, win 下是 .dll
var EXT = (Library.EXT = { linux: '.so', linux2: '.so', sunos: '.so', solaris: '.so', freebsd: '.so', openbsd: '.so', darwin: '.dylib', mac: '.dylib', win32: '.dll'