如何通过本地考试<;功能>;作为参数? 我用V8编写了一个NoDE.js的C++库。我在Windows上,我想从我的C++代码中调用Win32 API函数,枚举窗口< /COD>。code>EnumWindows将回调函数和可选回调函数参数作为参数。这个C++库的唯一目标是将代码>枚举窗口< /C> >到JavaScript Land,并使用如下……/P> mymodule.EnumWindows(function (id) { ... }); P>这样,我的C++代码看起来像……/P> BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { // re-cast the Local<Function> and call it with hWnd return true; } Handle<Value> EnumWindows(const Arguments& args) { HandleScope scope; // store callback function auto callback = Local<Function>::Cast(args[0]); // enum windows auto result = EnumWindows(EnumWindowsProc, callback); return scope.Close(Boolean::New(result == 1)); } BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM) { //重播本地并使用hWnd调用它 返回true; } 句柄枚举窗口(常量参数和参数) { 手镜镜; //存储回调函数 自动回调=Local::Cast(args[0]); //枚举窗口 自动结果=EnumWindows(EnumWindowsProc,回调); 返回scope.Close(Boolean::New(result==1)); }

如何通过本地考试<;功能>;作为参数? 我用V8编写了一个NoDE.js的C++库。我在Windows上,我想从我的C++代码中调用Win32 API函数,枚举窗口< /COD>。code>EnumWindows将回调函数和可选回调函数参数作为参数。这个C++库的唯一目标是将代码>枚举窗口< /C> >到JavaScript Land,并使用如下……/P> mymodule.EnumWindows(function (id) { ... }); P>这样,我的C++代码看起来像……/P> BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { // re-cast the Local<Function> and call it with hWnd return true; } Handle<Value> EnumWindows(const Arguments& args) { HandleScope scope; // store callback function auto callback = Local<Function>::Cast(args[0]); // enum windows auto result = EnumWindows(EnumWindowsProc, callback); return scope.Close(Boolean::New(result == 1)); } BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM) { //重播本地并使用hWnd调用它 返回true; } 句柄枚举窗口(常量参数和参数) { 手镜镜; //存储回调函数 自动回调=Local::Cast(args[0]); //枚举窗口 自动结果=EnumWindows(EnumWindowsProc,回调); 返回scope.Close(Boolean::New(result==1)); },c++,node.js,v8,C++,Node.js,V8,我的问题是如何传递本地您可以传递回调的地址: BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { Local<Function>& f = *reinterpret_cast<Local<Function>*>(lParam); // use f return true; } Handle<Value> EnumWindows(const Argu

我的问题是如何传递
本地您可以传递
回调的地址

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    Local<Function>& f = *reinterpret_cast<Local<Function>*>(lParam);

    // use f

    return true;
}

Handle<Value> EnumWindows(const Arguments& args)
{
    HandleScope scope;

    // store callback function
    auto callback = Local<Function>::Cast(args[0]);

    // enum windows
    auto result = EnumWindows(EnumWindowsProc,
                              reinterpret_cast<LPARAM>(&callback));

    return scope.Close(Boolean::New(result == 1));
}
BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM)
{
本地&f=*重新解释铸件(lParam);
//使用f
返回true;
}
句柄枚举窗口(常量参数和参数)
{
手镜镜;
//存储回调函数
自动回调=Local::Cast(args[0]);
//枚举窗口
自动结果=EnumWindows(EnumWindowsProc,
重新解释强制转换(&callback));
返回scope.Close(Boolean::New(result==1));
}
另一种选择是收集句柄以便稍后处理:

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    std::vector<HWND>* p = reinterpret_cast<std::vector<HWND>*>(lParam);
    p->push_back(hWnd);
    return true;
}

Handle<Value> EnumWindows(const Arguments& args)
{
    HandleScope scope;

    // store callback function
    auto callback = Local<Function>::Cast(args[0]);

    // enum windows
    std::vector<HWND> handles;
    auto result = EnumWindows(EnumWindowsProc,
                              reinterpret_cast<LPARAM>(&handles));

    // Do the calls...

    return scope.Close(Boolean::New(result == 1));
}
BOOL回调EnumWindowsProc(HWND-HWND,LPARAM-LPARAM)
{
std::vector*p=重新解释投射(lParam);
p->推回(hWnd);
返回true;
}
句柄枚举窗口(常量参数和参数)
{
手镜镜;
//存储回调函数
自动回调=Local::Cast(args[0]);
//枚举窗口
向量句柄;
自动结果=EnumWindows(EnumWindowsProc,
重新解释铸件(和手柄));
//打电话。。。
返回scope.Close(Boolean::New(result==1));
}