2

I want to know how to receive windows logon and logoff events inside a delphi windows service. The service itself is already built, now I want to show a systray icon that opens a settings/logs window. To show that icon I need to know if a user has logged in...

I have seen some info regarding "System Event Notification Service" for c#, but there is very little info on delphi.

GMEFil
  • 137
  • 8
  • 2
    I trust you understand that the notification icon will not be part of the service process. It needs a separate desktop process. – David Heffernan Apr 12 '13 at 11:00
  • @Ken Your supposed duplicate is a .net question – David Heffernan Apr 12 '13 at 11:04
  • Ken White, I saw that post, but since the dev language was different... David Hefferman, I was trying to avoid that. I have seen c# tutorials on how to do this in 1 process, so I wanted to know if it was possible to do in delphi. But if it is the only way... – GMEFil Apr 12 '13 at 11:05
  • 2
    You can't do it in one process in any language. Services run in session 0 and cannot show notification icons (or indeed any UI) on an interactive desktop. – David Heffernan Apr 12 '13 at 11:17
  • Well, thank you for the advice. I'll look into other possibilities. – GMEFil Apr 12 '13 at 11:21
  • 1
    @David: You're right; it is. Another note to self: No close votes before the coffee fully kicks in. – Ken White Apr 12 '13 at 12:30

1 Answers1

4

Since user sessions are based on RDS (Remote Desktop Services - former Terminal Services) technology you can try WTSRegisterSessionNotification/WTSRegisterSessionNotificationEx APIs. They give you information about various events like:

  • WTS_SESSION_LOGON
  • WTS_SESSION_LOGOFF
  • WTS_REMOTE_CONNECT
  • WTS_REMOTE_DISCONNECT
  • WTS_CONSOLE_CONNECT
  • WTS_CONSOLE_DISCONNECT etc.

Based on the event you can find user's session ID. Then you can start a process in this specific session (i.e. inject a program to a specific RDS session). For example you can start a process in user's session that shows your icon(s). This process can communicate with your service via named pipes, memory mapped files etc.

Personally I use these events to stop GUI intensive work when a user disconnects from a session (i.e. stop updating labels, listboxes, memos etc.)

** These APIs require to have a window that receives notifications. In a service you need to create a hidden one with a message loop (another topic)

Community
  • 1
  • 1
iPath ツ
  • 2,468
  • 20
  • 31