javascript 从 node.js msg 调用 Windows API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15775201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:04:20  来源:igfitidea点击:

Call Windows API from node.js msg

javascriptnode.jswinapi

提问by Mohammed Khabbaz

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

我是 Node 的新手,我有这个简单的 Node.js 服务器在 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

我只想调用 Windows API insted of line

console.log(msg);

is there any way to do this without using external library

有没有办法在不使用外部库的情况下做到这一点

any ideas?

有任何想法吗?

回答by Vadim Baryshev

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

我认为node-ffi可以帮助您做到这一点。node-ffi提供加载和调用动态库的功能。有了它,node-ffi您就可以访问user32(例如)lib 并从 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
);

回答by Jason Goemaat

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:

我不想编辑@Vadim 的答案,因为它已被接受,但我认为该软件包已重命名为“ ffi”。这对我有用:

npm install -s ffi

And using @Vadim's source but changing the package name to ffi:

并使用@Vadim 的源代码但将包名称更改为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
);

回答by Venryx

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

您还可以使用这个 NPM 包,它已经输入了(大部分)Win32 API(使用 ffi)并准备从 NodeJS 使用:https: //github.com/waitingsong/node-win32-api