微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

node-ffi模块的安装以及基于electron生成windows桌面应用程序

原文地址:

点击打开链接

一、为什么需要使用DLL

  • 需要使用系统 API 操作或扩展应用程序;
  • 需要调用第三方的接口API,特别是与硬件设备进行通信,而这些接口 API 基本上都是通过C++ 动态链接库(DLL)实现的;
  • 需要调用C++实现的一些复杂算法等。

二、node-ffi 是什么

node-ffi:Node.js Foreign Function Interface

node-ffiis a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code. It also simplifies the augmentation of node.js with C code as it takes care of handling the translation of types across JavaScript and C,which can add reams of boilerplate code to your otherwise simple C. See the example/factorial for an example of this use case.

WARNING:node-ffiassumes you kNow what you're doing. You can pretty easily create situations where you will segfault the interpreter and unless you've got C debugger skills,you probably won't kNow what's going on.

上面是 node-ffi 的介绍,英语不好,就不翻译了。

三、electron 使用 node-ffi

使用上一篇文章里的项目,在 package.json的 dependencies节点上加上node-ffi 依赖:

"dependencies": {
        "electron": "^1.6.11","ffi": "2.2.0"
    }

然后安装缺失的 npm包(参考之前的文章),注意,安装 ffi npm 包之前需要安装 python2.7,否则 ffi 会编译总是失败,导致安装不成功,具体安装参考:https://github.com/nodejs/node-gyp#installation

安装完成后,在 app.js 文件第一行中添加如下代码

const ffi = require('ffi')

然后运行程序,出现如下错误

参考资料:https://github.com/electron/electron/blob/master/docs-translations/zh-CN/tutorial/using-native-node-modules.md,原来是需要把 ffi 重新编译为适合 electron的模块。

打开 nodejs 命令行窗口,先执行如下命令:

set python=python安装路径\python.exe

设置 python 的路径,切换到项目目录,然后执行如下命令:

"./node_modules/.bin/electron-rebuild" "./node_modules/ffi"

编译完成后,运行程序,漂亮的界面终于出现了。

四、使用 ffi 调用 Widows API 解决一个小缺陷

上篇文章中的仿 QQ 登录界面还有一个小问题,就是鼠标右键点击窗口的任意地方,都会弹出系统菜单

现在使用 ffi 调用 user32.dll 中的GetSystemMenu 函数解决这个问题,首先新建一个 user32.js 文件,为了展示 ffi ,我多定义了几个API函数

const ffi = require('ffi')

exports.User32 = ffi.Library('user32',{
    'getwindowlongPtrW': ['int',['int','int']],'SetwindowLongPtrW': ['int','int','long']],'GetSystemMenu': ['int','bool']],'DestroyWindow': ['bool',['int']]
});

修改 app.js 文件,首先导入 user32.js:

const user32 = require('./app/scripts/user32').User32

然后修改如下内容

win.once('ready-to-show',() => {
        let hwnd = win.getNativeWindowHandle() //获取窗口句柄。
        user32.GetSystemMenu(hwnd.readUInt32LE(0),true); 禁用系统菜单.
        win.show()
    })

再运行项目,系统菜单就消失的无影无踪了。

最后,所有代码都已经提交到了github:https://github.com/starts2000/ElectronQQLogin,欢迎获取

原文地址:https://www.jb51.cc/windows/372943.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐