Nodejs deep interaction with C language

Source: Internet
Author: User

2, Node-ffi

1) Nodejs reading data from C language

Factorial.c

#include <stdint.h>#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#include<string.h>#ifDefined (WIN32) | | Defined (_WIN32)#defineEXPORT __declspec (dllexport)#else#defineEXPORT#endiftypedefstruct{    DoubleLon; Doublelat;}gps_info_t;gps_info_t*GPS;voidGet_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;returnGPS;}

Factorial.js

varFFI = require ('.. /node-ffi-master/')varRefstruct = require ('ref-struct');varRefarray = require ('Ref-array');var ref= Require ('ref');//GPS date structvarGPS =refstruct ({'Lon':ref. Types.Double,    'lat':ref. Types.Double,});varGpsstructarraytype =Refarray (GPS);varGps_info = Gpsstructarraytype (Ten);varLibfactorial = Ffi. Library ('./libfactorial', {  'Get_gps_info': [Gpsstructarraytype, ['void']],}); Gps_info= Libfactorial.get_gps_info (0) Console.log ('Your Output:'+ gps_info[0].lon) Console.log ('Your Output:'+ gps_info[0].lat)

2) Nodejs write data to C language

Factorial.c

#include <stdint.h>#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#include<string.h>#ifDefined (WIN32) | | Defined (_WIN32)#defineEXPORT __declspec (dllexport)#else#defineEXPORT#endiftypedefstruct{    Char*user_name; Char*passwd; Char*dev_id;} user_info_t;user_info_t*user;voidPut_user_info_shm_init (void) {User= (user_info_t *)malloc(sizeof(user_info_t)); User->user_name = (Char*)malloc( +*sizeof(Char)); User-&GT;PASSWD = (Char*)malloc( +*sizeof(Char)); User->dev_id = (Char*)malloc( +*sizeof(Char));} EXPORTvoidPut_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

varFFI = require ('.. /node-ffi-master/')varRefstruct = require ('ref-struct');varRefarray = require ('Ref-array');var ref= Require ('ref');//UI Date structvaruser =refstruct ({'user_name':'string',    'passwd':'string',    'dev_id':'string',});varUserstructarraytype =Refarray (user);varUser_info = Userstructarraytype (1); user_info[0].user_name ='ZDD'; user_info[0].PASSWD ='123'; user_info[0].dev_id ='65535';varLibfactorial = Ffi. Library ('./libfactorial', {  'Put_user_info': ['void', [Userstructarraytype]]}); Libfactorial.put_user_info (User_info) Console.log ('Your Input:'+ user_info[0].user_name) Console.log ('Your Input:'+ user_info[0].passwd) Console.log ('Your Input:'+ user_info[0].DEV_ID)

Nodejs deep interaction with C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.