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,需要点击 项目->属性 进行配置
相关推荐
极客小云11 小时前
【实时更新 | 2026年国内可用的npm镜像源/加速器配置大全(附测速方法)】
前端·npm·node.js
VXbishe18 小时前
基于web的校园失物招领管理系统-计算机毕设 附源码 24150
javascript·vue.js·spring boot·python·node.js·php·html5
努力学编程呀(๑•ี_เ•ี๑)19 小时前
【405】Not Allowed
java·vue.js·nginx·node.js
_Rookie._19 小时前
npm run 的原理
前端·npm·node.js
全栈前端老曹1 天前
【Redis】发布订阅模型 —— Pub/Sub 原理、消息队列、聊天系统实战
前端·数据库·redis·设计模式·node.js·全栈·发布订阅模型
UIUV1 天前
语义化搜索学习笔记(结合代码实战)
javascript·后端·node.js
灵犀坠1 天前
React+Node.js全栈实战:实现安全高效的博客封面图片上传(踩坑实录)
安全·react.js·node.js·router·query·clerk
全栈前端老曹1 天前
【Redis】Redis 持久化机制 RDB 与 AOF
前端·javascript·数据库·redis·缓存·node.js·全栈
mqiqe1 天前
pnpm 和npm 有什么区别?
前端·npm·node.js
likeflower9501 天前
Node.js 快速上手:核心特点 + 安装指南
node.js