473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Detect if Workstation is locked

Hi,
I want to detect in my C# app (can use iterop) if the workstation is locked.
I've seen on the web people saying it's impossible except by looking if the
Screensaver process is running. There must be a clean way of doing it
because MSN detects it and can change your status based on this.

Does someone have a solution to this please?

thanks

ThunderMusic
Sep 15 '06 #1
14 20686
Hello ThunderMusic,

There is no fuction to detect whether the workstration locked. You can detect
it by checking if any window is visible

THi,
TI want to detect in my C# app (can use iterop) if the workstation is
Tlocked.
TI've seen on the web people saying it's impossible except by looking
Tif the
TScreensaver process is running. There must be a clean way of doing it
Tbecause MSN detects it and can change your status based on this.
TDoes someone have a solution to this please?
T>
Tthanks
T>
TThunderMusic
T>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Sep 15 '06 #2
ok, but msn does it very very very well, there's certainly a way... Is there
a way to know the state of the computer, locked/unlocked. It does not have
to be an event. Only a function that returns locked/not locked. maybe via
the user account?

thanks

ThunderMusic

"Michael Nemtsev" <ne*****@msn.comwrote in message
news:17***************************@msnews.microsof t.com...
Hello ThunderMusic,

There is no fuction to detect whether the workstration locked. You can
detect it by checking if any window is visible

THi,
TI want to detect in my C# app (can use iterop) if the workstation is
Tlocked.
TI've seen on the web people saying it's impossible except by looking
Tif the
TScreensaver process is running. There must be a clean way of doing it
Tbecause MSN detects it and can change your status based on this.
TDoes someone have a solution to this please?
TTthanks
TTThunderMusic
T---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche


Sep 15 '06 #3
I just tought about it... maybe a message in the wndproc?
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
ok, but msn does it very very very well, there's certainly a way... Is
there a way to know the state of the computer, locked/unlocked. It does
not have to be an event. Only a function that returns locked/not locked.
maybe via the user account?

thanks

ThunderMusic

"Michael Nemtsev" <ne*****@msn.comwrote in message
news:17***************************@msnews.microsof t.com...
>Hello ThunderMusic,

There is no fuction to detect whether the workstration locked. You can
detect it by checking if any window is visible

THi,
TI want to detect in my C# app (can use iterop) if the workstation is
Tlocked.
TI've seen on the web people saying it's impossible except by looking
Tif the
TScreensaver process is running. There must be a clean way of doing it
Tbecause MSN detects it and can change your status based on this.
TDoes someone have a solution to this please?
TTthanks
TTThunderMusic
T---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche



Sep 15 '06 #4
See the blurb right after the first example in the link below. There may
very well be a more official API function for checking it, but not likely
given what this says (if there were they would have almost certainly pointed
you there given that this is the official source)

http://windowssdk.msdn.microsoft.com.../ms723150.aspx
Sep 15 '06 #5
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
ok, but msn does it very very very well, there's certainly a way... Is
there a way to know the state of the computer, locked/unlocked.
As Michael and John both posted, there IS a way. Test to see if your window
is visible (if it's not, and you haven't specifically made it not visible,
then the session is locked). Of course, you do need a window for that to
work.
It does not have to be an event. Only a function that returns locked/not
locked. maybe via the user account?
Also, see the SessionSwitch SystemEvent. It provides access to the
WM_WTSSESSION_CHANGE window message in .NET. As long as your program is
running, you can use that message to track when session changes occur
(including locking and unlocking).

By the way, I'd disagree that MSN does this "very well". I've seen it get
confused plenty of times. :)

Pete
Sep 15 '06 #6
I've found a solution, tought maybe it's not the best way, it does the trick
pretty well :

http://www.thescripts.com/forum/thread225183.html

thanks to all

ThunderMusic

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:12*************@corp.supernews.com...
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
>ok, but msn does it very very very well, there's certainly a way... Is
there a way to know the state of the computer, locked/unlocked.

As Michael and John both posted, there IS a way. Test to see if your
window is visible (if it's not, and you haven't specifically made it not
visible, then the session is locked). Of course, you do need a window for
that to work.
>It does not have to be an event. Only a function that returns locked/not
locked. maybe via the user account?

Also, see the SessionSwitch SystemEvent. It provides access to the
WM_WTSSESSION_CHANGE window message in .NET. As long as your program is
running, you can use that message to track when session changes occur
(including locking and unlocking).

By the way, I'd disagree that MSN does this "very well". I've seen it get
confused plenty of times. :)

Pete

Sep 15 '06 #7

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:OA**************@TK2MSFTNGP06.phx.gbl...
| Hi,
| I want to detect in my C# app (can use iterop) if the workstation is
locked.
| I've seen on the web people saying it's impossible except by looking if
the
| Screensaver process is running. There must be a clean way of doing it
| because MSN detects it and can change your status based on this.
|
| Does someone have a solution to this please?
|
| thanks
|
| ThunderMusic
|
|

When running on XP or higher you can register for session change
notifications by calling (through PInvoke) the TS API
"WTSRegisterSessionNotification", note that you need a Windows procedure to
capture the messages.
Here are the PInvoke declarations..

[DllImport("wtsapi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd,
int dwFlags);

[DllImport("wtsapi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr
hWnd);

and here how to register...

private const int NotifyForThisSession = 0; // This session only

WTSRegisterSessionNotification(this.Handle, NotifyForThisSession);

and here the messages you can check..

private const int SessionChangeMessage = 0x02B1;
private const int SessionLockParam = 0x7;
private const int SessionUnlockParam = 0x8;

in your overriden WndProc.

protected override void WndProc(ref Message m)
{
// check for session change notifications
if(m.Msg == SessionChangeMessage)
{
if(m.WParam.ToInt32() == SessionLockParam)
OnSessionLock(); // Do something when locked
else if(m.WParam.ToInt32() == SessionUnlockParam)
OnSessionUnlock(); // Do something when unlocked
}

base.WndProc(ref m);
return;
}
void OnSessionLock() {...}

...
void OnSessionUnlock() {...}
call "WTSUnRegisterSessionNotification" when you no longer need to be
notified...

Willy.
Sep 15 '06 #8
I've found a solution, tought maybe it's not the best way, it does the
trick pretty well :
I recommend you investigate this thoroughly. People often apply esoteric
ways of doing things but unless the techniques are officially sanctioned by
MSFT, you're asking for trouble (don't be surprised if it breaks one day
IOW).
Sep 15 '06 #9
cool!! wow!! thanks a lot... ;)
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:OA**************@TK2MSFTNGP06.phx.gbl...
| Hi,
| I want to detect in my C# app (can use iterop) if the workstation is
locked.
| I've seen on the web people saying it's impossible except by looking if
the
| Screensaver process is running. There must be a clean way of doing it
| because MSN detects it and can change your status based on this.
|
| Does someone have a solution to this please?
|
| thanks
|
| ThunderMusic
|
|

When running on XP or higher you can register for session change
notifications by calling (through PInvoke) the TS API
"WTSRegisterSessionNotification", note that you need a Windows procedure
to
capture the messages.
Here are the PInvoke declarations..

[DllImport("wtsapi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd,
int dwFlags);

[DllImport("wtsapi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr
hWnd);

and here how to register...

private const int NotifyForThisSession = 0; // This session only

WTSRegisterSessionNotification(this.Handle, NotifyForThisSession);

and here the messages you can check..

private const int SessionChangeMessage = 0x02B1;
private const int SessionLockParam = 0x7;
private const int SessionUnlockParam = 0x8;

in your overriden WndProc.

protected override void WndProc(ref Message m)
{
// check for session change notifications
if(m.Msg == SessionChangeMessage)
{
if(m.WParam.ToInt32() == SessionLockParam)
OnSessionLock(); // Do something when locked
else if(m.WParam.ToInt32() == SessionUnlockParam)
OnSessionUnlock(); // Do something when unlocked
}

base.WndProc(ref m);
return;
}
void OnSessionLock() {...}

..
void OnSessionUnlock() {...}
call "WTSUnRegisterSessionNotification" when you no longer need to be
notified...

Willy.


Sep 15 '06 #10
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
I've found a solution,
You mean you found *another* solution. Two different methods had already
been posted to this thread.
tought maybe it's not the best way, it does the trick pretty well :

http://www.thescripts.com/forum/thread225183.html
I think it's a pretty good bet that the other solution you found is not the
best way, and in fact is unlikely to be better than the methods already
given in this thread.

Pete
Sep 15 '06 #11
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:el**************@TK2MSFTNGP04.phx.gbl...
>
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>When running on XP or higher you can register for session change
notifications by calling (through PInvoke) the TS API
"WTSRegisterSessionNotification", note that you need a Windows procedure
to capture the messages.

cool!! wow!! thanks a lot... ;)
Or you could accomplish the same thing simply by handling the SessionSwitch
event, as I posted previously. No p/invoke required.

Pete
Sep 15 '06 #12

"John Brown" <no_spam@_nospam.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I've found a solution, tought maybe it's not the best way, it does the
trick pretty well :

I recommend you investigate this thoroughly. People often apply esoteric
ways of doing things but unless the techniques are officially sanctioned
by MSFT, you're asking for trouble (don't be surprised if it breaks one
day IOW).
Also consider something that works fine in your environment, but
not someone else's. Consider what happens when, say, running
under Terminal Services, Citrix, et.al.

I've been burned this way many, many times -- I've since learned
my lesson.

Later ...

Sarge
Sep 15 '06 #13

"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:el**************@TK2MSFTNGP04.phx.gbl...
| cool!! wow!! thanks a lot... ;)
|
|

Note that as Peter said, V2 of the framework nicely wraps the stuff I posted
through the SystemEvents class in Microsoft.Win32.
Note that just like you have to make sure to call
WTSUnRegisterSessionNotification in V1, you'll have to make sure to detach
the handler or you'll leak a handle.

Here's a small console app. sample...

using System;
using Microsoft.Win32;

public class App
{
static void Main()
{

SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
Console.ReadLine(); // block main thread
// !! detach the eventhandler when the application terminates, or you will
leak a handle.
SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
}
static void SystemEvents_SessionSwitch(object sender,
SessionSwitchEventArgs e)
{
if(e.Reason == SessionSwitchReason.SessionLock)
{...}
}
}

}
Willy.
Sep 16 '06 #14
thanks it works fine... ;) excellent!!! we have to spread the word...
because unfortunately, this information is pretty hard to get on the net...
it's said almost everywhere that it's not possible to know for sure.... Now
it is!! ;)

thanks

ThunderMusic

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>
"ThunderMusic" <No*************************@NoSpAm.comwrote in message
news:el**************@TK2MSFTNGP04.phx.gbl...
| cool!! wow!! thanks a lot... ;)
|
|

Note that as Peter said, V2 of the framework nicely wraps the stuff I
posted
through the SystemEvents class in Microsoft.Win32.
Note that just like you have to make sure to call
WTSUnRegisterSessionNotification in V1, you'll have to make sure to detach
the handler or you'll leak a handle.

Here's a small console app. sample...

using System;
using Microsoft.Win32;

public class App
{
static void Main()
{

SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
Console.ReadLine(); // block main thread
// !! detach the eventhandler when the application terminates, or you will
leak a handle.
SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
}
static void SystemEvents_SessionSwitch(object sender,
SessionSwitchEventArgs e)
{
if(e.Reason == SessionSwitchReason.SessionLock)
{...}
}
}

}
Willy.


Sep 19 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: alltel news | last post by:
Is there a handy way to detect whether a machine is locked- as in the screen saver has timed out and a password is required to get back to the desktop? Thanks Ken Webster
12
by: Alban Hertroys | last post by:
Good day, I have a number of threads doing inserts in a table, after which I want to do a select. This means that it will occur that the row that I want to select is locked (by the DB). In these...
1
by: google.matthew | last post by:
Hi My company's domain policy causes my workstation to lock when not in use, which appears very sensible. However, I have developed a MS access app which creates documents and e-mails them. ...
1
by: David | last post by:
Hi, There are anyway to detect when a workstation is locked like does the MSN Messenger that set the status to "Be Right Back" when the workstation is locked ? Some API ? Thanks =)
2
by: Kevin | last post by:
Hi, I need to trap when a workstation is locked. I saw this article http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/winlogon_notification_packages.asp, but i...
1
by: Eric Rupp via .NET 247 | last post by:
I am designing a program that will detect when a computer becomes locked (screen saver comes on/Ctrl+Alt+Del). I am looking to detect that and update a database for a custom Time Tracker program. ...
2
by: Welie | last post by:
I apologize if this is a faq. I searched for about 45 minutes and didn't find a good answer but there are many matching posts. I have a form which is based on a simple query to a linked table....
14
by: ThunderMusic | last post by:
Hi, I want to detect in my C# app (can use iterop) if the workstation is locked. I've seen on the web people saying it's impossible except by looking if the Screensaver process is running. There...
0
by: jz | last post by:
Does FindWindow work if the workstation the pgm is running on is locked? I have a crazy little application (vb .exe) that wakes up every 5 seconds and looks for a NT Authentication box that is...
0
by: =?Utf-8?B?am1hZ2FyYW0=?= | last post by:
My program needs to do X when someone 'starts using' their Windows user account, and it should do Y when they 'stop using' their Windows user account. By 'starts using' I mean they log on, unlock...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.