Create a node.js native extension on Windows

十年热恋 提交于 2019-11-27 17:32:35
Vadim Baryshev

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
);

This is now out of date. Today, use nan and node-gyp to write and build native modules that work across platforms.


I've gone through this pain myself. Here's a guide that helped me get it working. (PDF) Remember that since Node is now officially supported on Windows, Visual Studio is the recommended build tool for Node on Windows.

Basically:

  • Download and build the Node source code. (See PDF)
  • Create a new VC++ Win32 solution in VS, selecting DLL as the application type in the wizard that follows. Make sure ATL/MFC is unchecked.
  • Write your addon. As an example, here's one I wrote that gets Windows' current DNS settings. Specifically, the project's settings file (vcxproj) will be of interest since one of the toughest parts is getting all the library references/include paths set up. You might want to borrow my config and replace D:\node\ with the location of your Node repo.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!