检测用户何时解锁工作站

Detect when the user unlocks the workstation

我正在尝试检测用户何时解锁他们的工作站。以下代码尝试使用 WTSRegisterSessionNotification() 注册 window 以接收会话更改消息。据说在此之后我可以收听 WM_WTSSESSION_CHANGE 可以包含 WTS_SESSION_UNLOCK 作为参数。

问题:当前 WTSRegisterSessionNotification() 总是 returns false。

有谁知道我怎样才能做到这一点?我在 Windows 10 顺便说一下。

var {remote} = require('electron');
var ffi = require('ffi');
var winctl = require('winctl');

var NOTIFY_FOR_ALL_SESSIONS = 1;
var WM_WTSSESSION_CHANGE = parseInt('0x02B1', 16);

var hwnd = remote.getCurrentWindow().getNativeWindowHandle();

var wtsapi32 = ffi.Library('wtsapi32', {
  'WTSRegisterSessionNotification': [ 'bool', [ 'int', 'int' ] ]
});

// Attempt to register
var isregistered = wtsapi32.WTSRegisterSessionNotification(hwnd, NOTIFY_FOR_ALL_SESSIONS);

console.log(isregistered); // <----- RETURNS 0...?

IInspectable 推荐 GetLastError()。 不幸的是,这不是 node-ffi 支持的。 https://github.com/node-ffi/node-ffi/issues/261

您不应该使用 winctl.GetActiveWindow 来获取 HWND,因为您实际上并不知道哪个 window 处于活动状态。据我所知,GetActiveWindow 在内部调用 GetForegroundWindow,这很疯狂,原因有二:

  1. GetForegroundWindow returns 用户桌面上的前景 window 并且可能是另一个应用程序中的 window。
  2. Windows 有一个名为 GetActiveWindow 的实际函数,它与调用 GetForegroundWindow 不同。

以某种方式创建隐藏的原生 window 或使用 electron 提供的 window(也许 GetTopLevelNativeWindow?)

GetLastError returns 错误的值,因为您调用它的时间太晚了。在您和本机 Windows 函数之间有一层厚厚的东西,在您重新获得控制权之前,有东西在调用 SetLastError。 'ffi' 库需要一种方法来指定您希望它在调用请求的函数后立即代表您调用 GetLastError。

最后,Windows BOOL 是 int 大小的,我猜 node.js 将其视为一个字节?

最终放弃了这个。我验证了 HWND 是正确的,但无论出于何种原因,它仍然无法正常工作。我最终用 C# 编写了一个小函数,并使用 node-edge 在 node/electron 中执行了它。