0

I would like to capture workstation lock and unlock and user logon and logoff events. I already wrote a program which overrides the WndProc function, but this does not capture logon and logoff events (as the application is quit or not yet started when this event occurs).

So I thought it might work using a service. I already read Receive Windows Messages in a Service, but I could not find a GetMessage() in C#. How do I accomplish reading Windows messages in a service using C#?

BTW, I also tried the approach in Message pump in .NET Windows service, but I could not figure out how this fits into my problem. Also, the reference Microsoft link is not available any more...

Community
  • 1
  • 1
TomS
  • 467
  • 9
  • 25

1 Answers1

1

The Message pump in a .NET Windows Service article should be what you use. If you need a message pump you will also need a window handle that is receiving the messages. That article shows you how to create a Native Win32 window and how process the messages sent to it. What you are probably missing from that article is that you would just need to register for the MessageHandler.MessageReceived event that gets raised every time a new message comes in.

mageos
  • 1,216
  • 7
  • 15
  • I cannot derive a class from NativeWindow in my C# service, as I cannot use System.Windows.Forms classes in the service. – TomS May 13 '15 at 11:22
  • Why can't you reference the assembly? – mageos May 13 '15 at 11:28
  • don't know... When typing "using System.Windows.Forms", it's not recognized by Visual Studio's IntelliSense. Maybe I could add a reference to the correct assembly (which one would that be?), but I guess that Microsoft had their reasons to not add the references to System.Windows.Forms by default. Is this really the recommended way? – TomS May 13 '15 at 12:50
  • 1
    When you create a Windows Service project, the reference to System.WIndows.Forms is not added. Right Click References and add System.Windows.Forms and then you will be able to use it. – mageos May 13 '15 at 13:08