Ejecting a CD opened by explorer
-
hallo i wrote a small system tray utility to eject the CD, it uses Kernel32 via interop to access CreateFile, DeviceIOControl and CloseHandle methods (cannot use WMI). It works fine unless the CD content is being viewed in a explorer window, or "My computer" window is open (i.e.: all drive units listed). It seems that explorer locks the drive unit during these two views: any idea on how to overcome this? this is an extract of the code:
// Constants used in DLL methods
const uint GENERICREAD = 0x80000000;
const uint OPENEXISTING = 3;
const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560;
const int INVALID_HANDLE = -1;
const uint DRIVE_CDROM = 5;// File Handle private static IntPtr fileHandle; private static uint returnedBytes; // Use Kernel32 via interop to access required methods // Get a File Handle \[DllImport("kernel32", SetLastError = true)\] static extern IntPtr CreateFile(string fileName, uint desiredAccess, uint shareMode, IntPtr attributes, uint creationDisposition, uint flagsAndAttributes, IntPtr templateFile); \[DllImport("kernel32", SetLastError = true)\] static extern int CloseHandle(IntPtr driveHandle); \[DllImport("kernel32", SetLastError = true)\] static extern bool DeviceIoControl(IntPtr driveHandle, uint IoControlCode, IntPtr lpInBuffer, uint inBufferSize, IntPtr lpOutBuffer, uint outBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped); \[DllImport("kernel32", SetLastError = true)\] static extern int GetDriveType(string driveLetter);
...
public static void Eject(string driveLetter)
{
try
{
// Create an handle to the drive
fileHandle = CreateFile(driveLetter,
GENERICREAD,
0,
IntPtr.Zero,
OPENEXISTING,
0,
IntPtr.Zero);if ((int)fileHandle != INVALID\_HANDLE) { // Eject the disk DeviceIoControl(fileHandle, IOCTL\_STORAGE\_EJECT\_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref returnedBytes, IntPtr.Zero); } } catch
-
hallo i wrote a small system tray utility to eject the CD, it uses Kernel32 via interop to access CreateFile, DeviceIOControl and CloseHandle methods (cannot use WMI). It works fine unless the CD content is being viewed in a explorer window, or "My computer" window is open (i.e.: all drive units listed). It seems that explorer locks the drive unit during these two views: any idea on how to overcome this? this is an extract of the code:
// Constants used in DLL methods
const uint GENERICREAD = 0x80000000;
const uint OPENEXISTING = 3;
const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560;
const int INVALID_HANDLE = -1;
const uint DRIVE_CDROM = 5;// File Handle private static IntPtr fileHandle; private static uint returnedBytes; // Use Kernel32 via interop to access required methods // Get a File Handle \[DllImport("kernel32", SetLastError = true)\] static extern IntPtr CreateFile(string fileName, uint desiredAccess, uint shareMode, IntPtr attributes, uint creationDisposition, uint flagsAndAttributes, IntPtr templateFile); \[DllImport("kernel32", SetLastError = true)\] static extern int CloseHandle(IntPtr driveHandle); \[DllImport("kernel32", SetLastError = true)\] static extern bool DeviceIoControl(IntPtr driveHandle, uint IoControlCode, IntPtr lpInBuffer, uint inBufferSize, IntPtr lpOutBuffer, uint outBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped); \[DllImport("kernel32", SetLastError = true)\] static extern int GetDriveType(string driveLetter);
...
public static void Eject(string driveLetter)
{
try
{
// Create an handle to the drive
fileHandle = CreateFile(driveLetter,
GENERICREAD,
0,
IntPtr.Zero,
OPENEXISTING,
0,
IntPtr.Zero);if ((int)fileHandle != INVALID\_HANDLE) { // Eject the disk DeviceIoControl(fileHandle, IOCTL\_STORAGE\_EJECT\_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref returnedBytes, IntPtr.Zero); } } catch
Try this method: Open and Close CD drive in C#[^]
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
Try this method: Open and Close CD drive in C#[^]
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
Hi Giorgi, thanks, it worked. I wasn't very incline to use winmm.dll, but it works and that's all! Thanks again
-
Hi Giorgi, thanks, it worked. I wasn't very incline to use winmm.dll, but it works and that's all! Thanks again
You are welcome :)
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion