node.js ffi导入常量

时间:2018-08-27 15:16:33

标签: javascript node.js ffi

假设我有一个共享库libfoo.so,它具有以下源代码:

foo.h

    Set CopyRng1 = sh.Range("B3")
        Set CopyRng2 = sh.Range("C3")
        Set CopyRng3 = sh.Range("D3")
        Set CopyRng4 = sh.Range("G3")
        Set CopyRng5 = sh.Range("C5")
        Set CopyRng6 = sh.Range("A8:j25")
        Set CopyRng7 = sh.Range("A28:j45")

        'Test if there enough rows in the DestSh to copy all the data
        If Last + CopyRng1.Rows.Count > DestSh.Rows.Count Then
            MsgBox "There are not enough rows in the Destsh"
            GoTo ExitTheSub
        End If

        'This example copies values/formats, if you only want to copy the
        'values or want to copy everything look at the example below this macro
DestSh.Cells(Last + 1, "A").Resize(CopyRng1.Rows.Count, CopyRng1.Columns.Count).Value = CopyRng1.Value
DestSh.Cells(Last + 1, "B").Resize(CopyRng2.Rows.Count, CopyRng2.Columns.Count).Value = CopyRng2.Value
DestSh.Cells(Last + 1, "C").Resize(CopyRng3.Rows.Count, CopyRng3.Columns.Count).Value = CopyRng3.Value
DestSh.Cells(Last + 1, "D").Resize(CopyRng4.Rows.Count, CopyRng4.Columns.Count).Value = CopyRng4.Value
DestSh.Cells(Last + 1, "E").Resize(CopyRng5.Rows.Count, CopyRng5.Columns.Count).Value = CopyRng5.Value
DestSh.Cells(Last + 1, "F").Resize(CopyRng6.Rows.Count, CopyRng6.Columns.Count).Value = CopyRng6.Value

Last = LastRow(DestSh)

DestSh.Cells(Last + 1, "F").Resize(CopyRng7.Rows.Count, CopyRng7.Columns.Count).Value = CopyRng7.Value

foo.c

extern cont int fooconst;

使用来自node.js的const int fooconst = 5; 软件包,如何访问ffi

这是一个简单的示例,但是在我想做的事情中,fooconst是一个很大的结构,在字段中带有指针,我不想用javascript重写。

编辑:我不想修改C代码。

1 个答案:

答案 0 :(得分:4)

我想您需要一个函数来返回该值。否则,您将无法访问它。

因此,在您的foo.c中写:

const int fooconst = 5;

int getNum(){
    return fooconst;
}

在您的nodejs应用中:

let ffi = require("ffi");
let ref = require("ref");

let int = ref.types.int;

let fooconst = ffi.Library("./libfoo.so", {
    "getNum": [int, []]
});

let myConst = fooconst.getNum();