How to eject CD ROM in C#.

  • Thread starter Thread starter Maheshkumar.R
  • Start date Start date
M

Maheshkumar.R

Hi all,

I want to access, detect, eject(open and close) CD Rom using C#. How i can do this..?
 
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_WRITE = unchecked((int)0x00000002);

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

private const int FSCTL_LOCK_VOLUME = unchecked((int)0x00090018);

private const int FSCTL_DISMOUNT_VOLUME = unchecked((int)0x00090020);

private const int IOCTL_STORAGE_EJECT_MEDIA = unchecked((int)0x002D4808);

private const int IOCTL_STORAGE_MEDIA_REMOVAL = unchecked((int)0x002D4804);

[DllImport("kernel32.dll", EntryPoint="CreateFileW", CharSet=CharSet.Unicode, SetLastError=true)]

private static extern IntPtr CreateFile(

string lpFileName,

int dwDesiredAccess,

int dwShareMode,

IntPtr lpSecurityAttributes,

int dwCreationDisposition,

int dwFlagsAndAttributes,

IntPtr hTemplateFile);

[DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]

private static extern bool CloseHandle(IntPtr handle);

[DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]

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 Konstruktorlogik hinzu

//

}

public bool EjectMedia(string sPhysicalDrive)

{

bool ok = false;

bool KarteKannEntnommenWerden = false;

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

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

FILE_SHARE_READ | FILE_SHARE_WRITE,

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_VOLUME,

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

KarteKannEntnommenWerden = 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_MEDIA_REMOVAL, flg, 1,

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

if( !ok )

break;

// Schritt 5: Eject Media

ok = DeviceIoControl( h,

IOCTL_STORAGE_EJECT_MEDIA,

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

break;

}

// Schritt 6: Close Handle

ok = CloseHandle( h );

return KarteKannEntnommenWerden;

}

}

Then you can eject the cd-rom drive:

cEjectMedia myEjectMedia = new cEjectMedia();

myEjectMedia.EjectMedia(sDiskDrive);

You can also use it to remove usb drives.



Greetz

Michael
 
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
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_WRITE = unchecked((int)0x00000002);

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

private const int FSCTL_LOCK_VOLUME = unchecked((int)0x00090018);

private const int FSCTL_DISMOUNT_VOLUME = unchecked((int)0x00090020);

private const int IOCTL_STORAGE_EJECT_MEDIA = unchecked((int)0x002D4808);

private const int IOCTL_STORAGE_MEDIA_REMOVAL = unchecked((int)0x002D4804);

[DllImport("kernel32.dll", EntryPoint="CreateFileW", CharSet=CharSet.Unicode, SetLastError=true)]

private static extern IntPtr CreateFile(

string lpFileName,

int dwDesiredAccess,

int dwShareMode,

IntPtr lpSecurityAttributes,

int dwCreationDisposition,

int dwFlagsAndAttributes,

IntPtr hTemplateFile);

[DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]

private static extern bool CloseHandle(IntPtr handle);

[DllImport("kernel32.dll", ExactSpelling=true, SetLastError=true)]

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 Konstruktorlogik hinzu

//

}

public bool EjectMedia(string sPhysicalDrive)

{

bool ok = false;

bool KarteKannEntnommenWerden = false;

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

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

FILE_SHARE_READ | FILE_SHARE_WRITE,

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_VOLUME,

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

KarteKannEntnommenWerden = 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_MEDIA_REMOVAL, flg, 1,

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

if( !ok )

break;

// Schritt 5: Eject Media

ok = DeviceIoControl( h,

IOCTL_STORAGE_EJECT_MEDIA,

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

break;

}

// Schritt 6: Close Handle

ok = CloseHandle( h );

return KarteKannEntnommenWerden;

}

}

Then you can eject the cd-rom drive:

cEjectMedia myEjectMedia = new cEjectMedia();

myEjectMedia.EjectMedia(sDiskDrive);

You can also use it to remove usb drives.



Greetz

Michael
 
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.ComponentModel.Container components = null;

private Button btn1;
private Button btn2;

// Constructor
public CDFrm()
{
InitializeComponent();
}

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

// Initialize form components
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();

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.Add( btn1 );
this.Controls.Add( btn2 );

// Add Event for Button Click
btn1.Click += new EventHandler(btnEject_Click);
btn2.Click += new EventHandler(btnClose_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.EventArgs e )
{
int ret = mciSendString( "set cdaudio door open", null, 0,
IntPtr.Zero );
}

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

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

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

Thnkz,
Mahesh
/cyberiafreak/
 
Back
Top