Click here to Skip to main content
15,883,938 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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,

C#
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)
                    {
                        // Locked
                        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...
Posted
Updated 3-Nov-11 20:02pm
v2

1 solution

When you connect using Remote desktop or MSTSC by default you're not connecting to the same console session as the main console when using the device directly.

Start remote desktop using this command:

mstsc /console

This will connect you to same session as your main console session and should display what you're looking for.
 
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