我尝试使用DeviceIoControl函数(Win32 API函数)弹出CDROM驱动器,当我的CDROM驱动器没有磁盘时,它可以完美工作,但是在将磁盘插入其中后,Marshal.GetLastWin32Error()返回32(ERROR_SHARING_VIOLATION:该过程无法访问该文件,因为它正在被另一个进程使用),在DeviceIoControl中传递的driveHandle由CreateFile()函数创建。

你能帮我吗?我喜欢这种操作CD ROM相关内容的方法,我可以使用winmm.dll弹出我的CDROM,但我认为这种方法值得尝试。

OK,这是代码:

using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Runtime.InteropServices;

namespace DVD_ejector
{

    public partial class Form1 : Form
    {
        const int OPENEXISTING = 3;
        const int IOCTL_STORAGE_EJECT_MEDIA = 2967560;
        const uint GENERICREAD = 0x80000000;
        const int INVALID_HANDLE = -1;
        public Form1()
        {
            InitializeComponent();
            DriveInfo[] drs = DriveInfo.GetDrives();
            List<DriveInfo> cdRoms = new List<DriveInfo>();
            foreach (DriveInfo dInfo in drs)
            {
                if (dInfo.DriveType == DriveType.CDRom)
                {
                    cdRoms.Add(dInfo);
                }
            }
            comboBox1.DataSource = cdRoms;
            comboBox1.DisplayMember = "Name";

            if (comboBox1.Items.Count > 0) comboBox1.SelectedIndex = 0;
            button1.Click += (sender, e) =>
            {
                Eject(@"\\.\" + ((DriveInfo)comboBox1.SelectedItem).Name[0]+":");
            };
        }
        [DllImport("kernel32", SetLastError=true)]
        static extern IntPtr CreateFile(string fileName, uint desiredAccess, uint shareMode, IntPtr attributes,uint creationDisposition, uint flagsAndAttribute, IntPtr fileTemplate);
        [DllImport("kernel32")]
        static extern int CloseHandle(IntPtr fileHandle);
        [DllImport("kernel32")]
        static extern bool DeviceIoControl(IntPtr driveHandle, int ctrlCode, IntPtr inBuffer, int inBufferSize, IntPtr outBuffer, int outBufferSize, ref int bytesReturned, IntPtr overlapped);
        int bytesReturned;
        private void Eject(string cdDrive)
        {
            IntPtr driveHandle = CreateFile(cdDrive, GENERICREAD, 0, IntPtr.Zero, OPENEXISTING, 0, IntPtr.Zero);
            try
            {
                if((int)driveHandle != INVALID_HANDLE)
                   DeviceIoControl(driveHandle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                CloseHandle(driveHandle);
            }
        }
    }
}

最佳答案

如错误所示,该设备正被其他设备使用,但它在CreateFile调用而不是DeviceIoControl上发生故障,并且您的代码未正确检查故障。

出现共享冲突的原因是,您试图专门打开该设备,如果任何东西试图打开它或其中的文件,包括防病毒,资源管理器,搜索索引器等,该设备都会失败。

此更新的Eject函数修复了共享模式和错误处理,现在在正确的位置报告错误。

private void Eject(string cdDrive) {
    IntPtr driveHandle = new IntPtr(INVALID_HANDLE);
    try {
        // Open the device
        driveHandle = CreateFile(cdDrive, GENERICREAD, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, IntPtr.Zero, OPENEXISTING, 0, IntPtr.Zero);
        if ((int)driveHandle == INVALID_HANDLE) { throw new Win32Exception(); }

        // Try and eject
        bool ejected = DeviceIoControl(driveHandle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesReturned, IntPtr.Zero);
        if (!ejected) { throw new Win32Exception(); }

    } catch (Exception ex) {
        MessageBox.Show(ex.Message);

    } finally {
        if ((int)driveHandle != INVALID_HANDLE) { CloseHandle(driveHandle); }
    }
}

关于c# - DeviceIoControl无法弹出非空CDROM驱动器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12472770/

10-10 08:38