Solved

Windows System Events

Posted on 2003-12-08
12
1,541 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
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:sohilm
Comment Utility
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
Comment Utility
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_
Comment Utility




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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now