在Windows上创建node.js本机扩展 [英] Create a node.js native extension on Windows
问题描述
我正在尝试从node.js应用程序中访问特定的本机Windows API调用。我相信最好的方法是在我的node.js应用程序中创建一个本机扩展/插件到节点,然后从JavaScript调用。
I'm trying to access a particular native Windows API call from within a node.js application. I believe that the best way to do this is to create a native extension / addon to node that I can require in my node.js application and call from the JavaScript.
我在哪里可以找到有关如何实际编译此类可执行文件的资源?我可以找到有关如何编写它的说明,但所有构建说明似乎都适用于Linux。我很好用mingw编译g ++,我不需要使用Visual Studio。另外,在哪里可以找到编译扩展所需的头文件?
Where can I find resources on how to actually compile this sort of executable? I can find instructions on how to write it, but all the build instructions seem to be for Linux. I'm fine with compiling through g++ on mingw, I don't need to use Visual Studio. Also, where do I find the header files that I need in order to compile the extension?
推荐答案
我认为node-ffi 将为您提供帮助。这很简单,也很有效。
I think node-ffi will help you. It's simple and it's works.
npm install ffi
var FFI = require('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
);
这篇关于在Windows上创建node.js本机扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!