Search code examples
c++node.jsnode-gypnode.js-addon

GetLastInputInfo fails in node addon


My goal is to make a module which provides access to the last time of user interaction (Client side app - not a server app). The Windows API has a function called GetLastInputInfo (https://msdn.microsoft.com/en-us/library/windows/desktop/ms646302(v=vs.85).aspx). Below is the code which should load the time information into last_input and it returns 0/1 for failure/success. Unfortunately, it fails every time.

Addon code:

#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)

Any thoughts?


Solution

  • LASTINPUTINFO structure has member cbSize, that should be initialized:

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

    It's a common way for versioning in Windows API.