windows里编译node-ffi模块

环境 win7  node9.9  npm6.3  node-gyp3.7.0

npm install node-gyp -g

1.github上下载最新ffi代码

https://github.com/node-ffi/node-ffi

2.用cpm安装ffi需要的模块

3.node-gyp configure

node-gyp build

4.编译好不出错后就可以拷到别处用了。下面代码可以测试编译好了么。调用windows里的user32.dll

var FFI = require('./node_modules/node-ffi');
function TEXT(text){
    return new Buffer(text, 'ucs2').toString('binary');
}
var user32 = new FFI.Library('user32', {
    'MessageBoxW':
        [
            'int32', [ 'int32', 'string', 'string', 'int32' ]
        ]
});
var OK_or_Cancel = user32.MessageBoxW(
    0, TEXT('I am Node.JS!'), TEXT('Hello, World!'), 1
);
console.log(OK_or_Cancel);

  

如果linux下就可以下载node-ffi直接npm i 然后 node-gyp configure   node-gyp build

原文地址:https://www.cnblogs.com/adjk/p/9440832.html