usb-ffi.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict'
  2. /**
  3. * 软件锁检测
  4. *
  5. * @author EllisRan.
  6. * @date 2018/6/20
  7. * @version
  8. */
  9. const path = require('path')
  10. const ffi = require('ffi')
  11. const electron = require('electron')
  12. const Notification = electron.Notification
  13. const ipcMain = electron.ipcMain
  14. const usb = require('usb')
  15. const usbffi = function (win) {
  16. ipcMain.on('testUsb', function () {
  17. testUsb(win.webContents)
  18. })
  19. usb.on('attach', function (device) {
  20. testUsb(win.webContents)
  21. })
  22. usb.on('detach', function (device) {
  23. win.webContents.send('usbOut')
  24. const notification = new Notification({
  25. title: '软件锁',
  26. body: '你的软件锁已拔出'
  27. })
  28. notification.show()
  29. })
  30. }
  31. async function testUsb (webContents) {
  32. try {
  33. let pathstr = path.join('data/zhlData.dll')
  34. let libm = ffi.Library(pathstr, {
  35. 'GetProductIDList': ['string', []]
  36. })
  37. console.log(await libm.GetProductIDList())
  38. let alllist = await libm.GetProductIDList()
  39. if (alllist !== '') {
  40. // let productlist = alllist.split('|')
  41. // for (let i in productlist) {
  42. // let productNum = productlist[i].split(':')[0]
  43. // let productVerList = productlist[i].split(':')[1].split(';')
  44. // }
  45. }
  46. webContents.send('usbIn')
  47. const notification = new Notification({
  48. title: '软件锁',
  49. body: '启动器检测到软件锁'
  50. })
  51. notification.show()
  52. } catch (err) {
  53. console.error(err)
  54. }
  55. }
  56. export default usbffi