Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a node.js native extension on Windows

Tags:

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.

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?

like image 342
Timothy Armstrong Avatar asked Mar 08 '12 20:03

Timothy Armstrong


People also ask

What is native node JS?

In node, most modules are written in javascript. Some modules, like the fs module are written in C/C++, as you can't edit files from plain javascript. IIRC, these modules are called 'native' because the code for these modules is slightly different depending on the OS node runs on.

Does NVM work on Windows?

Node Version Manager, more commonly called nvm, is the most popular way to install multiple versions of Node. js, but is only available for Mac/Linux and not supported on Windows. Instead, we recommend installing nvm-windows and then using it to install Node. js and Node Package Manager (npm).

Does Nodejs work on Windows?

Node. js makes it possible for you to run JavaScript programs outside of a web browser or on a server. To run a Node. js application on Windows, follow these three steps.


1 Answers

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
);
like image 134
Vadim Baryshev Avatar answered Sep 19 '22 12:09

Vadim Baryshev