Hi,
I am working one project on which I want to check if machine is locked then it should throw a message to user that "Machine was locked on [Time].";
Check bode below,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class CheckMachineStatus : Form
{
const int DESKTOP_SWITCHDESKTOP = 256;
[DllImport("user32", EntryPoint = "OpenDesktopA",
CharSet = CharSet.Ansi,
SetLastError = true,
ExactSpelling = true)]
private static extern Int32 OpenDesktop(string lpszDesktop,
Int32 dwFlags,
bool fInherit,
Int32 dwDesiredAccess);
[DllImport("user32", CharSet = CharSet.Ansi,
SetLastError = true,
ExactSpelling = true)]
private static extern Int32 CloseDesktop(Int32 hDesktop);
[DllImport("user32", CharSet = CharSet.Ansi,
SetLastError = true,
ExactSpelling = true)]
private static extern Int32 SwitchDesktop(Int32 hDesktop);
public CheckMachineStatus()
{
InitializeComponent();
}
private void CheckMachineStatus_Load(object sender, EventArgs e)
{
if (IsSystemLocked())
{
MessageBox.Show("System was locked on : " + DateTime.Now);
Application.Exit();
}
else
{
MessageBox.Show("System was not locked.");
Application.Exit();
}
}
public bool IsSystemLocked()
{
int hwnd = -1;
int rtn = -1;
try
{
hwnd = OpenDesktop("Default", 0, false, DESKTOP_SWITCHDESKTOP);
if (hwnd != 0)
{
rtn = SwitchDesktop(hwnd);
if (rtn == 0)
{
CloseDesktop(hwnd);
return true;
}
else
{
CloseDesktop(hwnd);
}
}
else
{
}
}
catch
{
return false;
}
return false;
}
}
}
This code worked well in below scenario,
I scheduled this exe on specific time and locked my machine, when I unlock my machine I found that message box was there.
but, I open that machine with Remote Desktop Connection with same user name and password from another machine and schedule this same exe for specific time and closed that remote desktop connection.
When I unlock my main machine I found that scheduled worked well but it displayed message that "System was not locked." i.e. it falls in else part.
Can anyone help me out that what should I do if want to get machine lock status in this scenario?
Thanks in advance...