How to open CD doors

  • Thread starter Thread starter midnighthell
  • Start date Start date
M

midnighthell

I'm looking for one sample (code) , how to make that doors of cdrom
(recorder, never mind) to be open on button press.

thanks...
 
Elder Hyde said:
Ask google mate, ask google.

I asked him and he gave me this:

http://www.codeguru.com/system/cdr.html

Except that's not in C#.

Try the following code fragment:

public bool Eject()
{
if (((int) _handle != -1) && ((int) _handle != 0))
{
uint Dummy = 0;
if (DeviceIoControl(_handle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero,
0, IntPtr.Zero, 0, ref Dummy, IntPtr.Zero) != 0)
return true;
}
return false;
}

Get the handle with:

IntPtr _handle = CreateFile(@"\\.\" + _driveLetter + ':', GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0,
IntPtr.Zero);

and remember to close it when you're finished with a call to CloseHandle().

Here are the imports:

[System.Runtime.InteropServices.DllImport("Kernel32.dll",
SetLastError=true)]
private extern static int DeviceIoControl(IntPtr hDevice, uint
IoControlCode,
IntPtr lpInBuffer, uint InBufferSize,
IntPtr lpOutBuffer, uint nOutBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);

[System.Runtime.InteropServices.DllImport("Kernel32.dll",
SetLastError=true)]
private extern static IntPtr CreateFile(string FileName, uint DesiredAccess,
uint ShareMode, IntPtr lpSecurityAttributes,
uint CreationDisposition, uint dwFlagsAndAttributes,
IntPtr hTemplateFile);

[System.Runtime.InteropServices.DllImport("Kernel32.dll",
SetLastError=true)]
private extern static int CloseHandle(IntPtr hObject);

private const uint IOCTL_STORAGE_EJECT_MEDIA = 0x002D4808;

I hope this helps...

Stuart Mackellar.
 
Maybe some overkill (but way better than PInvoke stuff) , just get a copy of
the Windows Media Player DSK, it comes with a PIA ready to be used from any
..NET project.
Below a small sample to get you started.....

using Microsoft.MediaPlayer.Interop;
...

WindowsMediaPlayer Player = new WindowsMediaPlayer();
Player.cdromCollection.Item(0).eject();;

Willy.
 
Back
Top