Recent

Author Topic: How to get notified, when user unlocks workstation  (Read 368 times)

tboege

  • New Member
  • *
  • Posts: 16
How to get notified, when user unlocks workstation
« on: March 17, 2023, 03:11:46 pm »
I would like my program to be notified, when a user unlocks windows (eg. after windows+l or automatic LockWorkStation by screensaver og gpo).
I have tried the "sessionchangetest.zip" from https://www.lazarusforum.de/viewtopic.php?f=55&t=9059
I have changed "Execution Level" to "asInvoker"
The codes is like:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, windows, LMessages;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Button3: TButton;
  18.     Memo1: TMemo;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure Button2Click(Sender: TObject);
  21.     procedure Button3Click(Sender: TObject);
  22.     procedure WndProc(var TheMessage: TLMessage); override;
  23.   private
  24.     { private declarations }
  25.   public
  26.     { public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. function WTSRegisterSessionNotification(hWnd: HWND; dwFlags: DWORD): BOOL; stdcall external 'Wtsapi32.dll';
  39. function WTSUnRegisterSessionNotification(hWnd: HWND): BOOL; stdcall external 'Wtsapi32.dll';
  40. function LockWorkStation: BOOL; stdcall external 'User32.dll';
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43. begin
  44.   SetLastError(0);
  45.   if WTSRegisterSessionNotification(WindowHandle,0) then Memo1.Lines.Add('REGISTRED');
  46.   if GetLastError<>0 then Memo1.Lines.Add('ERROR: '+IntToStr(GetLastError));
  47. end;
  48.  
  49. procedure TForm1.Button2Click(Sender: TObject);
  50. begin
  51.   SetLastError(0);
  52.   if WTSUnRegisterSessionNotification(WindowHandle) then Memo1.Lines.Add('UNREGISTRED');
  53.   if GetLastError<>0 then Memo1.Lines.Add('ERROR: '+IntToStr(GetLastError));
  54. end;
  55.  
  56. procedure TForm1.Button3Click(Sender: TObject);
  57. begin
  58.   LockWorkStation;
  59. end;
  60.  
  61. procedure TForm1.WndProc(var TheMessage: TLMessage);
  62. begin
  63.   if TheMessage.msg=WM_WTSSESSION_CHANGE then
  64.   begin
  65.     Memo1.Lines.Add('- WM_WTSSESSION_CHANGE -');
  66.   end;
  67.   inherited WndProc(TheMessage);
  68. end;
  69.  
  70. end.
  71.  

If I try to register (by pressing Button1), GetLastError returns 5 - and when the workstation is locked/unlocked, I never get to  line 65: Memo1.Lines.Add('- WM_WTSSESSION_CHANGE -');
The result is the same, if i run the program with elevated rigths.

Any suggestions?

GetMem

  • Hero Member
  • *****
  • Posts: 4090

tboege

  • New Member
  • *
  • Posts: 16
Re: How to get notified, when user unlocks workstation
« Reply #2 on: March 17, 2023, 04:51:15 pm »
@GetMem
Thank You - it solved my problem (and it works when compiled for 64bit as well as when compiled for 32bit)

 

TinyPortal © 2005-2018