我试图检测用户何时解锁其工作站。以下代码尝试使用WTSRegisterSessionNotification()注册窗口以接收会话更改消息。据说在此之后,我可以监听WM_WTSSESSION_CHANGE,它可以包含WTS_SESSION_UNLOCK作为参数。
问题:目前WTSRegisterSessionNotification()始终返回false。
任何人都知道如何实现这一目标?我正在使用Windows 10 btw。
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...?
无可置疑的推荐GetLastError()。 遗憾的是,node-ffi不支持这一点。 https://github.com/node-ffi/node-ffi/issues/261
答案 0 :(得分:0)
You should not be using winctl.GetActiveWindow
to get a HWND because you don't actually know which window is active. As far as I can tell GetActiveWindow calls GetForegroundWindow
internally and that is insane for two reasons:
Create a hidden native window somehow or use a window provided by electron (GetTopLevelNativeWindow
perhaps?)
GetLastError
returns the wrong value because you are calling it too late. There is a thick layer of stuff between you and the native Windows function and something is calling SetLastError
before you regain control. The 'ffi' library needs a way to specify that you want it to call GetLastError on your behalf immediately after calling the requested function.
And finally, a Windows BOOL
is int sized and I'm guessing node.js treats it as a byte?
答案 1 :(得分:0)
结束了对此的放弃。我验证了HWND是正确的,但无论出于何种原因它仍然无效。我最终在C#中编写了一个小函数,并使用node-edge从节点/电子内执行它。