.NET Compact Framework Lock Down Device

By Robbe D. Morris

Printer Friendly Version

Robbe Morris
Robbe & Melisa Morris
  Download Source Code
Here's a quick tip on how to disable the taskbar on your mobile devices (this code was tested on several different iPAQs of varying models).  This, in combination with setting your application to run on start up, set your start up form .Minimize = false, .Maximize = false, .ControlBox = false, and .WindowState = Maximized will pretty much lock down the device while users are running the application.  Thus, the user cannot toy with the settings on the device or run unapproved applications.
Be sure that you call the LockDown.Execute method in your Form_Load event before anything else runs.  Also be sure to disable the lock down when the application closes.  Failure to do so will result in the device remaining in a locked state until a soft reset occurs.
This sample code does not prevent someone from taking the device, connecting it to a PC via ActiveSync, and manipulating files.  It simply sufficiently locks down use of the device while a user runs your application.
Feel free to download the sample application above or peruse the sample code below.
 


 
C# Lockdown Class
 
using System;
using System.Runtime.InteropServices;

namespace App
{
 
   public class LockDown
   {

     public LockDown() { }
	 
     [DllImport("coredll.dll", EntryPoint="FindWindow")]
     private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
	
     [DllImport("coredll.dll", EntryPoint="EnableWindow")]
     private static extern bool EnableWindow(IntPtr hwnd,bool bEnable);

     public static bool Execute(bool enabled)
     {

        IntPtr hwnd = FindWindow("HHTaskBar",null);
 
        if (!hwnd.Equals(IntPtr.Zero))
        {

          if (enabled)
          {
             return EnableWindow(hwnd,false);
          }
          else
          {
             return EnableWindow(hwnd,true);
          }
        }
		    
        return true;

     }
 
  }
}

C# Form1

  static void Main() 
  {
    Application.Run(new Form1());
  }

  private void cmdExit_Click(object sender, System.EventArgs e)
  {
    App.LockDown.Execute(false); 
    this.Close();
    Application.Exit(); 
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
    App.LockDown.Execute(true); 
    this.label1.Text = "Taskbar locked";
  }

  private void cmdUnlock_Click(object sender, System.EventArgs e)
  {
     App.LockDown.Execute(false); 
     this.label1.Text = "Taskbar unlocked";
  }

  private void cmdLock_Click(object sender, System.EventArgs e)
  {
    App.LockDown.Execute(true); 
    this.label1.Text = "Taskbar locked";
  }
  


Robbe has been a Microsoft MVP in C# since 2004.  He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.