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
# Compilernpm i -g node-gyp# C + + build tools1. Open PowerShell in administrator mode2. npm --vs2015 i -g --production windows-build-toolsor npm i -g --production windows-build-tools# External interface call library1. npm install ref-napi // Basic type2. npm install ref-array-napi // Array type3. npm install ref-struct-napi // Structure type4. npm install ffi-napi // Connecting C code and JS code# Compile ref-napi Library1. cd ./node_modules/ref-napi2. node-gyp configure3. 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
