I've now tried this myself. While my solution was correct per se, it wasn't enough. Thus I have now implemented this in
FPC 3.3.1, to make it easier to utilize this. You can either use FPC trunk or you can backport the changes (see at the bottom).
Now if you create a new Daemon application (I assume you use the Lazarus package for that) you need to go to your daemon's definition in the Daemon Mapper and inside
WinBindings.AcceptedCodes enable
wccSessionChange. In your daemon you then add an event handler for the new
OnControlCodeEvent (
not OnControlCode!). The parameters of this event handler will be like those for
LPHANDLER_FUNCTION_EX (minus the
lpContext parameter).
For example (the constants used here are in
JwaWinSvc and
JwaWinUser):
procedure TDaemon1.DataModuleControlCodeEvent(Sender: TCustomDaemon; ACode,
AEventType: DWord; AEventData: Pointer; var Handled: Boolean);
var
evtype, sessionid: String;
begin
if ACode = SERVICE_CONTROL_SESSIONCHANGE then begin
case aEventType of
WTS_CONSOLE_CONNECT:
evtype := 'Console Connect';
WTS_CONSOLE_DISCONNECT:
evtype := 'Console Disconnect';
WTS_REMOTE_CONNECT:
evtype := 'Remote Connect';
WTS_REMOTE_DISCONNECT:
evtype := 'Remote Disconnect';
WTS_SESSION_LOGON:
evtype := 'Session Logon';
WTS_SESSION_LOGOFF:
evtype := 'Session Logoff';
WTS_SESSION_LOCK:
evtype := 'Session Lock';
WTS_SESSION_UNLOCK:
evtype := 'Session Unlock';
WTS_SESSION_REMOTE_CONTROL:
evtype := 'Session Remote Control';
otherwise
evtype := Format('Unknown event type %d', [aEventType]);
end;
if Assigned(aEventData) then
sessionid := Format('%d', [PWTSSESSION_NOTIFICATION(aEventData)^.dwSessionId])
else
sessionid := 'none';
Logger.Info('Session Change of type ''%s'' with session %s', [evtype, sessionid]);
end;
end;
What you do with the received session ID is up to you...
If you want to backport the changes to FPC 3.2.0:
You need the changes of revisions
46326 and
46327. Apply these to your 3.2.0 sources (those that are provided by Lazarus for example).
You then need to do a slight adjustment in
packages/fcl-extra/src/win/daemonapp.inc: in
TDaemonController.ReportStatus.GetAcceptedCodes you need to disable the handling of
wccTimeChange,
wccTriggerEvent and
wccUserModeReboot (this way you don't need to add revision
46325 as well which might lead to the need to compile more than necessary).
Once you've done the changes you go to the
packages/fcl-extra/src directory and execute the following:
fpc -FUpath\to\units -Fiwin daemonapp.pp
The
path\to\units is the path where the compiler FPC 3.2.0 units are located in case of Lazarus that should be
fpc\3.2.0\units\x86_64-win64\fcl-extra (or
i386-win32 if you use the 32-bit variant).