Dll files are dynamic link libraries that are often used in desktop software.
The library is not integrated into the framework because many tools are installed. However, this article provides complete installation instructions for your reference.
1:Prepare the environment
# Compiler
npm i -g node-gyp
# C + + build tools
1. Open PowerShell in administrator mode
2. npm --vs2015 i -g --production windows-build-tools
or npm i -g --production windows-build-tools
# External interface call library
1. npm install ref-napi // Basic type
2. npm install ref-array-napi // Array type
3. npm install ref-struct-napi // Structure type
4. npm install ffi-napi // Connecting C code and JS code
# Compile ref-napi Library
1. cd ./node_modules/ref-napi
2. node-gyp configure
3. node-gyp build
- test Code ``` const ffi = require(‘ffi-napi’); var ref = require(‘ref-napi’); var ArrayType = require(‘ref-array-napi’);
/**
exec dll file */ async execDll () { // Resource path let dllPath = ‘’; const dllFile = ‘myDllDemo.dll’; if (electronApp.isPackaged) { // After packing
dllPath = path.join(this.app.config.execDir, “resources”, “extraResources”, “dll”, dllFile); } else { // Before packing dllPath = path.join(this.app.config.execDir, “build”, “extraResources”, “dll”, dllFile); }// Mapping to C language int array type var IntArray = ArrayType(ref.types.int);
// Load the DLL file without writing the extension, and map the functions in the DLL to JS methods const MyDellDemo = new ffi.Library(dllPath, { // Method name must be consistent with C function name add: [ ‘int’, // Corresponding C function return type [‘int’, ‘int’] // C function parameter list ], // Use the shorthand type of the built-in type in FFI addPtr: [‘void’, [‘int’, ‘int’, ‘int*’]],
initArray: [‘void’, [IntArray, ‘int’]] });
const res = MyDellDemo.add(1, 2); console.log(
add method result of 1 + 2 is:
+ res);var intBuf = ref.alloc(ref.types.int, 100); console.log(‘addPtr Data before call>>’, ref.deref(intBuf)); MyDellDemo.addPtr(2, 2, intBuf); console.log(‘addPtr Post call data>>’, ref.deref(intBuf));
let myArray = new IntArray(8); MyDellDemo.initArray(myArray, 8); console.log(‘Initialization array execution result:’); for (var i = 0; i < myArray.length; i++) { console.log(myArray[i]); }
return true; } ```
- the location of the dll file used for the test:
./build/extraResources/dll/myDllDemo.dll
注:引用 https://blog.csdn.net/paopao_wu/article/details/107507225