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:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, windows, LMessages;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure WndProc(var TheMessage: TLMessage); override;
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function WTSRegisterSessionNotification(hWnd: HWND; dwFlags: DWORD): BOOL; stdcall external 'Wtsapi32.dll';
function WTSUnRegisterSessionNotification(hWnd: HWND): BOOL; stdcall external 'Wtsapi32.dll';
function LockWorkStation: BOOL; stdcall external 'User32.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
SetLastError(0);
if WTSRegisterSessionNotification(WindowHandle,0) then Memo1.Lines.Add('REGISTRED');
if GetLastError<>0 then Memo1.Lines.Add('ERROR: '+IntToStr(GetLastError));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SetLastError(0);
if WTSUnRegisterSessionNotification(WindowHandle) then Memo1.Lines.Add('UNREGISTRED');
if GetLastError<>0 then Memo1.Lines.Add('ERROR: '+IntToStr(GetLastError));
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
LockWorkStation;
end;
procedure TForm1.WndProc(var TheMessage: TLMessage);
begin
if TheMessage.msg=WM_WTSSESSION_CHANGE then
begin
Memo1.Lines.Add('- WM_WTSSESSION_CHANGE -');
end;
inherited WndProc(TheMessage);
end;
end.
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?