在 ElectronNode.js中调用 Windows DLL主流有纯 JS 直接调用、C 原生扩展、进程间通信三大类方案。下面按推荐度 上手难度详细说明。一、纯 JS 直接调用最常用无 C 代码1.Koffi现代首选2025–2026 推荐优势维护活跃、性能好、兼容新版 Electron、支持同步/异步、结构体、回调、stdcall/cdecl依赖koffi无需ref/ref-struct安装npminstallkoffi示例调用 DLL 函数constkoffirequire(koffi);// 1. 加载 DLLconstlibkoffi.load(./mydll.dll);// 或绝对路径// 2. 声明函数返回类型 [参数类型]// 例int Add(int a, int b);constAddlib.func(Add,int,[int,int]);// 3. 调用constsumAdd(10,20);console.log(sum);// 30// 4. 结构体/指针简单示例constPointkoffi.struct({x:int,y:int});constGetPointlib.func(GetPoint,Point,[]);constptGetPoint();console.log(pt.x,pt.y);适用绝大多数场景硬件 SDK、系统 DLL、第三方库2.ffi-napi ref-napi传统主流优势生态成熟、文档多、大量旧项目在用问题Electron 20.3.8 可能内存保护报错需用 fork 版或降级安装npminstallffi-napi ref-napi ref-struct-napi示例constffirequire(ffi-napi);constrefrequire(ref-napi);constlibffi.Library(./mydll.dll,{Add:[int,[int,int]]});constsumlib.Add(10,20);console.log(sum);适用老项目维护不推荐新项目二、C Node 扩展性能/复杂交互首选1.Node-API (N-API) node-addon-api原理用 C 写一层Node 原生扩展.node在 C 里LoadLibrary/调用 DLL再暴露给 JS优势最稳定、性能最高、复杂结构体/回调/内存完全可控劣势必须会 C、需配置binding.gyp、跨平台编译步骤初始化npminitnpminstallnode-addon-apinpminstall-gnode-gyp编写binding.gyp{targets:[{target_name:mydll_bridge,sources:[bridge.cpp],include_dirs:[!(node -p \require(node-addon-api).include\)],dependencies:[!(node -p \require(node-addon-api).gyp\)],libraries:[../libs/mydll.lib]// 配套 .lib}]}编写bridge.cppC 桥接#includenapi.h#includewindows.htypedefint(*AddFunc)(int,int);Napi::ValueAdd(constNapi::CallbackInfoinfo){Napi::Env envinfo.Env();HMODULE dllLoadLibraryA(./mydll.dll);AddFunc add(AddFunc)GetProcAddress(dll,Add);intainfo[0].AsNapi::Number();intbinfo[1].AsNapi::Number();intresadd(a,b);FreeLibrary(dll);returnNapi::Number::New(env,res);}Napi::ObjectInit(Napi::Env env,Napi::Object exports){exports.Set(Add,Napi::Function::New(env,Add));returnexports;}NODE_API_MODULE(mydll_bridge,Init)编译node-gyp configure node-gyp build--archx64JS 调用constbridgerequire(./build/Release/mydll_bridge.node);console.log(bridge.Add(10,20));// 30适用高频调用、复杂回调、硬件 SDK、内存敏感场景三、其他方案特殊场景1.edge-jsC#/.NET DLL直接调用.NET Framework/.NET Core DLLC#/VBnpminstalledge-jsconstedgerequire(edge-js);consthelloedge.func((){/* async (string input) { return Hello input; } */});hello(Electron,(err,res){console.log(res);});适用仅 .NET DLL2.进程间通信IPC/管道/HTTP写一个C/C# 控制台程序封装 DLLElectron 通过child_process/ WebSocket / HTTP 通信优势完全隔离崩溃、跨语言、不污染主进程劣势性能低、通信复杂适用DLL 不稳定易崩溃、需独立进程隔离3.Win32 API 直接调用系统 DLLuser32.dll/kernel32.dll用Koffi/ffi-napi直接调用// Koffi 调用 MessageBoxWconstuser32koffi.load(user32.dll);constMessageBoxWuser32.func(MessageBoxW,int,[ptr,wstr,wstr,uint]);MessageBoxW(null,Hello Electron,Title,0);四、方案对比2026 最新方案上手难度稳定性性能维护适用场景Koffi⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐活跃新项目首选、绝大多数 DLLffi-napi⭐⭐⭐⭐⭐⭐⭐⭐停滞老项目、兼容旧 ElectronNode-API 扩展⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐官方高性能、复杂交互、硬件 SDKedge-js⭐⭐⭐⭐⭐⭐⭐⭐一般仅 .NET DLLIPC 进程⭐⭐⭐⭐⭐⭐⭐⭐⭐高隔离不稳定 DLL五、常见坑与解决架构不匹配DLL 是 x86 → Electron 必须用 32 位--archia32DLL 是 x64 → Electron 必须用 64 位依赖缺失报错 126/127用Dependency Walker查看缺少 VC 运行库/Qt 等把依赖 DLL 放到同目录调用约定Koffi自动支持stdcallWindows API/cdeclffi-napiffi.Ccdecl/ffi.Win32stdcallElectron 版本兼容优先 Koffi兼容最新版ffi-napi20.3.8 报错 → 用ffi-napilatest或 fork 版六、推荐路线新项目、无 C→Koffi老项目、已用 ffi-napi→ 继续维护高性能/硬件 SDK/复杂回调→Node-API 扩展仅 .NET DLL→edge-jsDLL 易崩溃→独立进程 IPC