Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am trying to cancel the windows lock event but so far unable to do so.
I am able to detect the windows lock and unlock event using hooks but not able to cancel the event.

This is the code i am using:
C#
[DllImport("WtsApi32.dll")]
       private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags);
       [DllImport("WtsApi32.dll")]
       private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);

       private const int NOTIFY_FOR_THIS_SESSION = 0;
       private const int WM_WTSSESSION_CHANGE = 0x2b1;
       private const int WTS_SESSION_LOCK = 0x7;
       private const int WTS_SESSION_UNLOCK = 0x8;

       //public event EventHandler OnMachineLocked;
       //public event EventHandler OnMachineUnlocked;

       protected override void OnHandleDestroyed(EventArgs e)
       {
           // unregister the handle before it gets destroyed
           WTSUnRegisterSessionNotification(this.Handle);
           base.OnHandleDestroyed(e);
       }

       protected override void WndProc(ref Message m)
       {
           if (m.Msg == WM_WTSSESSION_CHANGE)
           {
               int value = m.WParam.ToInt32();
               if (value == WTS_SESSION_LOCK)
               {
                   OnMachineLocked(EventArgs.Empty);
               }
               else if (value == WTS_SESSION_UNLOCK)
               {
                   OnMachineUnlocked(EventArgs.Empty);
               }
           }
           base.WndProc(ref m);
       }

       protected virtual void OnMachineLocked(EventArgs e)
       {

       }

       protected virtual void OnMachineUnlocked(EventArgs e)
       {

       }
       #endregion



Kindly guide me how to go about this and is there any alternate way to implement this?.
Thanks in advance.
Posted
Comments
Addy Tas 30-Nov-11 15:53pm    
According to the documentation you should call base.WinProc(ref m) only if you do not handle the message. would it not work if you'd just ommit calling the base in the two cases you'd like not to happen?
shivanikat 5-Dec-11 6:18am    
Hi Addy Tas,
Thanks for the suggestion.
I have tried ommiting the code base.WinProc(ref m) from my code, but it has started giving win32 exception "Error Creating Windows handle" in some other class.
The Scenario for my application is that this code is present in a windows form 'MainForm' which in turn is called by a AppController Class; On starting the application the AppController class calls the MainForm by using this code
CodeApplication.Run(App.UI.MainForm);
When i omit Codebase.WinProc(ref m) from WndProc method in MainForm and start my application, i am getting the exception on this line:
CodeApplication.Run(App.UI.MainForm)
Kindly suggest any solution to the problem. I am not able to resolve the issue as i am not able to understand the dependency with this code.
In Case you need furthur information, please let me know.
Addy Tas 5-Dec-11 6:28am    
Hi,

Did you make sure that you only omitted calling the base when receiving a WTS_SESSION_LOCK or WTS_SESSION_UNLOCK?

e.g.
protected override void WndProc(ref Message m) {

bool foundLock = false; if (m.Msg == WM_WTSSESSION_CHANGE)

{ int value = m.WParam.ToInt32();

if (value == WTS_SESSION_LOCK) {

foundLock = true;

OnMachineLocked(EventArgs.Empty); }

else if (value == WTS_SESSION_UNLOCK) {

foundLock = true;

OnMachineUnlocked(EventArgs.Empty); }

} if(!foundLock) base.WndProc(ref m); }
shivanikat 5-Dec-11 7:48am    
Hi,

Yes i made sure that i am only ommiting calling the base, this is the code i am using:

protected override void WndProc(ref Message m)
{
try
{
if (m.Msg == WM_WTSSESSION_CHANGE)
{
int value = m.WParam.ToInt32();
switch (value)
{
case WTS_SESSION_LOCK:
OnMachineLocked(EventArgs.Empty);
break;
case WTS_SESSION_UNLOCK:
OnMachineUnlocked(EventArgs.Empty);
break;
default:
base.WndProc(ref m);
break;
}
}
}
}
Addy Tas 5-Dec-11 11:15am    
You do not call the base when you don't receive a WM_WTSESSION_CHANGE. You will need to call it for anything that you don't handle.

Hi,

Use the following; this seems to be working for me:

private const int WM_WTSSESSION_CHANGE	= 0x02B1;
private const int WTS_SESSION_LOCK	= 0x07;
private const int WTS_SESSION_UNLOCK	= 0x08;

protected override void WndProc(ref Message m)
{
	if ( (m.Msg == WM_WTSSESSION_CHANGE) &&
		((m.WParam.ToInt32() == WTS_SESSION_LOCK) ||
		 (m.WParam.ToInt32() == WTS_SESSION_UNLOCK)) )
	{
		return;				
	}
	base.WndProc(ref m);
}


Regards, AT
 
Share this answer
 
Can you Provide Full solution in attachement.
I have problem in compiling.
I also wanted to ask which type of solution you have made?[Winform, windows service(which i think it is), console application, e.t.c]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900