Reader Level:
ARTICLE

Prevent CD Autoplays

Posted by Scott Lysle Articles | Windows Forms C# December 19, 2006
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.
  • 0
  • 0
  • 7798
Download Files:
 

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

[System.Runtime.InteropServices.DllImport("User32.Dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

public static extern int RegisterWindowMessage(string lpString);

 

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 int MessageID;

 

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 void Form1_Load(object sender, System.EventArgs e)

{

      TextBox1.Text = TextBox1.Text + "Form Loaded..." + Environment.NewLine;

}

 

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

 

public Form1()

{

      // Required for Windows Form Designer support

      InitializeComponent();

      TextBox1.Text = TextBox1.Text + "Initialized..." + Environment.NewLine;

      MessageID = RegisterWindowMessage(Convert.ToString("QueryCancelAutoPlay"));

      // TODO: Add any constructor code after InitializeComponent call

}

 

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 override void WndProc(ref System.Windows.Forms.Message msg)

{

      if (msg.Msg == MessageID)

      {

            TextBox1.Text = TextBox1.Text + "QueryCancelAutoPlay Processed" + Environment.NewLine;

            msg.Result = new IntPtr[1];

      }

      else

      {

            base.WndProc(msg);

      }

}

 

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.

Summary:

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:

http://www.gotdotnet.com/workspaces/workspace.aspx?id=2d7c26d1-8e05-4fd8-a868-cf794409a37e

NOTE: THIS ARTICLE IS CONVERTED FROM VB.NET TO C# USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON VB.NET Heaven (http://www.vbdotnetheaven.com/).

COMMENT USING

Trending up