nodejs调用c++dll感想

这几天折腾了几天终于搞定了,nodejs调用c++dll的问题,回想起来有一下几个难点:
1:函数参数,类型如下:
unsigned short arg0 = args[0]->Uint32Value();//其他整数类型类似
args[0]->ToString()
String::NewFromUtf8(isolate, “data”)//创建一个string类型
2:返回结构:

Local<Object> obj = Object::New(isolate);
    obj->Set(String::NewFromUtf8(isolate, "re"), num);
    obj->Set(String::NewFromUtf8(isolate, "handle"), handle);
    Local<Array> arr = Array::New(isolate, MAX_AXIS );
    for (int i = 0; i < MAX_AXIS; ++i)
    {
        arr->Set(i, Number::New(isolate, dta.data[i]) );
    }
    obj->Set(String::NewFromUtf8(isolate, "data"), arr);
    args.GetReturnValue().Set(obj);

代码中构造的string类型供外部调用使用。

总体代码如下:

// cnc_resetconnect.cc
#include 
#include 
#include 
#include 

namespace demo {

using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Number;
using v8::Uint32;
using v8::Integer;
using v8::Exception;
using v8::Array;

#define MAX_AXIS        48
typedef struct odbaxis {
    short   dummy;             /* dummy */
    short   type;              /* axis number */
    long    data[MAX_AXIS];    /* data value */
} ODBAXIS;

void GetOdbaxis(short value, ODBAXIS dta, Isolate* isolate, Local& obj)
{
    Local num = Uint32::New(isolate, value);
    Local dummy = Uint32::New(isolate, dta.dummy);
    Local type = Uint32::New(isolate, dta.type);
    Local arr = Array::New(isolate, MAX_AXIS );

    for (int i = 0; i < MAX_AXIS; ++i)
    {
        arr->Set(i, Number::New(isolate, dta.data[i]) );
    }

    obj->Set(String::NewFromUtf8(isolate, "re"), num);
    obj->Set(String::NewFromUtf8(isolate, "dummy"), dummy);
    obj->Set(String::NewFromUtf8(isolate, "type"), type);
    obj->Set(String::NewFromUtf8(isolate, "data"), arr);
}

/* read machine axis position */
//FWLIBAPI short WINAPI cnc_machine(unsigned short, short, short, ODBAXIS *);
void Cnc_machine(const FunctionCallbackInfo& args) {
    Isolate* isolate = args.GetIsolate();

    // Check the number of arguments passed.
    if (args.Length() != 4) {
        // Throw an Error that is passed back to JavaScript
        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, "Wrong number of arguments")));
        return;
    }

    HINSTANCE hDLL;
    hDLL = LoadLibrary("Fwlib32.dll");//加载动态链接库Fwlib32.dll文件;
    if (!hDLL) {
        // Throw an Error that is passed back to JavaScript
        DWORD dRe = GetLastError();
        char sz[4];
        sprintf(sz, "cat not get lib, error is %d", dRe);
        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, sz)));
        return;
    }

    typedef  short(WINAPI *pMax)(unsigned short, short, short, ODBAXIS *);//函数指针
    pMax pCnc_resetconnect = NULL;
    pCnc_resetconnect = (pMax)GetProcAddress(hDLL, "cnc_machine");
    if (!pCnc_resetconnect) {
        // Throw an Error that is passed back to JavaScript
        isolate->ThrowException(Exception::TypeError(
            String::NewFromUtf8(isolate, "can not get function: cnc_machine from Fwlib32.dll.")));
        return;
    }

    unsigned short arg0 = args[0]->Uint32Value();
    unsigned short arg1 = args[1]->Uint32Value();
    unsigned short arg2 = args[2]->Uint32Value();
    ODBAXIS obj = {};
    short value = pCnc_resetconnect(arg0, arg1, arg2, &obj);

    Local objR = Object::New(isolate);
    GetOdbaxis(value, obj, isolate, objR);
    args.GetReturnValue().Set(objR/*String::NewFromUtf8(isolate, pReturn)*/);
    //Local num = Uint32::New(isolate, value);

    // Set the return value (using the passed in
    // FunctionCallbackInfo&)
    //args.GetReturnValue().Set(num);
}

void init(Local exports) {
    NODE_SET_METHOD(exports, "cnc_machine", Cnc_machine);
}

NODE_MODULE(addon, init)

}  // namespace demo






你可能感兴趣的:(nodejs)