Recently, the project of our company has been suspended, so I just have time to share the pit stepped by the electron integration FFI during this period, which should help children with the same needs.

The background of the project is as follows: the application on Windows side is developed by electronJs. Due to some complex usages, the application side needs to integrate SDK, that is, the DLL compiled by C++, and the DLL called on electron side is realized by ffi library. It is really a long story to build this environment.

Let me start with the environment and version that I ended up using

  1. Python 2.7.15
  2. NPM install –global –production windows-build-tools// install C++ build-tools for Windows using NPM
  3. Node: version 13.9.0 (32 bit)
  4. Ffi: Branch FFI – NAPI version 2.4.6

A few tips

  • Node uses the 32-bit version, because the SDK we compiled is different between 32-bit and 64-bit. The 64-bit Windows system can run 32-bit programs, so the SDK we packaged and used is all 32bit
  • Some errors in the installation process, like environment variables, how to set directly under Baidu
  • To switch between Node versions and Arch, it is best to install an NVM to switch between nodes

The above use of FFI-NAPI synchronous call DLL file is no problem, but the asynchronous callback, the source code error, should be the library itself bug, this is not easy to solve, if you insist on asynchronous call DLL encapsulation method, we will eventually use another way to achieve.

Finally, attached are the results of the FFI version I tested.



We can use the node-ffi library and use c++ callback. However, we can’t drop this version in my project, because my project is complicated. There are also local sqlite, participle and other node libraries. Lower the Node version and rebuild will not pass these libraries.

For node version 12*, only ffi branches corresponding to 12* can be installed, portal, package.json are modified as follows, this also cannot use c++ callback, other functions are ok.

"ffi": "github:lxe/node-ffi#node-12"."ref": "github:lxe/ref#node-12"."ref-array": "github:lxe/ref-array#node-12"."ref-struct": "github:lxe/ref-struct#node-12"Copy the code