node-addon C++ 插件需要注意 dll 的依赖
LoadLibrary 加载方式
11132221san7myf15703182920190322
- dll 文件所在目录添加到环境变量
- dll 文件复制到 C:\Windows\System32 或 C:\Windows\SysWOW64
- 绝对路径
运行时改变工作目录
char dir[MAX_PATH] = { 0 };// 保存当前目录GetCurrentDirectoryA(MAX_PATH, dir);// 将当前的工作目录设置为 DLL 目录SetCurrentDirectory(R"(D:\workspace\xxx\libs\)");// 动态加载 DLL 模块句柄module = LoadLibrary("libgreeting_shared.dll");// 回到当前目录SetCurrentDirectory(dir);
动态传递 dll 文件名,需要和启动 js 同目录
std::string dll = info[0].As<Napi::String>().Utf8Value();// Handle to DLLHINSTANCE module;// 动态加载 DLL 模块句柄module = LoadLibrary(dll.c_str());
常见问题
如何确定 dll 是 32 位还是 64 位
- 记事本打开,32位: PE L,64 位: PE d
- 64 位和 32 位 dll 无法互相调用(2021.11)
- 需要重新编译 dll
- 或者 COM 服务通信
- 配置 32 位编译环境
- visual studio 应该是可以直接切换,没试过
- mingw-w64:64 和 32 位可以并存
- clion 配置 32 位编译环境(不需要重新配置 cmake 和 gdb)
- 193 NodeJs、node-gyp 和 dll 位数不一致
- 126 没找到 dll,多数情况是缺少依赖的 dll
- dependencywalker 分析 dll 依赖,太老了,不能用(2021)
- dll 分析工具 ProcessMonitor 动态分析指定规则下的 dll 依赖以及加载情况
```cpp
举例:a.dll 依赖 b.dll,筛选 D:\ 后可以看到如下信息:
operation path result QueryOpen D:\xx\a.dll SUCCESS
CreateFile D:\xx\a.dll SUCCESS LoadImage D:\xx\a.dll SUCCESS QueryOpen D:\xx\b.dll PATH NOT FOUND QueryOpen D:\xx\node_modules.bin\b.dll PATH NOT FOUND
举例:libgcc_s_seh-1.dll、libstdc++-6.dll、libwinpthread-1.dll
把 mingw64 的相应 dll 复制到js目录
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin ```
- 203 可能是函数名称不对
