Solved

Windows System Events

Posted on 2003-12-08
12
1,550 Views
Last Modified: 2007-12-19
I’ve been trying to get the answer to this question for a while now (MS news groups, google, etc.) without success.

Is there a way to trap the following events in .NET application:
1) user locks the computer (i.e. ctrl + alt + del -> lock computer)
2) user unlocks the computer
 
I need my C# windows application to react when those events happen. The app will be running on Windows 2000 and possibly XP machines.

Is this possible with .NET?

Extra points available if it’s possible :)

Thanks,
Alex
0
Comment
Question by:astankovic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9899766
It's possible, but very hard. Also you will have to use lower level code, C/C++ probably. Have a look at this discussion, it is about trapping the Ctrl + Alt + Del key combination but has very good discussions and also a link to a commercial tool that does this. Unfortunately no code is available.

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20691493.html
0
 
LVL 7

Accepted Solution

by:
TransBind earned 500 total points
ID: 9899853
You can do this through an API call on Windows 9x (95, 98, ME, etc). I don't recall the name, but if you were to google for this subject you'd probably find something.

You cannot do this API call on NT, 2000, or XP. It requires you to write a GINA.DLL (see MSDN library), which is a Win32 DLL, which you can't write in C#. You'll need C or C++ to do it.

Good luck :)
0
 
LVL 6

Expert Comment

by:martinv
ID: 9900272
Purchasing KeyboardControlSDK (for c/c++) could help as they claims that they can catch ctrl+alt+delete. The drawback is that it uses kernel level hook which must be installed by an administator.

http://www.aspheute.com/english/20000918.asp
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:TheAvenger
ID: 9900304
I think you gave a wrong URL, martinv. The right one (given by me in the above mentioned thread) is:

http://www.meliorasoft.com/kits/keyboard/index.htm
0
 
LVL 6

Expert Comment

by:martinv
ID: 9900435
to TheAvenger:
thank you, you are right. The bad url has  remained in my clipboard from answering another question. Thank you for correcting it.

0
 
LVL 4

Author Comment

by:astankovic
ID: 9900658
Thanks for your comments.

This is for an app that I'm writing on myown with no intentioins to make it available to public (yet :) )...so I'm not likely to purchase third party code.

As for catching the ctrl + alt + delete, i don't think it would work in this case. Some people use ctrl + alt + delete to access the task manager, or they can do "ctrl + alt + delete" but then cancel insted of pressing "lock computer" button. And when the user unlocks the computer it doesn't neceseraly need to press ctrl + alt + delete (the unlock dialog might already be there).
I just gave "ctr + alt + delete -> lock computer" to explain what i mean by "lock computer".

I was hoping there was something like SystemEvents class which exposes events when user logs off/shuts down pc, etc.

0
 
LVL 3

Expert Comment

by:sohilm
ID: 9901307
Just an idea -- try and figure out a way to monitor api calls to user32.dll(not sure how you can do this, but I know its possible), check to see when LockWorkstation api is called. Fire your event

Sohil
0
 
LVL 6

Expert Comment

by:martinv
ID: 9901377
Hm,
some magic with SetWindowsHookEx could work, but I strongly suggest to try it rather in c/c++ than c#. You are adding next level of complexity by using .NET framework for such low-level task and furthermore you'll find a lot of examples in c++ and quite few (if any) in c#.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9902012




here, I've used these WIN API before to do something ver similar.

WIN API Page
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/functions_by_category.asp

Keyboard Hook page
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput.asp

You may also want to look into windows messaging since there are a number of ways to do various low level operations.

For instance...  The start button.

You can either click it with the mouse
Ctrl + esc
windows key


Locking the computer
Ctrl+alt+del click lock/press return (same thing)
windows key + L


etc...


The only way to really check for these things is to either

1) hook the windows messages and read them before they get processed
2) poll the keyboard and determine which keys are pressed

The first option is best, but also more difficult.  Option 2 is pretty basic and straight forward, but you need to play with multi-threading to get it to work properly.  You will also have to make some determination as to the interval you want to poll in, if you don't wait at all you really eat up CPU time.
0
 
LVL 3

Expert Comment

by:sohilm
ID: 9903816
Perhaps you can use a windows hook to monitor a call to user32.dll for LockWorkstation.

http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q318804

I hope this helps.

Sohil
0
 
LVL 4

Author Comment

by:astankovic
ID: 9905096
Big thanks to everybody who commented!

You guys gave me several possibilities that I can research further.

sohilm,
I tired the example from msdn you provided, and it was only trapping mouse events when mouse pointer was in the applications window. That might be because you can’t have global hooks in .net. Based on that, I’, thinking that I wont be able to trap LockWorkstation message because that would require a global hook. I might be wrong there, since I never did anything with hooks.

TransBind,
I think your solution has the most potential. If I can figure out how to write that Win32 DLL and then use it in my .net app that would work perfectly because winlogon can inform the DLL of Lock and Unlock events…

Thanks a lot,
Alex
0
 

Expert Comment

by:nilesh_bhatkhalkar
ID: 9946959
Instead of using gina try using notification dll that could be faster and much easier also less risky to code and use. Just see the "Winlogon Notification Packages" in MSDN. (Applies to windows 2000 and higher)

Also Please try to find out more about SystemParameterInfo function which I think could be used to get the locked and unlocked status.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question