Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / Visual Basic

Preventing a CD from Auto-Playing

Rate me:
Please Sign up or sign in to vote.
4.40/5 (3 votes)
28 Aug 2006CPOL4 min read 49.5K   543   14   6
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.

Image 1

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:

VB
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:

VB
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:

VB
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:

VB
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:

VB
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:

Image 2

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:

Image 3

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:

Image 4

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionrun the app with elevated privilege Pin
Member 798412129-Dec-16 0:14
Member 798412129-Dec-16 0:14 
GeneralImportant point missing Pin
mav.northwind10-Nov-07 21:55
mav.northwind10-Nov-07 21:55 
General:) Pin
Coder_200727-Jun-07 12:03
Coder_200727-Jun-07 12:03 
QuestionNext Updation Pin
Praveen Bhosage24-Jun-07 20:48
Praveen Bhosage24-Jun-07 20:48 
QuestionCD+G format Pin
wasif_Muhammad2-May-07 21:09
wasif_Muhammad2-May-07 21:09 
AnswerRe: CD+G format Pin
salysle3-May-07 4:09
salysle3-May-07 4:09 
You can burn CDs in general using the Windows Media Player ActiveX control; however, for the karoki format (CD+G), I think you have obtain a plug-in that will handle it. I have noted that several are availabe for sale but I am not aware as to whether or not such an item can be obtained for free or directly through Microsoft.

This link may help get you started: http://msdn2.microsoft.com/en-us/library/bb248315.aspx

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.