Prevent CD Autoplays in VB.NET


This article describes the approach used programmatically to prevent a CD from going into auto play mode; this may be useful to you if you are, for example, writing an application that logs directly to CD and you are trying to burn data periodically to a CD but you do not want the CD to go into auto play mode after burning a some data to it.

Getting Started:

In order to get started, unzip the included project and open the solution in the Visual Studio 2005 environment.  You will find a single form within the application and the application itself serves no purpose other than to demonstrate the approach used to prevent a CD from entering into auto play.

The Code:

The code is simple enough, in the declarations at the beginning of the class you will note that a User32.dll function is imported; this function import looks like this:

Public Declare Auto Function RegisterWindowMessage Lib "User32.Dll" _

    (ByVal lpString As String) As Integer

This import allows us to register a windows message which will subsequently be processed by an overridden version of the standard WndProc function.  Following the DLL function import, we declare a variable used to contain a message ID as an integer value:

Private MessageID As Integer

We will use this variable to capture the return value, an integer, derived from calling the RegisterWindowMessage function.
Following these two declaration, in the form load, I have added a call to display that the form has loaded, this is just a status message and it serves no purpose other than to show the order of events as the form is processed:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles MyBase.Load


    TextBox1.Text = TextBox1.Text & "Form Loaded..." & _



End Sub

Following the form load handler, we need to write to Sub New.  Within Sub New we are going to register the message used to prevent auto play from occurring.  This is accomplished with the following code:

Public Sub New()


    ' This call is required by the Windows Form Designer.


    TextBox1.Text = TextBox1.Text & "Initialized..." & _



    ' Add any initialization after the InitializeComponent() call.


    'Call to stop CD autoplay

    MessageID = _



End Sub

After the InitializeComponent call, I have added another status message indicating that initialization has occurred, this code serves no other purpose other than to provide this status.  The next portion of the code accomplished the task of cancelling auto play.  Here the MessageID  variable is set to the returned integer value derived from calling the RegisterWindowMessage call, RegisterWindowMessage is passed the command to cancel auto play.  From this point on, whenever a CD attempts to auto play, the message will fire and auto play will be cancelled.

The last bit of code required is used to override the WndProc subroutine; this subroutine processes all windows messages and for that reason is must be configured to both look for and process the command to cancel auto play and to allow all other windows messages to process normally.  That code looks like this:

Protected Overrides Sub WndProc(ByRef msg As



    ' look for the queryCancelAutoPlay request and process it

    If msg.Msg = MessageID Then

      TextBox1.Text = TextBox1.Text & "QueryCancelAutoPlay Processed" & _


      msg.Result = New IntPtr(1)


        ' allow MyBase to process all normal messages


    End If

End Sub

The first part of the if-then statement will look for and process the command to cancel auto play, the else portion of the statement will allow other windows messages to process normally.

Testing the Application.

To test the code, get a CD or DVD that will auto play normally.  Without running the this application, insert the disk into the drive and verify that it does auto play.  After verifying that the disk auto plays, open the drive to stop the disk and then run the application.  When the application starts, you should see a form that looks like this:


Figure 1:  Test Application Loaded

At this point, there is no CD or DVD in the drive and we can see that the form was initialized and loaded.  During initialization the message to cancel auto play was processed.  Now, insert the CD or DVD and watch the form, the CD will load and try to auto play, when that occurs, the message to prevent auto play will be processed and the auto play will cancel; at that point, you should see the form update to look like this:


Figure 2:  First Auto Play Cancellation Processed

Subsequent attempts to auto play will trigger the message repeatedly, after several attempts to insert the auto playing CD or DVD, the form will look like this:


Figure 3:  Multiple Auto Play Cancellations Processed

This demonstrates that each time auto play is attempted, the registered message will be processed and auto play will be cancelled.


This simple application demonstrated that you interact with the operating system through the User32.dll and process your own message traffic.  If you are interested in other things that you can do with the WndProc command, take a look at the WindowMessage.WndProc in the .NET Framework Class library documentation.  If you are interested in burning CDs, take at a look at this project: