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()
InitializeComponent()
TextBox1.Text = TextBox1.Text & "Initialized..." & _
Environment.NewLine
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)
If msg.Msg = MessageID Then
TextBox1.Text = TextBox1.Text & _
"QueryCancelAutoPlay Processed" & _
Environment.NewLine
msg.Result = New IntPtr(1)
Else
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.