一、為什么需要使用DLL
需要使用系統(tǒng) API 操作或擴展應(yīng)用程序;
需要調(diào)用第三方的接口API,特別是與硬件設(shè)備進行通信,而這些接口 API 基本上都是通過 C++ 動態(tài)鏈接庫(DLL)實現(xiàn)的;
需要調(diào)用C++實現(xiàn)的一些復(fù)雜算法等。
二、node-ffi 是什么
node-ffi:Node.js Foreign Function Interface
node-ffi
is 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-ffi
assumes 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 節(jié)點上加上node-ffi 依賴:
"dependencies": { "electron": "^1.6.11", "ffi": "2.2.0" }
然后安裝缺失的 npm 包(參考之前的
延伸閱讀
學(xué)習(xí)是年輕人改變自己的最好方式