CD / DVD Tray Locker
How to prevent a CD/DVD drive from being opened.
Overview
A few days ago (at the time of writing), someone on the C# forum asked how to lock the CD drive. I thought this was an interesting problem that could be useful if writing burning software or something, and this is the solution.How To Do It
It requires PInvoke to use three kernel32.dll functions.- CreateFile to get a handle to the drive
- DeviceIoControl to perform the lock/unlock by passing a bool to its InBuffer parameter
- CloseHandle to release the handle.
The Code
public static class CDTrayLocker
{
public static bool Lock(string driveLetter)
{
return ManageLock(driveLetter, true);
}
public static bool Unlock(string driveLetter)
{
return ManageLock(driveLetter, false);
}
private static bool ManageLock(string driveLetter, bool lockDrive)
{
bool result = false;
string fileName = string.Format(@"\\.\{0}:", driveLetter);
SECURITY_ATTRIBUTES securityAttributes = new SECURITY_ATTRIBUTES();
IntPtr deviceHandle = CreateFile(
fileName,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ref securityAttributes,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
IntPtr.Zero);
if (deviceHandle != INVALID_HANDLE_VALUE)
{
IntPtr outBuffer;
int bytesReturned;
NativeOverlapped overlapped = new NativeOverlapped();
result = DeviceIoControl(
deviceHandle,
IOCTL_STORAGE_MEDIA_REMOVAL,
ref lockDrive,
1,
out outBuffer,
0,
out bytesReturned,
ref overlapped);
CloseHandle(deviceHandle);
}
return result;
}
// http://msdn.microsoft.com/en-us/library/aa379560(VS.85).aspx
[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_ATTRIBUTES
{
int nLength;
IntPtr lpSecurityDescriptor;
bool bInheritHandle;
}
private const int FILE_SHARE_READ = 0x00000001;
private const int FILE_SHARE_WRITE = 0x00000002;
private const uint GENERIC_READ = 0x80000000;
private const int OPEN_EXISTING = 3;
private const int FILE_ATTRIBUTE_NORMAL = 0x80;
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private const int IOCTL_STORAGE_MEDIA_REMOVAL = 0x2D4804;
// http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
ref SECURITY_ATTRIBUTES lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile);
// http://msdn.microsoft.com/en-us/library/aa363216(VS.85).aspx
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool DeviceIoControl(
IntPtr hDevice,
int dwIoControlCode,
ref bool lpInBuffer,
int nInBufferSize,
out IntPtr lpOutBuffer,
int nOutBufferSize,
out int lpBytesReturned,
ref NativeOverlapped lpOverlapped);
// http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(
IntPtr hObject);
}
To use this, simply call Lock
or Unlock
passing the letter of the required drive.
// Where "D" is your drive letter!
CDTrayLocker.Lock("D");
// Do your thing
CDTrayLocker.Unlock("D");
Points to Note
If you lock a driven
times, you will need to unlock it n
times too.
It works under Vista without elevation!
Edit: