Click here to Skip to main content
Click here to Skip to main content
Go to top

Preventing a CD from Auto-Playing

, 28 Aug 2006
Rate this:
Please Sign up or sign in to vote.
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.

Introduction

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 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 declarations, 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..." & _
    Environment.NewLine

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.
    InitializeComponent()
    TextBox1.Text = TextBox1.Text & "Initialized..." & _
                    Environment.NewLine

    ' Add any initialization after the InitializeComponent() call.

    'Call to stop CD autoplay
    MessageID = _
    RegisterWindowMessage(Convert.ToString("QueryCancelAutoPlay"))


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 accomplishes the task of cancelling auto-play. Here, the MessageID variable is set to the returned integer value derived from calling RegisterWindowMessage. 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, 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 System.Windows.Forms.Message)

    ' look for the queryCancelAutoPlay request and process it
    If msg.Msg = MessageID Then
      TextBox1.Text = TextBox1.Text & _
                      "QueryCancelAutoPlay Processed" & _
                      Environment.NewLine
      msg.Result = New IntPtr(1)
    Else
        ' allow MyBase to process all normal messages
        MyBase.WndProc(msg)
    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 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 is 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.

Summary

This simple application demonstrates 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralImportant point missing Pinmembermav.northwind10-Nov-07 21:55 
General:) PinmemberCodeWatch27-Jun-07 12:03 
QuestionNext Updation PinmemberPraveen Bhosage24-Jun-07 20:48 
QuestionCD+G format Pinmemberwasif_Muhammad2-May-07 21:09 
AnswerRe: CD+G format Pinmembersalysle3-May-07 4:09 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 28 Aug 2006
Article Copyright 2006 by salysle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid