nodejs呼び出しc++dll感想

8352 ワード

この数日間苦労しましたが、ついに解決しました.nodejsがc+dllを呼び出した問題は、思い出してみるといくつかの難しい点があります.1:関数パラメータ、タイプは以下の通りです.unsigned shart 0=args[0]-.Uint 32 Value().他の整数タイプは、args[0]->ToString()String::NewFroomUtf 8(isolate)//stringタイプを作成します.
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