Click here to Skip to main content
14,176,439 members
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 30 Aug 2009
Licenced CPOL

CD / DVD Tray Locker

, 13 Jan 2011
Rate this:
Please Sign up or sign in to vote.
How to prevent a CD/DVD drive from being opened.


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.

We also need to define the SECURITY_ATTRIBUTES structure. Although we don't actually use any of the parameters, we need to pass a default instance of this.

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(
            ref securityAttributes,

        if (deviceHandle != INVALID_HANDLE_VALUE)
            IntPtr outBuffer;
            int bytesReturned;
            NativeOverlapped overlapped = new NativeOverlapped();
            result = DeviceIoControl(
                ref lockDrive,
                out outBuffer,
                out bytesReturned,
                ref overlapped);
        return result;

    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;

    [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);

    [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);

    [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!
// Do your thing

Points to Note

If you lock a drive n times, you will need to unlock it n times too.

It works under Vista without elevation!

Edit: I haven't had time to look into the reasons why, but I got a Stack error using .NET 4.0. I recompiled using 2.0 and it worked perfectly. If I find the reason I will update here.
Updated with changes to accommodate changes with .NET 4.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

CEO Dave Meadowcroft
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 Reason for my vote 5 it's very usef... Pin
RaviRanjanKr16-Jan-11 2:46
professionalRaviRanjanKr16-Jan-11 2:46 
GeneralThanks. I will need this! Pin
Dr.Walt Fair, PE13-Jan-11 14:18
professionalDr.Walt Fair, PE13-Jan-11 14:18 
GeneralReason for my vote of 5 Good Article. Pin
Dalek Dave13-Jan-11 10:36
professionalDalek Dave13-Jan-11 10:36 
GeneralReason for my vote of 5 Hi Dave. Well done. Except for the s... Pin
Luc Pattyn13-Jan-11 6:43
professionalLuc Pattyn13-Jan-11 6:43 
GeneralReason for my vote of 5 very nice Pin
Аslam Iqbal13-Jan-11 3:06
professionalАslam Iqbal13-Jan-11 3:06 
GeneralNice code...i had been looking for this! Great code. Thanks. Pin
GPUToaster™23-May-10 20:01
memberGPUToaster™23-May-10 20:01 
QuestionOnly works when no media in drive? Pin
rdavisunr20-Apr-11 14:02
memberrdavisunr20-Apr-11 14:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190525.1 | Last Updated 14 Jan 2011
Article Copyright 2009 by DaveyM69
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid