Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Ejecting a CD opened by explorer

Ejecting a CD opened by explorer

Scheduled Pinned Locked Moved C#
comtoolstutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LordZoster
    wrote on last edited by
    #1

    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
    
    G 1 Reply Last reply
    0
    • L LordZoster

      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
      
      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        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

        L Offline
        L Offline
        LordZoster
        wrote on last edited by
        #3

        Hi Giorgi, thanks, it worked. I wasn't very incline to use winmm.dll, but it works and that's all! Thanks again

        G 1 Reply Last reply
        0
        • L LordZoster

          Hi Giorgi, thanks, it worked. I wasn't very incline to use winmm.dll, but it works and that's all! Thanks again

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          You are welcome :)

          Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups