nodejs与c/c++交互目前主流的方式有两种,node addon c++ 和 node-ffi .

1、node addon c++

1)nodejs从c语言读取数据

addon.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> typedef struct
{
double lon;
double lat;
}gps_info_t; gps_info_t* gps; void get_gps_shm_init(void)
{
gps = (gps_info_t *)malloc(sizeof(gps_info_t));
} Napi::Object CreateObject(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object obj = Napi::Object::New(env); gps->lon = 55.5;
gps->lat = 66.6; printf("send lon: %f\n", gps->lon);
printf("send lat: %f\n", gps->lat); obj.Set(Napi::String::New(env, "lon"), gps->lon);
obj.Set(Napi::String::New(env, "lat"), gps->lat); return obj;
} Napi::Object Init(Napi::Env env, Napi::Object exports) {
get_gps_shm_init();
return Napi::Function::New(env, CreateObject, "createObject");
} NODE_API_MODULE(addon, Init)

addon.js

var addon = require('bindings')('addon');

var obj = addon('gps');

console.log(obj.lon);
console.log(obj.lat); module.exports.obj = obj;

2)nodejs向c语言写数据

addon.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> typedef struct
{
char *user_name;
char *passwd;
char *dev_id;
}user_info_t; user_info_t* user; void put_user_info_shm_init(void)
{
user = (user_info_t *)malloc(sizeof(user_info_t));
user->user_name = (char*)malloc(*sizeof(char));
user->passwd = (char*)malloc(*sizeof(char));
user->dev_id = (char*)malloc(*sizeof(char));
} Napi::Object CreateObject(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object obj = Napi::Object::New(env); memset(user->user_name, , );
memset(user->passwd, , );
memset(user->dev_id, , );
memcpy(user->user_name, info[].ToString(), );
memcpy(user->passwd, info[].ToString(), );
memcpy(user->dev_id, info[].ToString(), ); printf("recv user info (user_name): %s\n", user->user_name);
printf("recv user info (passwd): %s\n", user->passwd);
printf("recv user info (dev_id): %s\n", user->dev_id); return obj;
} Napi::Object Init(Napi::Env env, Napi::Object exports) {
put_user_info_shm_init();
return Napi::Function::New(env, CreateObject, "createObject");
} NODE_API_MODULE(addon, Init)

addon.js

var addon = require('bindings')('addon');

var user_info = {
user_name = 'zdd';
passwd = '123';
dev_id = '65535'
} var obj = addon(user_info.user_name,user_info.passwd,user_info.dev_id); module.exports.user_info = user_info;

nodejs的C/C++拓展,将c/c++源码编译成js模板库

ubuntu下编译

node-gyp configure
node-gyp build

node addon.js

交叉编译
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
export LD=arm-linux-gnueabihf-ld
export RAINLIB=arm-linux-gnueabihf-rainlib
export AR=arm-linux-gnueabihf-ar
export LINK=arm-linux-gnueabihf-g++
node-gyp configure --arch=arm
node-gyp build

node addon.js

2、node-ffi

1)nodejs从c语言读取数据

factorial.c

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h> #if defined(WIN32) || defined(_WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif typedef struct
{
double lon;
double lat;
}gps_info_t; gps_info_t* gps; void get_gps_shm_init(void)
{
gps = (gps_info_t *)malloc(sizeof(gps_info_t));
} EXPORT gps_info_t* get_gps_info(void) {
get_gps_shm_init();
gps->lon = 55.5;
gps->lat = 55.5;return gps;
}

factorial.js

var ffi = require('../node-ffi-master/')
var refStruct = require('ref-struct');
var refArray = require('ref-array');
var ref = require('ref'); //gps date struct
var gps = refStruct({
'lon':ref.types.double,
'lat':ref.types.double,
}); var gpsStructArrayType = refArray(gps);
var gps_info = gpsStructArrayType(); var libfactorial = ffi.Library('./libfactorial', {
'get_gps_info': [ gpsStructArrayType, [ 'void' ] ],
}); gps_info = libfactorial.get_gps_info() console.log('Your output: ' + gps_info[].lon)
console.log('Your output: ' + gps_info[].lat)

2)nodejs向c语言写数据

factorial.c

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h> #if defined(WIN32) || defined(_WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif typedef struct
{
char *user_name;
char *passwd;
char *dev_id;
}user_info_t; user_info_t* user; void put_user_info_shm_init(void)
{
user = (user_info_t *)malloc(sizeof(user_info_t));
user->user_name = (char*)malloc(*sizeof(char));
user->passwd = (char*)malloc(*sizeof(char));
user->dev_id = (char*)malloc(*sizeof(char));
} EXPORT void put_user_info(user_info_t* user_info) {
put_user_info_shm_init();
printf("get userinfo(user_name):%s",user_info->user_name);
printf("get userinfo(user_name):%s",user_info->passwd);
printf("get userinfo(user_name):%s",user_info->dev_id);
}

factorial.js

var ffi = require('../node-ffi-master/')
var refStruct = require('ref-struct');
var refArray = require('ref-array');
var ref = require('ref'); //ui date struct
var user = refStruct({
'user_name':'string',
'passwd':'string',
'dev_id':'string',
}); var userStructArrayType = refArray(user);
var user_info = userStructArrayType(); user_info[].user_name = 'zdd';
user_info[].passwd = '';
user_info[].dev_id = ''; var libfactorial = ffi.Library('./libfactorial', {
'put_user_info': [ 'void', [ userStructArrayType ] ]
}); libfactorial.put_user_info(user_info) console.log('Your input: ' + user_info[].user_name)
console.log('Your input: ' + user_info[].passwd)
console.log('Your input: ' + user_info[].dev_id)

gcc / arm-linux-gnueabihf-gcc -shared -fpic factorial.c -o libfactorial.so

node factorial.js

上面两种方式写了个demo放在github上了

https://github.com/zhoudd1/nodejs_call_c_cc

3、还有一种小众化的方式

通过child_process 方式调用EXE进程来实现数据交互。

var cp= require('child_process');
//同步的方式

letout = cp.execFileSync("testdll.exe", ["arg1","arg2"],{});

//异步的方式

cp.execFile("testdll.exe", ["arg1","arg2"], {}, (error, stdout, stderr) => {
    console.log(stdout);
})
 

这种方式NODE 会接管stdout 和 stderr ,exe中把结果通过 printf 输出。
缺点:需要先生成EXE,EXE 中调用 DLL/so 并且返回结果。
优点:不需要配置 FFI,不受DLL/so 的影响

更多细节

https://www.cnblogs.com/chyingp/p/node-learning-guide-child_process.html

https://blog.csdn.net/zeping891103/article/details/52230175

nodejs与c语言交互应用实例的更多相关文章

  1. python与c语言交互应用实例

    1.python向c语言写数据 1) 先将接收端编译成一个共享链接库gcc/arm-linux-gnueabihf-gcc -o bluetooth_proxy.so -shared -fPIC bl ...

  2. UML标准建模语言与应用实例

    一.基本信息 标题:UML标准建模语言与应用实例 时间:2012 出版源:科技创新导报 领域分类:UML标准建模语言 面向对象 系统分析与设计 二.研究背景 问题定义:UML建模语言用图形来表现典型的 ...

  3. linux下C语言多线程编程实例

    用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...

  4. golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍

    golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍 go语言爬虫框架:gocolly/colly,goquery,colly,chrom ...

  5. FFI (语言交互接口(Foreign Function Interface))

    FFI(Foreign Function Interface)是用来与其它语言交互的接口, 在有些语言里面称为语言绑定(language bindings), Java 里面一般称为 JNI(Java ...

  6. nodejs实现前后端交互

    本人nodejs入门级选手,站在巨人(文殊)的肩膀上学习了一些相关知识,有幸在项目中使用nodejs实现了前后端交互,因此,将整个交互过程记录下来,方便以后学习. 本文从宏观讲述nodejs实现前后端 ...

  7. react UI交互 简单实例

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  8. nodejs+mongoose操作mongodb副本集实例

    继上一篇设置mongodb副本集之后,开始使用nodejs访问mongodb副本集: 1:创建项目     express 项目名称 2:npm install mongoose    安装mongo ...

  9. 第4章-Vue.js 交互及实例的生命周期

    一.学习目标 了解实例生命周期的过程 理解钩子函数的作用 掌握Vue.js过滤器的使用方法 (重点) 能够使用网络请求进行前后端交互 (重点.难点) 二.交互的基本概念 2.1.前端和后端的概念 说明 ...

随机推荐

  1. Redis常用命令解析——INFO, MONITOR, SLOWLOG

    1. INFO info指令返回服务器相关信息,包括: server: General information about the Redis server clients: Client conne ...

  2. 基于Ambari构建自己的大数据平台产品

    目前市场上常见的企业级大数据平台型的产品主流的有两个,一个是Cloudera公司推出的CDH,一个是Hortonworks公司推出的一套HDP,其中HDP是以开源的Ambari作为一个管理监控工具,C ...

  3. BI开发之——Mdx基础语法(2)(转至指尖流淌)

    结合webcast中老师的讲解,现在把基础语法应用通过几个案例应用如下: 一.维度的概念 上图中一个维度(Dimension):Region 改为度下有四个级别(Levels):country.pro ...

  4. NodeJS与Javascript时代

    如果你一直在关注互联网的相关技术,你应该会有这样一种感觉,web技术正在发生着变革,虽然我们不愿相信,但一个事实已经越来越清晰的摆在了眼前:LAMP组合的时代将要成为历史,在web诞生的二十年间,它影 ...

  5. 使用PULL解析XML文件

    转载博文1:http://blog.csdn.net/wangkuifeng0118/article/details/7313241 XmlPull和Sax类似,是基于流(stream)操作文件,然后 ...

  6. UDP也需要现有Server端,然后再有Client端

    UDP编程: DatagramSocket(邮递员):对应数据报的Socket概念,不需要创建两个socket,不可使用输入输出流. DatagramPacket(信件):数据包,是UDP下进行传输数 ...

  7. ubuntu被delete的文件位置

    在-/.local/share/Trash/files下边 可以通过 cd / find -name <filename> 找到盖文件的位置

  8. 修改Apache访问权限

    You don't have permission to access / on this server.错误,居然说我此台服务器上无权限,ok解决办法如下: 找到:apache文件,进入conf文件 ...

  9. jQuery实用技巧必备

    本文实例总结了经典且实用的jQuery代码开发技巧.分享给大家供大家参考.具体如下: 1. 禁止右键点击 $(document).ready(function(){  $(document).bind ...

  10. Hibernate_day01--Hibernate配置文件详解_核心api

    Hibernate映射配置文件(重点) 1 映射配置文件名称和位置没有固定要求 2 映射配置文件中,标签name属性值写实体类相关内容 (1)class标签name属性值实体类全路径 (2)id标签和 ...