Microsoft.NET (1)


Remote Lock Computer

If you work at home using remote control tools you might have had troubles accessing your computer if the connection has been dropped. Some remote control tools like RDP or Unicenter Remote Control will only allow you to access the computer if it’s locked. Something which usually makes sense but can be annoying if you lose the connection or if the remote control tool crashes.

There are some VB Scripts out there which you can use to lock the computer from another computer. They work but aren’t really nice as the have to copy a file to the computer you’re trying to lock. You also don’t have a nice interface and sometimes you even have to create some files manually. Not really userfriendly, something which might annoy you – especially if you just lost your internet connection.

I did some quick work and came up with a rather small C# tool which uses WMI to execute a remote command. There’s a command you can execute to lock your local computer, just hit Windows+R (or click on Start and select Run) and enter this command:

rundll32 user32.dll,LockWorkStation

Once it has been executed, your computer will be locked and ready for remote access. I only created a small interface which combines WMI and this rundll32 command to make the process of locking a computer in your network a bit easier. Please note, you’ll need plenty of permissions. I’m not 100% sure but I think the impersonate option I’m using will only work if you’re a domain admin. Enough talking, here’s the code:

using System;
using System.Windows.Forms;
using System.Management;
 
namespace RemoteLock
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private void btnLock_Click(object sender, EventArgs e)
        {
            ConnectionOptions connOptions = new ConnectionOptions();
            connOptions.Impersonation = ImpersonationLevel.Impersonate;
 
            if (txtUsername.Text != String.Empty && txtPassword.Text != String.Empty)
            {
                connOptions.Username = txtUsername.Text;
                connOptions.Password = txtPassword.Text;
            }
            connOptions.EnablePrivileges = true;
 
            ManagementScope managementScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", txtComputer.Text), connOptions);
            managementScope.Connect();
 
            ObjectGetOptions objectGetOptions = new ObjectGetOptions();
            ManagementPath managementPath = new ManagementPath("Win32_Process");
            ManagementClass processClass = new ManagementClass(managementScope, managementPath, objectGetOptions);
 
            ManagementBaseObject inParameters = processClass.GetMethodParameters("Create");
 
            inParameters["CommandLine"] = @"rundll32 user32.dll,LockWorkStation";
            ManagementBaseObject outParameters = processClass.InvokeMethod("Create", inParameters, null);
 
            MessageBox.Show("WMI process created, return value: " + outParameters["returnValue"]);
        }
    }
}

The project should compile just fine if you open the sln file using the free Microsoft Visual C# Express 2010 edition. There’s a binary file in the zip file for those who don’t have the time or interest to compile the project on their own. The code is free, improve it, copy it, trash it.. I’d only appreciate if I’d get some credits for it!

Download RemoteLock