Nodejs 调用 dll

1. 环境

  • Visual Studio 15
  • Nodejs 18.16.1

2. 动态链接库dll生成

2.1 新建项目

2.2 项目结构和代码

2.2.1 项目结构

2.2.2 代码示例

  • pch.h文件
arduino 复制代码
#ifndef PCH_H
#define PCH_H

// 添加要在此处预编译的标头
#include "framework.h"

#endif //PCH_H

//定义宏
#ifdef  IMPORT_DLL
#else
#define IMPORT_DLL extern "C" _declspec(dllimport)
#endif //  IMPORT_DLL

IMPORT_DLL int add(int a, int b);
  • dllmain.cpp 文件
arduino 复制代码
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"

int add(int a, int b) {
	return a + b;
}

2.3生成dll文件

右键点击"生成",生成dll文件

3. 使用ffi-napi 或 koffi调用dll

本例使用为ffi-napi,但是在electron 等项目中,可能会出现build之后无法使用dll的情况,可以换成koffi

sql 复制代码
安装ffi-napi
yarn add ffi-napi

如果需要复杂数据类型,可以使用ref-napi 、ref-array-napi等
yarn add  ref-napi 
  • app.js
ini 复制代码
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const ArrayType = require('ref-array-napi');

// 定义数组类型
const IntArray = ArrayType(ref.types.int);
const doubleArray = ArrayType(ref.types.double);

// 创建数组实例
const array = new IntArray([15, 25]);


let libm = ffi.Library("D:\\code\\cjj-workspace\\Dll4\\x64\\Release\\Dll4.dll", {
    'add': ['int', ['int', 'int']],
})

let tmp = libm.add(4, 9)
console.log(tmp)

执行结果:

4. 一些踩坑

  1. 调用dll报错 Dynamic Symbol Retrieval Error: Win32 error 127或者 Dynamic Linking Error: Win32 error 126

通常是因为找不到dll抛出的方法。 在Node.js中调用使用C++编译的DLL文件时,通常需要使用extern "C"来确保正确的函数签名和名称。 因为编译器在将源代码编译为二进制代码时会对函数名称进行"名称修饰"(Name Mangling),也称为名称混淆。这是为了在编译期间解决函数重载和类型安全的问题。名称修饰会改变函数名称的格式。

  1. dll发到缺少visual studio的环境,报错Dynamic Linking Error: Win32 error 126
  1. 检查dll路径是否错误
  2. 检查dll是否发的是release版本 通常vs生成的dll,默认是debug模式,这个模式生成的dll放到其他人的电脑不能直接使用的。想要生成移植到其他电脑也能使用的dll,需要点击 项目->属性 进行配置
相关推荐
贩卖纯净水.3 小时前
Webpack的基本使用 - babel
前端·webpack·node.js
贩卖纯净水.5 小时前
Webpack依赖
前端·webpack·node.js
笑醉踏歌行7 小时前
NVM,Node.Js 管理工具
运维·ubuntu·node.js
chxii9 小时前
1.4 Node.js 的 TCP 和 UDP
node.js
xd000021 天前
11. vue pinia 和react redux、jotai对比
node.js
程序猿小D1 天前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
前端老六喔1 天前
🎉 开源项目推荐 | 让你的 TypeScript/React 项目瘦身更简单!
node.js·前端工程化
醉书生ꦿ℘゜এ1 天前
npm error Cannot read properties of null (reading ‘matches‘)
前端·npm·node.js
超级土豆粉1 天前
从0到1写一个适用于Node.js的User Agent生成库
linux·ubuntu·node.js
空中湖1 天前
‘pnpm‘ 不是内部或外部命令,也不是可运行的程序
npm·node.js