c# - 驱动器号与 DevicePath

标签 c# c++ pinvoke drive-letter cd-rom

我从上周开始搜索这个,几乎所有关于 DriveLetter x DevicePath/Volume/Device ID/whatever 的链接对我来说都是紫色的。

我正在开发一个应用程序,它使用 SetupApi 函数和这些 guids GUID_DEVINTERFACE_USB_DEVICE "A5DCBF10-6530-11D2-901F-00C04FB951ED", GUID_DEVINTERFACE_VOLUME "53F5630D-B6BF-11D0-94B9-0F9-0F2-0 ".

所以...我已经能够使用驱动器盘符映射设备。如何? 使用 SetupApi 结果,使用 DevicePath 通过 DeviceIOControl 获取 DiskNumber 和 PartitionNumber。而且没关系!!!其实不是全部。 实际上到CDROM驱动器我无法得到它。

这是我的代码。

int bytesReturned = 0;
IOCTL.DiskExtents de1 = new IOCTL.DiskExtents();
int numDiskExtents = 0;
IntPtr ptrDe1 = Marshal.AllocHGlobal(Marshal.SizeOf(de1));
bool result = IOCTL.DeviceIoControl(FileHandle, IOCTL.IOControlCodes.IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, IntPtr.Zero, 0, ptrDe1, Marshal.SizeOf(de1), bytesReturned, null);
if ((!result)) {
    Marshal.FreeHGlobal(ptrDe1);
    //libera a memória alocada.
    throw new System.ComponentModel.Win32Exception(); //is thrown here
} else {
    de1 = Marshal.PtrToStructure(ptrDe1, typeof(IOCTL.DiskExtents));
    Marshal.FreeHGlobal(ptrDe1);
    return de1.first.DiskNumber;
}

对于我拥有的所有 CDROM 驱动器,都会抛出一个 IncorrectFunction。

为了获取文件句柄,我同时使用驱动器号(如“\.\D:”)和设备路径。并且都失败了。

我不知道我是不是做错了什么,因为这个适用于 USB 大容量存储和硬盘。

谢谢。

最佳答案

现在我使用 DevicePath 中的 VolumeName 而不是 DiskNumber 和 PartitionNumber。 我们从“\.\C:”和 DevicePath 中获得一个 VolumeName。

关于c# - 驱动器号与 DevicePath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8986274/

相关文章:

c++ - 错误 : invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'

c++ - 公共(public)接口(interface)方法的私有(private)实现

c# - 向 IntPtr 添加偏移量

c# - 使用 gzip 密码保护 xml 文件

c# - 以 ReSharper 方式使用嵌套条件循环有什么好处?

c# - 使用 ATL/COM 将托管字节 [] 转换为非托管字节数组

c# - 如何让一个方法接受两种类型的数据作为参数?

c++ - 全局C++对象初始化

.net - 隐藏进程的所有窗口(例如 Internet Explorer)

c# - 使用 PostMessage/SendMessage 将 key 发送到 c# IE WebBrowser