在 Electron 项目中通过 node-ffi-napi 调用 c/c++的 dll 动态链接库
目前使用的Node版本是 12, Electron 版本 6.0.10 npm包为 [email protected], (最早使用Electron 1.7时用的是 ffi, 但 ffi 已停止支持最新版本的Node)
# dev
npm i
npm start
# build
npm run dist
"extraFiles": [
"dll" // Where *.dll File
]
MyDLL.dll
Source Code#include "stdafx.h"
#include "testdll.h"
#include <iostream>
using namespace std;
float Add(float plus1, float plus2)
{
float add_result = plus1 + plus2;
return add_result;
}
char *Hello()
{
return "Hello This is Cpp Addon";
}
int StrLength(char * str)
{
return strlen(str);
}
#pragma once
#ifndef TestDll_H_
#define TestDll_H_
#ifdef MYLIBDLL
#define MYLIBDLL extern "C" _declspec(dllimport)
#else
#define MYLIBDLL extern "C" _declspec(dllexport)
#endif
MYLIBDLL char* Hello();
MYLIBDLL float Add(float plus1, float plus2);
MYLIBDLL int StrLength(char * str);
//You can also write like this:
//extern "C" {
//_declspec(dllexport) int Add(int plus1, int plus2);
//};
#endif
LIBRARY "MyDLL"
EXPORTS
Add @1
npm install --global windows-build-tools