#include <windows.h>
__declspec(dllexport) int add(int a, int b) {
return a + b;
}
gcc -shared -o add.dll add.c -Wl,–out-implib,libadd.a
-Wl,–add-stdcall-alias是用于确保32位程序可以正确链接到64位DLL的GCC特定选项。如果你在64位机器上编译32位程序,或者反过来,你可能需要这个选项。
#include <windows.h>
#include <stdio.h>
__declspec(dllimport) int add(int a, int b);
int main() {
int result = add(2, 3);
printf("The result is %d\n", result);
return 0;
}
gcc -o myprogram myprogram.c -L. -ladd -Wl,–add-stdcall-alias
并测试myprogram.exe,输出
The result is 5
npm install ffi-napi
const ffi = require('ffi-napi');
const myLib = ffi.Library('path/to/your/dll', {
'Add': ['int', ['int', 'int']],
});
// Now you can use myLib.Add(a, b) to call your DLL function.
const result = myLib.Add(1, 2);
console.log(result); // Outputs: 3
3