GetLastInputInfo 在节点插件中失败

GetLastInputInfo fails in node addon

我的目标是制作一个模块,提供对上次用户交互的访问(客户端应用程序 - 不是服务器应用程序)。 Windows API 有一个叫做 GetLastInputInfo (https://msdn.microsoft.com/en-us/library/windows/desktop/ms646302(v=vs.85).aspx) 的函数。下面是应该将时间信息加载到 last_input 的代码,它是 returns 0/1 for failure/success。不幸的是,它每次都失败。

插件代码:

#include <node.h>
#include <v8.h>

#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>

using namespace v8;

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

    LASTINPUTINFO last_input;
    if (::GetLastInputInfo(&last_input)) {
        return scope.Close(String::New("Success!"));
    }
    else {
        return scope.Close(String::New("Failed for some reason!"));
    }
}

void init(Handle<Object> exports) {
    exports->Set(String::NewSymbol("time_since_input"), FunctionTemplate::New(TimeSinceInput)->GetFunction());
}

NODE_MODULE(addon, init)

有什么想法吗?

LASTINPUTINFO 结构有成员 cbSize,应该对其进行初始化:

The size of the structure, in bytes. This member must be set to sizeof(LASTINPUTINFO).

这是 Windows API 中版本控制的常用方法。