本文來介紹下怎麼使用electron-edge-js來調用C#動態鏈接庫,由於是調用C#動態鏈接庫,所以也只能在windows平臺上使用,這一點需要注意
在開始之前,同樣需要安裝node-gyp和windows-build-tools,具體安裝方法請參照上一篇
1.安裝electron-edge-js
npm install electron-edge-js --save
2.準備C# dll文件
dll文件請去gitee中獲取,這裏沒辦法上傳
electron-vue-demos
3.調用dll中方法
因爲是windows專屬功能,這裏我們要放到windows的判斷中
// 只在windows平臺下加載
edge = require('electron-edge-js')
invoke = edge.func({
assemblyFile: path.resolve('resources/dll/electronedge.dll'),
typeName: 'electronedge.Class1',
methodName: 'Invoke'
})
具體調用方法
if (process.platform === 'win32') {
invoke('這是自定義字符串', function (err, val) {
if (err) throw err
console.log(val)
this.$message({
message: 'dll返回的內容爲:' + val,
type: 'success'
})
})
} else {
this.$notify.error({
title: '錯誤',
message: '此功能爲windows專屬功能,mac無法使用'
})
}
但是這個時候我們運行程序就會報這個錯
這個時候我們需要做以下處理:
在vue.config.js文件中增加
externals: ['electron-edge-js']
這個時候我們程序就運行正常了
更多內容請關注公衆號