//下面是代码,经过测试运行成功!
var addon = require('./build/Release/addon');
var y=addon.max(2,3);
console.log(y);
其中 dlldemo.dll
里有个 max
函数,求两个 int
整数的最大值;
#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <iostream>
#include <windows.h>
#include "stdafx.h"
using namespace v8;
Handle<Value> Max(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
HINSTANCE hDLL;
hDLL=LoadLibrary("dlldemo.dll");//加载动态链接库dlldemo.dll文件;
typedef int(*pMax)(int a,int b);//函数指针
pMax Max=NULL;
Max=(pMax)GetProcAddress(hDLL,"Max");
if (Max)
{
int A=Max(args[0]->NumberValue(),args[1]->NumberValue());
//cout<<"比较的结果为"<<A;
Local<Number> num = Number::New(A);
return scope.Close(num);
}
//Local<Number> num = Number::New(args[0]->NumberValue() + args[1]->NumberValue());
Local<Number> num = Number::New(123);
return scope.Close(num);
}
void Init(Handle<Object> exports)
{
exports->Set(String::NewSymbol("max"),FunctionTemplate::New(Max)->GetFunction());
}
NODE_MODULE(addon, Init)