我想从节点 js 加载一个 dll 文件。这是头文件:
#pragma once
#ifdef __cplusplus
#define EXAMPLE __declspec(dllexport)
extern "C" {
EXAMPLE int Add(int, int);
}
#endif
Run Code Online (Sandbox Code Playgroud)
在编译为,我选择“编译为 C 代码”
在主动解决方案平台中,我选择 x64
然后,我使用 ffi 模块加载它:
var ffi = require('ffi');
var Lib = ffi.Library('test', {'Add' : ['int',['int','int']]});
Run Code Online (Sandbox Code Playgroud)
但我得到了一个错误:
C:\Users\TheHai\node_modules\ffi\lib\dynamic_library.js:112
throw new Error('Dynamic Symbol Retrieval Error: ' + this.error())
^
Error: Dynamic Symbol Retrieval Error: Win32 error 127
at DynamicLibrary.get (C:\Users\TheHai\node_modules\ffi\lib\dynamic_library.js:112:11)
at C:\Users\TheHai\node_modules\ffi\lib\library.js:50:19
at Array.forEach (native)
at Object.Library (C:\Users\TheHai\node_modules\ffi\lib\library.js:47:28)
at Object.<anonymous> (C:\Users\TheHai\Downloads\Compressed\nodejs-websocket-master\samples\chat\server.js:8:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10) …Run Code Online (Sandbox Code Playgroud) 我是 nodejs 的新手。我正在尝试从 node-js 的 dll 中调用 c++ API。到目前为止我发现可以使用ffi来做到这一点。我按照安装说明安装了必要的构建工具。但是当我运行时,npm install ffi我收到以下错误。
难道我做错了什么?
**d:\codelathe\electronpoc\practice\dll\electroncallingdll\node_modules\ref\src\binding.cc(222): error C2661: 'v8::Value:
:BooleanValue': no overloaded function takes 0 arguments**
[D:\CodeLathe\ElectronPOC\Practice\dll\ElectroncallingDll\node
_modules\ref\build\binding.vcxproj]
d:\codelathe\electronpoc\practice\dll\electroncallingdll\node_modules\ref\src\binding.cc(253): error C2660: 'v8::Value:
:Uint32Value': function does not take 0 arguments [D:\CodeLathe\ElectronPOC\Practice\dll\ElectroncallingDll\node_module
s\ref\build\binding.vcxproj]
c:\users\nikhe\.node-gyp\12.0.0\include\node\v8.h(2567): note: see declaration of 'v8::Value::Uint32Value' (compiling
source file ..\src\binding.cc)
d:\codelathe\electronpoc\practice\dll\electroncallingdll\node_modules\ref\src\binding.cc(360): error C2664: 'v8::String
::Utf8Value::Utf8Value(const v8::String::Utf8Value &)': cannot convert argument 1 from 'v8::Local<v8::Value>' to 'const
v8::String::Utf8Value &' [D:\CodeLathe\ElectronPOC\Practice\dll\ElectroncallingDll\node_modules\ref\build\binding.vcxp
roj]
d:\codelathe\electronpoc\practice\dll\electroncallingdll\node_modules\ref\src\binding.cc(360): note: Reason: cannot c
onvert from 'v8::Local<v8::Value>' to 'const v8::String::Utf8Value' …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 node-ffi 从 node.js 中使用 libvlc,虽然它似乎适用于一般的基本媒体播放器功能,但当我尝试使用时,我的程序中不断出现崩溃、分段错误和一般冻结libvlc 的异步事件系统并将其与节点的 EventEmitter 集成。到目前为止,我使用的代码托管在https://gist.github.com/2644721,但似乎不起作用。
GDB 产生了好坏参半的结果,但我收到的最后一次崩溃是:
Program received signal SIGSEGV, Segmentation fault.
0x000000000057cc86 in v8::Function::Call(v8::Handle<v8::Object>, int, v8::Handle<v8::Value>*) ()
(gdb) bt
#0 0x000000000057cc86 in v8::Function::Call(v8::Handle<v8::Object>, int, v8::Handle<v8::Value>*) ()
#1 0x00007ffff5997a41 in CallbackInfo::DispatchToV8(CallbackInfo*, void*, void**) ()
from /home/adam/node_modules/node-ffi/compiled/0.6/linux/x64/ffi_bindings.node
#2 0x00007ffff5997adb in CallbackInfo::WatcherCallback(uv_async_s*, int) ()
from /home/adam/node_modules/node-ffi/compiled/0.6/linux/x64/ffi_bindings.node
#3 0x00000000007be12f in ev_invoke_pending ()
#4 0x00000000007c2087 in ev_run ()
#5 0x00000000007b597f in uv_run ()
#6 0x000000000052a147 in node::Start(int, char**) ()
#7 0x00007ffff63ca76d in __libc_start_main ()
from /lib/x86_64-linux-gnu/libc.so.6
#8 …Run Code Online (Sandbox Code Playgroud) 我正在尝试monetdb与之联系node.js.我有一个简单的(20行)c程序,可以moentdb使用mapi库进行查询.
我可以使用这些库来构建node.js使用这些库并连接到的东西(模块/插件)monetdb吗?
(使用odbc是一种选择,但它有其自身的缺点.)
Update1:
node-ffi非常棒.我能够很容易地创建一个fetch表程序.(例如,我添加了我的工作代码.)
所以,如果我有3个选项
1. ODBC
2. node-ffi
3. ac程序获取数据库数据并通过socket监听node.js的连接
在性能方面,这是更好的实现选择,如果我没有多少时间为node.js开发插件
var ffi = require("ffi");
var libmylibrary = ffi.Library('/usr/local/lib/libmapi.so', {
"mapi_connect":["int",["string",'int',"string","string","string","string"]],
"mapi_query":['int',["int","string"]],
"mapi_fetch_row":["int",["int"]],
"mapi_fetch_field":["string",["int","int"]]
});
var res = libmylibrary.mapi_connect("localhost", 50000,"monetdb", "monetdb", "sql", "demo");
console.log(res);
var ret=libmylibrary.mapi_query(res,"select * from table");
while(libmylibrary.mapi_fetch_row(ret)){
console.log(libmylibrary.mapi_fetch_field(ret,0));
console.log(libmylibrary.mapi_fetch_field(ret,1));
}
Run Code Online (Sandbox Code Playgroud)
更新2:
以上代码不建议用于生产用途...它不使用node.js的异步功能,所以请将它用于婴儿步骤
我正在尝试为我的soup.io博客构建一个简单的rust rss'harvester',然后将这些条目发布到带有node.js的diaspora(因为有一个npm包)
我想学习如何使用节点生锈,这就是我正在构建这个项目的原因.
我的问题是我不知道如何ffi用正确的类型调用函数.
var lib = ffi.Library('target/debug/libmain', {
'get_soup': ['Vec<Post>', ['String']]
});
Run Code Online (Sandbox Code Playgroud)
这'Vec<Post>'不起作用.我知道我必须使用ref它.但我真的不知道实际上是怎么做的.
我知道我必须将锈类型转换为javascript吗?
我如何Vec<Post>在我的ffi功能中使用?
我的github项目:Realtin/suppe
这里有相关代码:
extern crate rss;
extern crate hyper;
use rss::Rss;
use std::io::prelude::*;
#[derive(Debug)]
pub struct Post {
title: String,
link: String,
description: String,
}
fn main() {
let user = "realtin".to_string();
let vec = get_soup(&user);
println!("{:?}", vec[vec.len()-1]);
}
#[no_mangle]
pub extern fn get_soup(user: &str) ->Vec<Post>{
let url = format!("http://{}.soup.io/rss", user);
let mut vec = …Run Code Online (Sandbox Code Playgroud) 我正在使用 Node.js ffi插件来调用 C++ DLL。
我遇到的问题是我提供的结构 - 它包含一个 char 数组 - 我不相信我的设置正确。结果我无法访问内容。
C++头文件中的例程定义:
int GetSysConfig(MyConfig * config);
Run Code Online (Sandbox Code Playgroud)
MyConfig结构体在 C++ 中定义如下:
typedef struct{
int attribute;
char path[256];
}MyConfig;
Run Code Online (Sandbox Code Playgroud)
我对应的 Node.js 结构定义:
var ffi = require('ffi');
var ref = require('ref');
var StructType = require('ref-struct');
var ArrayType = require('ref-array');
// This seems to be the problematic part?
var charArray = ArrayType('char');
charArray.length = 256;
var MyConfig = StructType({
'attribute' : 'int',
'path' : charArray
})
Run Code Online (Sandbox Code Playgroud)
注意:下面是我从 Node.js 调用 DLL 的地方 …
我正在尝试使用Nodejs v6.2.0在我的Windows 7 PC上运行node-ffi的应用程序,但它崩溃时出现以下错误.我尝试了各种方法,但没有解决我的问题
E:\iwork\workbase\myapp\node_modules\bindings\bindings.js:91 Uncaught Error: Could not locate the bindings file. Tried:
? E:\iwork\workbase\myapp\node_modules\ref\build\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\build\Debug\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\build\Release\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\out\Debug\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\Debug\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\out\Release\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\Release\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\build\default\binding.node
? E:\iwork\workbase\myapp\node_modules\ref\compiled\6.1.0\win32\ia32\binding.node
Run Code Online (Sandbox Code Playgroud) 我使用node-ffi调用EnumPrintersA / EnumPrintersW函数来获取可从我的 PC 访问的本地打印机列表。
您应该创建一个缓冲区,该缓冲区将由 EnumPrinters 函数填充信息。
但您不知道所需的缓冲区大小。
在这种情况下,您需要执行EnumPrintersA / EnumPrintersW两次。
在第一次调用期间,该函数计算有关打印机信息的内存量,在第二次调用期间,该函数用有关打印机的信息填充缓冲区。
对于EnumPrinters函数的 Unicode 版本,打印机名称中的每个字母将在 Windows 中使用两个字符进行编码。
为什么第一次调用EnumPrintersW返回与第一次调用EnumPrintersA相同的所需内存量?
Unicode 字符串的长度是非 unicode 字符串的两倍,但所需的缓冲区大小相同。
var ffi = require('ffi')
var ref = require('ref')
var Struct = require('ref-struct')
var wchar_t = require('ref-wchar')
var int = ref.types.int
var intPtr = ref.refType(ref.types.int)
var wchar_string = wchar_t.string
var getPrintersA = function getPrinters() {
var PRINTER_INFO_4A = Struct({
'pPrinterName' : ref.types.CString,
'pServerName' : ref.types.CString,
'Attributes' : …Run Code Online (Sandbox Code Playgroud) 我创建了一个电子应用程序,它使用两个 NodeJS 本机模块node ref和node ffi. 该应用程序目前正在 Windows 上开发,Windows 可执行文件是在electro-builder的帮助下生成的。这一切都工作得很好,并且该应用程序已正确安装在 Windows 中。由于两者ffi and ref都是本机模块,我只是使用electro-rebuild为平台 win32 重建它们(我猜内部使用node-gyp)。
但问题是,我的 CI/CD 服务器是 Ubuntu 16,我需要在那里生成 Windows 可执行文件。如果我的应用程序没有节点的本机插件,它工作得很好,但是包含本机模块,会生成 exe 文件,但在启动时它说%1 不是 win32 应用程序,原因是,本机 deps 是为Linux平台而不是windows。
所以我想知道,有没有办法在 Linux 机器上为 win32 平台重建节点本机模块,或者如果不可能,那么我们如何.node跨平台使用预构建的文件。
我有一个遗留的Delphi dll,它需要一个json字符串作为输入(pAnsiChar)并返回一个int作为成功或失败.我已经设法使用node-ffi从nodejs连接到dll.但是,我得到返回int值指向无效的json字符串.
有人能指出我如何使用pAnsiChar作为函数参数从节点调用Delphi dll
谢谢