How to eject CD ROM in C#.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maheshkumar.R

    How to eject CD ROM in C#.

    Hi all,

    I want to access, detect, eject(open and close) CD Rom using C#. How i can do this..?

    --

    Mähésh Kumär. R
    cyberiafreak
  • Matthias Pfeffer

    #2
    Re: How to eject CD ROM in C#.

    Hi!

    One way is to use wmi.

    public class cEjectMedia

    {

    private const int INVALID_HANDLE_ VALUE = -1;

    private const int GENERIC_READ = unchecked((int) 0x80000000);

    private const int GENERIC_WRITE = unchecked((int) 0x40000000);

    private const int FILE_SHARE_READ = unchecked((int) 0x00000001);

    private const int FILE_SHARE_WRIT E = unchecked((int) 0x00000002);

    private const int OPEN_EXISTING = unchecked((int) 3);

    private const int FSCTL_LOCK_VOLU ME = unchecked((int) 0x00090018);

    private const int FSCTL_DISMOUNT_ VOLUME = unchecked((int) 0x00090020);

    private const int IOCTL_STORAGE_E JECT_MEDIA = unchecked((int) 0x002D4808);

    private const int IOCTL_STORAGE_M EDIA_REMOVAL = unchecked((int) 0x002D4804);

    [DllImport("kern el32.dll", EntryPoint="Cre ateFileW", CharSet=CharSet .Unicode, SetLastError=tr ue)]

    private static extern IntPtr CreateFile(

    string lpFileName,

    int dwDesiredAccess ,

    int dwShareMode,

    IntPtr lpSecurityAttri butes,

    int dwCreationDispo sition,

    int dwFlagsAndAttri butes,

    IntPtr hTemplateFile);

    [DllImport("kern el32.dll", ExactSpelling=t rue, SetLastError=tr ue)]

    private static extern bool CloseHandle(Int Ptr handle);

    [DllImport("kern el32.dll", ExactSpelling=t rue, SetLastError=tr ue)]

    private static extern bool DeviceIoControl (

    IntPtr hDevice,

    int dwIoControlCode ,

    byte[] lpInBuffer,

    int nInBufferSize,

    byte[] lpOutBuffer,

    int nOutBufferSize,

    out int lpBytesReturned ,

    IntPtr lpOverlapped );

    public cEjectMedia()

    {

    //

    // TODO: Fügen Sie hier die Konstruktorlogi k hinzu

    //

    }

    public bool EjectMedia(stri ng sPhysicalDrive)

    {

    bool ok = false;

    bool KarteKannEntnom menWerden = false;

    // Schritt 1: Volume öffnen // Laufwerk anpassen! //

    IntPtr h = CreateFile( @"\\.\" + sPhysicalDrive, GENERIC_READ,

    FILE_SHARE_READ | FILE_SHARE_WRIT E,

    IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero );

    if( h.ToInt32() == -1 )

    return false;

    while( true )

    {

    // Schritt 2: Volume sperren

    for( int i = 0; i < 10; i++ )

    {

    int nout = 0;

    ok = DeviceIoControl ( h, FSCTL_LOCK_VOLU ME,

    null, 0, null, 0, out nout, IntPtr.Zero );

    if( ok )

    break;

    Thread.Sleep( 500 );

    }

    if( !ok )

    break;

    // Schritt 3: Volume dismounten

    int xout = 0;

    ok = DeviceIoControl ( h, FSCTL_DISMOUNT_ VOLUME,

    null, 0, null, 0, out xout, IntPtr.Zero );

    if( !ok )

    break;

    // ab hier kann die Karte ohne Datenverlust

    // entnommen werden

    KarteKannEntnom menWerden = true;

    // Schritt 4: Prevent Removal Of Volume

    byte[] flg = new byte[1];

    flg[0] = 0; // 0 = false

    int yout = 0;

    ok = DeviceIoControl ( h,

    IOCTL_STORAGE_M EDIA_REMOVAL, flg, 1,

    null, 0, out yout, IntPtr.Zero );

    if( !ok )

    break;

    // Schritt 5: Eject Media

    ok = DeviceIoControl ( h,

    IOCTL_STORAGE_E JECT_MEDIA,

    null, 0, null, 0, out xout, IntPtr.Zero );

    break;

    }

    // Schritt 6: Close Handle

    ok = CloseHandle( h );

    return KarteKannEntnom menWerden;

    }

    }

    Then you can eject the cd-rom drive:

    cEjectMedia myEjectMedia = new cEjectMedia();

    myEjectMedia.Ej ectMedia(sDiskD rive);

    You can also use it to remove usb drives.



    Greetz

    Michael

    Comment

    • Maheshkumar.R

      #3
      Re: How to eject CD ROM in C#.

      Thnkz for this unmanaged approach to solve this problem..but i'm searching something in managed code approach. I mean again we are importing the unmanaged DLL into our App, but i'm looking something in managed approach...??

      I'm wondering how to get this simple thing in .NET..?

      Mahe
      "Matthias Pfeffer" <knubbel@gmx.ne t> wrote in message news:eqsvB8PjFH A.1412@TK2MSFTN GP09.phx.gbl...
      Hi!

      One way is to use wmi.

      public class cEjectMedia

      {

      private const int INVALID_HANDLE_ VALUE = -1;

      private const int GENERIC_READ = unchecked((int) 0x80000000);

      private const int GENERIC_WRITE = unchecked((int) 0x40000000);

      private const int FILE_SHARE_READ = unchecked((int) 0x00000001);

      private const int FILE_SHARE_WRIT E = unchecked((int) 0x00000002);

      private const int OPEN_EXISTING = unchecked((int) 3);

      private const int FSCTL_LOCK_VOLU ME = unchecked((int) 0x00090018);

      private const int FSCTL_DISMOUNT_ VOLUME = unchecked((int) 0x00090020);

      private const int IOCTL_STORAGE_E JECT_MEDIA = unchecked((int) 0x002D4808);

      private const int IOCTL_STORAGE_M EDIA_REMOVAL = unchecked((int) 0x002D4804);

      [DllImport("kern el32.dll", EntryPoint="Cre ateFileW", CharSet=CharSet .Unicode, SetLastError=tr ue)]

      private static extern IntPtr CreateFile(

      string lpFileName,

      int dwDesiredAccess ,

      int dwShareMode,

      IntPtr lpSecurityAttri butes,

      int dwCreationDispo sition,

      int dwFlagsAndAttri butes,

      IntPtr hTemplateFile);

      [DllImport("kern el32.dll", ExactSpelling=t rue, SetLastError=tr ue)]

      private static extern bool CloseHandle(Int Ptr handle);

      [DllImport("kern el32.dll", ExactSpelling=t rue, SetLastError=tr ue)]

      private static extern bool DeviceIoControl (

      IntPtr hDevice,

      int dwIoControlCode ,

      byte[] lpInBuffer,

      int nInBufferSize,

      byte[] lpOutBuffer,

      int nOutBufferSize,

      out int lpBytesReturned ,

      IntPtr lpOverlapped );

      public cEjectMedia()

      {

      //

      // TODO: Fügen Sie hier die Konstruktorlogi k hinzu

      //

      }

      public bool EjectMedia(stri ng sPhysicalDrive)

      {

      bool ok = false;

      bool KarteKannEntnom menWerden = false;

      // Schritt 1: Volume öffnen // Laufwerk anpassen! //

      IntPtr h = CreateFile( @"\\.\" + sPhysicalDrive, GENERIC_READ,

      FILE_SHARE_READ | FILE_SHARE_WRIT E,

      IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero );

      if( h.ToInt32() == -1 )

      return false;

      while( true )

      {

      // Schritt 2: Volume sperren

      for( int i = 0; i < 10; i++ )

      {

      int nout = 0;

      ok = DeviceIoControl ( h, FSCTL_LOCK_VOLU ME,

      null, 0, null, 0, out nout, IntPtr.Zero );

      if( ok )

      break;

      Thread.Sleep( 500 );

      }

      if( !ok )

      break;

      // Schritt 3: Volume dismounten

      int xout = 0;

      ok = DeviceIoControl ( h, FSCTL_DISMOUNT_ VOLUME,

      null, 0, null, 0, out xout, IntPtr.Zero );

      if( !ok )

      break;

      // ab hier kann die Karte ohne Datenverlust

      // entnommen werden

      KarteKannEntnom menWerden = true;

      // Schritt 4: Prevent Removal Of Volume

      byte[] flg = new byte[1];

      flg[0] = 0; // 0 = false

      int yout = 0;

      ok = DeviceIoControl ( h,

      IOCTL_STORAGE_M EDIA_REMOVAL, flg, 1,

      null, 0, out yout, IntPtr.Zero );

      if( !ok )

      break;

      // Schritt 5: Eject Media

      ok = DeviceIoControl ( h,

      IOCTL_STORAGE_E JECT_MEDIA,

      null, 0, null, 0, out xout, IntPtr.Zero );

      break;

      }

      // Schritt 6: Close Handle

      ok = CloseHandle( h );

      return KarteKannEntnom menWerden;

      }

      }

      Then you can eject the cd-rom drive:

      cEjectMedia myEjectMedia = new cEjectMedia();

      myEjectMedia.Ej ectMedia(sDiskD rive);

      You can also use it to remove usb drives.



      Greetz

      Michael

      Comment

      • Publicjoe

        #4
        Re: How to eject CD ROM in C#.

        This is another approach using a dll, full simple app provided

        Hope this helps

        Publicjoe

        C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
        C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html - 72
        Chapters
        VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html - 28
        Chapters
        C++ Ebook at http://www.publicjoe.f9.co.uk/cppt/samples/ebook.html - 8
        Chapters
        Java Ebook at http://www.publicjoe.f9.co.uk/java/samples/ebook.html - 2
        Chapters
        Mirrors at http://dowhileloop.com/publicjoe/ and


        ///--- Beginning of code ---///

        using System;
        using System.Drawing;
        using System.Text;
        using System.Windows. Forms;
        using System.Runtime. InteropServices ;

        public class CDFrm : Form
        {
        private System.Componen tModel.Containe r components = null;

        private Button btn1;
        private Button btn2;

        // Constructor
        public CDFrm()
        {
        InitializeCompo nent();
        }

        // Clean up any resources being used.
        protected override void Dispose( bool disposing )
        {
        if( disposing )
        {
        if (components != null)
        {
        components.Disp ose();
        }
        }
        base.Dispose( disposing );
        }

        // Initialize form components
        private void InitializeCompo nent()
        {
        this.components = new System.Componen tModel.Containe r();

        this.Text = "CD Demo";
        this.Size = new Size( 200, 100 );

        // Button initialization and setup
        btn1 = new Button();
        btn1.Text = "Open";
        btn1.Location = new Point( 20, 15 );
        btn1.Size = new Size( 70, 20 );

        btn2 = new Button();
        btn2.Text = "Close";
        btn2.Location = new Point( 110, 15 );
        btn2.Size = new Size( 70, 20 );

        // Add the Buttons to the Form
        this.Controls.A dd( btn1 );
        this.Controls.A dd( btn2 );

        // Add Event for Button Click
        btn1.Click += new EventHandler(bt nEject_Click);
        btn2.Click += new EventHandler(bt nClose_Click);
        }

        // The main entry point for the application.
        [STAThread]
        static void Main()
        {
        Application.Run ( new CDFrm() );
        }

        // Event for Open Button Click
        private void btnEject_Click( object sender, System.EventArg s e )
        {
        int ret = mciSendString( "set cdaudio door open", null, 0,
        IntPtr.Zero );
        }

        // Event for Close Button Click
        private void btnClose_Click( object sender, System.EventArg s e )
        {
        int ret = mciSendString( "set cdaudio door closed", null, 0,
        IntPtr.Zero );
        }

        // DLL access
        [DllImport( "winmm.dll" , EntryPoint="mci SendStringA",
        CharSet=CharSet .Ansi )]
        protected static extern int mciSendString( string lpstrCommand,
        StringBuilder
        lpstrReturnStri ng,
        int uReturnLength,
        IntPtr hwndCallback );
        }

        ///--- End of code ---///


        Comment

        • Maheshkumar.R

          #5
          Re: How to eject CD ROM in C#.

          Hmm, Great link and thnkz for your resource Mike..really i'm finding lot of
          resources over there...

          Thnkz,
          Mahesh
          /cyberiafreak/

          "Publicjoe" <mike@publicjoe .co.uk> wrote in message
          news:uxfSsuSjFH A.2444@TK2MSFTN GP10.phx.gbl...[color=blue]
          > This is another approach using a dll, full simple app provided
          >
          > Hope this helps
          >
          > Publicjoe
          >
          > C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
          > C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html - 72
          > Chapters
          > VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html - 28
          > Chapters
          > C++ Ebook at http://www.publicjoe.f9.co.uk/cppt/samples/ebook.html - 8
          > Chapters
          > Java Ebook at http://www.publicjoe.f9.co.uk/java/samples/ebook.html - 2
          > Chapters
          > Mirrors at http://dowhileloop.com/publicjoe/ and
          > http://publicjoe.justbe.com/
          >
          > ///--- Beginning of code ---///
          >
          > using System;
          > using System.Drawing;
          > using System.Text;
          > using System.Windows. Forms;
          > using System.Runtime. InteropServices ;
          >
          > public class CDFrm : Form
          > {
          > private System.Componen tModel.Containe r components = null;
          >
          > private Button btn1;
          > private Button btn2;
          >
          > // Constructor
          > public CDFrm()
          > {
          > InitializeCompo nent();
          > }
          >
          > // Clean up any resources being used.
          > protected override void Dispose( bool disposing )
          > {
          > if( disposing )
          > {
          > if (components != null)
          > {
          > components.Disp ose();
          > }
          > }
          > base.Dispose( disposing );
          > }
          >
          > // Initialize form components
          > private void InitializeCompo nent()
          > {
          > this.components = new System.Componen tModel.Containe r();
          >
          > this.Text = "CD Demo";
          > this.Size = new Size( 200, 100 );
          >
          > // Button initialization and setup
          > btn1 = new Button();
          > btn1.Text = "Open";
          > btn1.Location = new Point( 20, 15 );
          > btn1.Size = new Size( 70, 20 );
          >
          > btn2 = new Button();
          > btn2.Text = "Close";
          > btn2.Location = new Point( 110, 15 );
          > btn2.Size = new Size( 70, 20 );
          >
          > // Add the Buttons to the Form
          > this.Controls.A dd( btn1 );
          > this.Controls.A dd( btn2 );
          >
          > // Add Event for Button Click
          > btn1.Click += new EventHandler(bt nEject_Click);
          > btn2.Click += new EventHandler(bt nClose_Click);
          > }
          >
          > // The main entry point for the application.
          > [STAThread]
          > static void Main()
          > {
          > Application.Run ( new CDFrm() );
          > }
          >
          > // Event for Open Button Click
          > private void btnEject_Click( object sender, System.EventArg s e )
          > {
          > int ret = mciSendString( "set cdaudio door open", null, 0,
          > IntPtr.Zero );
          > }
          >
          > // Event for Close Button Click
          > private void btnClose_Click( object sender, System.EventArg s e )
          > {
          > int ret = mciSendString( "set cdaudio door closed", null, 0,
          > IntPtr.Zero );
          > }
          >
          > // DLL access
          > [DllImport( "winmm.dll" , EntryPoint="mci SendStringA",
          > CharSet=CharSet .Ansi )]
          > protected static extern int mciSendString( string lpstrCommand,
          > StringBuilder
          > lpstrReturnStri ng,
          > int uReturnLength,
          > IntPtr hwndCallback );
          > }
          >
          > ///--- End of code ---///
          >
          >[/color]


          Comment

          Working...