大家谁用过 IOCTL_STORAGE_GET_DEVICE_NUMBER呀?我这显示未定义?
下面的程序实现了从当前盘符映射到物理硬盘的磁盘
//得到盘符以后,使用DeviceIoControl函数,操作码是IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS
//会得到一个VOLUME_DISK_EXTENTS结构信息。这个结构里的DISK_EXTENT结构有一个变量DiskNumber,
//用来表明该盘符所在的disk,0表示物理盘0,1表示物理盘1依次类推
const int IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x2D1080;
int GetDeviceNumber(string DevicePath)
{
int ans = -1;
IntPtr h = CreateFile(DevicePath.TrimEnd('\\'), 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (h.ToInt32() != INVALID_HANDLE_VALUE)
{
int requiredSize;
STORAGE_DEVICE_NUMBER Sdn = new STORAGE_DEVICE_NUMBER();
int nBytes = Marshal.SizeOf(Sdn);
IntPtr ptrSdn = Marshal.AllocHGlobal(nBytes);
if(DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero,0,ptrSdn,nBytes,out requiredSize,IntPtr.Zero))
{
Sdn = (STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(ptrSdn, typeof(STORAGE_DEVICE_NUMBER));
// just my way of combining the relevant parts of the
// STORAGE_DEVICE_NUMBER into a single number
ans = (Sdn.DeviceType << 8) + Sdn.DeviceNumber;
}
Marshal.FreeHGlobal(ptrSdn);
CloseHandle(h);
}
return ans;
}
}
编译后报错,提示"IOCTL_STORAGE_GET_DEVICE_NUMBER"未定义
错误 CS0103: 当前上下文中不存在名称“IOCTL_STORAGE_GET_DEVICE_NUMBER”
加上定义后,又报错了
const int IOCTL_STORAGE_GET_DEVICE_NUMBER = 0x2D1080;
错误 CS1502: 与“ConsoleApplication3.GetDevicesNumber.DeviceIoControl(System.IntPtr, uint, System.IntPtr, uint, ref ConsoleApplication3.GetDevicesNumber.GetVersionOutParams, uint, ref uint, System.IntPtr)”最匹配的重载方法具有一些无效参数
错误 CS1503: 参数“5”: 无法从“System.IntPtr”转换为“ref ConsoleApplication3.GetDevicesNumber.GetVersionOutParams”
错误 CS1503: 参数“6”: 无法从“int”转换为“uint”
错误 CS1503: 参数“7”: 无法从“out int”转换为“ref uint”