Skip to content

DLL files are dynamic link libraries, frequently needed in desktop software.

Because calling this library involves installing many tools, it is not integrated into the framework; however, this document provides complete installation and usage instructions for reference.

Pitfall Avoidance Guide

  1. A 32-bit dll must be called on a 32-bit node and a 32-bit electron

This content is no longer being updated. It is recommended to use koffi https://koffi.dev/

Prepare Environment

bash
# Compilation tools
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 calling libraries
1. npm install ref-napi         // Basic types
2. npm install ref-array-napi   // Array types
3. npm install ref-struct-napi  // Struct types
4. npm install ffi-napi         // Connect C code and JS code

# Compile ref-napi library
1. cd ./node_modules/ref-napi
2. node-gyp configure  // Configure
3. node-gyp build      // Compile

Test Code

javascript
const ffi = require('ffi-napi');
var ref = require('ref-napi');
var ArrayType = require('ref-array-napi');
    
/**
 * exec dll file
 */
async execDll () {
  // Resource path
  const dllFile = 'myDllDemo.dll';
  const dllPath = path.join(Ps.getExtraResourcesDir(), "dll", dllFile);

  // Map to C language int array type
  var IntArray = ArrayType(ref.types.int);

  // Load DLL file, no need to write extension, map functions in DLL to JS methods
  const MyDellDemo = new ffi.Library(dllPath, {
    // Method name must match the C function name
    add: [
      'int', // Corresponds to C function return type
      ['int', 'int'] // C function parameter list
    ],
    // Use shorthand types from ffi built-in types
    addPtr: ['void', ['int', 'int', 'int*']],
    // IntArray is the type constructed above through ArrayType
    initArray: ['void', [IntArray, 'int']]
  });

  // Call add method
  const res = MyDellDemo.add(1, 2);
  console.log(`add method result of 1 + 2 is: ` + res);

  // Call addPtr method
  // Use Buffer class to achieve memory sharing between C code and JS code, making Buffer act as a pointer in C language.
  // C functions use pointers to operate on memory outside the function, so first allocate an int type memory space. The first parameter is the C language data type, the second parameter is the default value
  var intBuf = ref.alloc(ref.types.int, 100);
  console.log('addPtr data before call>>', ref.deref(intBuf)); //Get the pointed-to content
  MyDellDemo.addPtr(2, 2, intBuf); // Call function, pass pointer
  console.log('addPtr data after call>>', ref.deref(intBuf));

  // Call initArray method
  // IntArray is the data type created earlier using ref-napi and ref-array-napi libraries, array length is 8
  // Memory space must be allocated here, otherwise the pointer inside the function cannot operate on memory
  let myArray = new IntArray(8);
  MyDellDemo.initArray(myArray, 8);
  console.log('Initialized array execution result:');
  for (var i = 0; i < myArray.length; i++) {
    console.log(myArray[i]);
  }

  return true;
}

DLL file location used for testing:

./build/extraResources/dll/myDllDemo.dll

Reference: https://blog.csdn.net/paopao_wu/article/details/107507225