Sunday, 9 April 2017

Detect when the user unlocks the workstation

I'm trying to detect when a user unlocks their workstation. The following code attempts to register the window to receive session change messages using WTSRegisterSessionNotification(). Supposedly after this I can listen for WM_WTSSESSION_CHANGE which can contain WTS_SESSION_UNLOCK as a param.

Issue: Currently WTSRegisterSessionNotification() always returns false.

Anyone know how I can achieve this? I'm on Windows 10 btw.

var ffi = require('ffi');
var winctl = require('winctl');

var hwnd = winctl.GetActiveWindow().getHwnd();

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

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 FALSE...?

// Use Electron to hook the windows message loop (Not really relevant to the question)
this.win.hookWindowMessage(WM_WTSSESSION_CHANGE, (wParam, lParam) => {
  alert('Session change detected');
});



via JP_

No comments:

Post a Comment