| Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
const WM_WTSSESSION_CHANGE = $2B1; WTS_SESSION_LOCK = 7; WTS_SESSION_UNLOCK = 8; NOTIFY_FOR_THIS_SESSION = 0; NOTIFY_FOR_ALL_SESSIONS = 1;
type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); protected procedure OnWMWTSessionChange(var Msg: TMessage); message WM_WTSSESSION_CHANGE; private { Private declarations } public FNotification: BOOL; { Public declarations } end;
var Form1: TForm1;
// Routines function RegisterSessionNotification(Wnd: HWND; dwFlags: DWORD): BOOL; function UnRegisterSessionNotification(Wnd: HWND): BOOL;
implementation
{$R *.dfm}
function RegisterSessionNotification(Wnd: HWND; dwFlags: DWORD): BOOL; type // WTS API TWTSRegisterSessionNotification = function(Wnd: HWND; dwFlags: DWORD): BOOL; stdcall; var hWTSapi32dll: THandle; WTSRegisterSessionNotification: TWTSRegisterSessionNotification; begin Result := False; hWTSAPI32DLL := LoadLibrary('Wtsapi32.dll'); if (hWTSAPI32DLL > 0) then try @WTSRegisterSessionNotification := GetProcAddress(hWTSAPI32DLL, 'WTSRegisterSessionNotification'); if Assigned(WTSRegisterSessionNotification) then Result := WTSRegisterSessionNotification(Wnd, dwFlags); finally if hWTSAPI32DLL > 0 then FreeLibrary(hWTSAPI32DLL); end; end;
function UnRegisterSessionNotification(Wnd: HWND): BOOL; type // WTS API TWTSUnRegisterSessionNotification = function(Wnd: HWND): BOOL; stdcall; var hWTSapi32dll: THandle; WTSUnRegisterSessionNotification: TWTSUnRegisterSessionNotification; begin Result := False; hWTSAPI32DLL := LoadLibrary('Wtsapi32.dll'); if (hWTSAPI32DLL > 0) then try @WTSUnRegisterSessionNotification := GetProcAddress(hWTSAPI32DLL, 'WTSUnRegisterSessionNotification'); if Assigned(WTSUnRegisterSessionNotification) then Result := WTSUnRegisterSessionNotification(Wnd); finally if hWTSAPI32DLL > 0 then FreeLibrary(hWTSAPI32DLL); end; end;
procedure TForm1.FormCreate(Sender: TObject); begin FNotification := RegisterSessionNotification(Handle, NOTIFY_FOR_THIS_SESSION); end;
procedure TForm1.OnWMWTSessionChange(var Msg: TMessage); begin case Msg.WParam of WTS_SESSION_LOCK: ShowMessage('WTS_SESSION_LOCK'); WTS_SESSION_UNLOCK: ShowMessage('WTS_SESSION_UNLOCK'); end; end;
procedure TForm1.FormDestroy(Sender: TObject); begin if FNotification then UnRegisterSessionNotification(Handle); end;
end.
|
|