This unit only holds the smalles amount of needed functions, because the WTS-Functions are not included in Borland’s windows unit. Who wants to have an up-to-date win32api should check

http://members.chello.nl/m.vanbrakel2/

!!! The given example will only work on Windows XP or higher !!!
// UNIT SIMPLEWTS START
unit simpleWTS;

interface

uses
Windows;

function WTSRegisterSessionNotification(hWnd: HWND; dwFlags: DWORD): BOOL; stdcall;
function WTSUnRegisterSessionNotification(hWND: HWND): BOOL; stdcall;

const
  NOTIFY_FOR_ALL_SESSIONS  = 1;
  NOTIFY_FOR_THIS_SESSIONS = 0;

implementation

function WTSRegisterSessionNotification;
  external 'wtsapi32.dll' Name 'WTSRegisterSessionNotification';

function WTSUnRegisterSessionNotification;
  external 'wtsapi32.dll' Name 'WTSUnRegisterSessionNotification';

end.

Now the form that should be notified if the Status changed
don’t forget to “use” the simpleWTS-unit

unit UfrmMain;

interface

uses
   { ... everything else like Windows, SysUtils, Controls }, simpleWTS;
type
  TfrmMain = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    FintLockedCount: Integer;  { will save how often the Workstation got locked }
    procedure WndProc(var Message: TMessage); override;
    { we need our own WndProc to catch all those WTS-Messages we need }
  public
  end;

var
  frmMain: TFrmMain;


implementation


procedure TfrmMain.WndProc(var Message: TMessage);
begin
  case Message.Msg of
    WM_WTSSESSION_CHANGE:
      begin
        if Message.wParam = WTS_SESSION_LOCK then
        begin
          Inc(FintLockedCount);
        end;
        if Message.wParam = WTS_SESSION_UNLOCK then
        begin
          Label1.Caption := 'Die Session wurde bereits ' +
            IntToStr(FintLockedCount) + ' mal gesperrt.';
        end;
      end;
  end;
  inherited;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  WTSRegisterSessionNotification(Handle, NOTIFY_FOR_ALL_SESSIONS);
  FintLockedCount := 0;
end;

end.