11

I am new at Node, I have this simple Node.js server works on windows

Server Code

var ws = require("websocket-server");

var server = ws.createServer();

server.addListener("connection", function(client){
    console.log("new connection");
    client.send("aaaaaa");
    client.addListener("message", function(msg){
        console.log(msg);
    });
});

server.listen(8080);

I just want to call windows API insted of line

console.log(msg);

is there any way to do this without using external library

any ideas?

Mohammed Khabbaz
  • 113
  • 1
  • 1
  • 4

3 Answers3

16

I think node-ffi can help you to do that. node-ffi provides functionality for loading and calling dynamic libraries. With node-ffi you can get access to user32 (for example) lib and call their functions from node.js.

var FFI = require('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
);
Vadim Baryshev
  • 25,689
  • 4
  • 56
  • 48
  • thanks i have tried it, unfortunately doesn't working 'npm install node-ffi' not working for me, so i have asked if there is any solution without using external library – Mohammed Khabbaz Apr 02 '13 at 22:18
  • 4
    As far as i know node-ffi requires python to be installed on your system. Node.js don't have native methods to calling winapi functions. – Vadim Baryshev Apr 02 '13 at 22:20
  • 2
    I have installed it but there is configuration errors : gyp ERR! configure error gyp ERR! stack Error: `gyp` failed with exit code: 1 gyp ERR! stack at ChildProcess.onCpExit (C:\ProgramFiles\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:415:16) and so ... – Mohammed Khabbaz Apr 02 '13 at 22:25
  • Also you need to install `node-gyp` with requirements. Look at this installation instructions: https://github.com/TooTallNate/node-gyp#installation – Vadim Baryshev Apr 02 '13 at 23:04
  • 2
    Worked for me! Though note that the module's been renamed to just "ffi". – Venryx Apr 27 '17 at 01:14
9

I didn't want to edit @Vadim's answer because it is accepted, but I think the package has been renamed to just 'ffi'. This worked for me:

npm install -s ffi

And using @Vadim's source but changing the package name to 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
);
Pete
  • 1,305
  • 1
  • 12
  • 36
Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113
  • This answer is excellent but the buffer api has deprecated using new like this, and also you need a trailing null for c style strings. Code: "function TEXT(text) { return Buffer.from(text + '\0', 'ucs2')}" – Andrew E Jan 20 '21 at 10:44
1

You can also use this NPM package which already has (much of) the Win32 API entered (using ffi) and ready to use from NodeJS: https://github.com/waitingsong/node-win32-api

Venryx
  • 15,624
  • 10
  • 70
  • 96
  • Just giving my two cents, I would not use such library as it not being used by a lot of projects (looking at the NPM stats) the development may stop at any moment leaving you with an outdated library. – Adi Darachi Apr 30 '20 at 14:23