DeviceIoControl with IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS - C#

时间:2016-05-30 19:31:24

标签: c# winapi

我的C ++代码使用IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS调用DeviceIoControl,我需要将其转换为C#。
我发现很多DeviceIoControl p \ invoke样本,但没有这个特定的标志。 我尝试过使用以下extern:

[DllImport("kernel32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeviceIoControl(SafeFileHandle hVol, int controlCode, IntPtr inBuffer, int inBufferSize, ref DiskExtents outBuffer, int outBufferSize, ref int bytesReturned, IntPtr overlapped);

结构:

[StructLayout(LayoutKind.Sequential)]
public struct DiskExtent
{
    public uint DiskNumber;
    public long StartingOffset;
    public long ExtentLength;
}

[StructLayout(LayoutKind.Sequential)]
public struct DiskExtents
{
    public int numberOfExtents;
    public DiskExtent[] first;
}

以下调用代码:

int bytesReturned = 0;
DiskExtents diskExtents = new DiskExtents();
bool res = DeviceIoControl(hVol, 5636096, IntPtr.Zero, 0, ref diskExtents, Marshal.SizeOf(diskExtents), ref bytesReturned, IntPtr.Zero);

但是调用返回false并且数据结构始终为空。 任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

您的代码存在的问题是,除非已知大小,否则无法对数组进行编组。这意味着,在进行DiskExtents调用时,无法正确编组DeviceIoControl结构。

以下代码适用于我(我倾向于选择显式大小的类型用于互操作目的,并且更喜欢坚持使用Win32标头中的名称,因为我已经知道它们的含义并且可以轻松地将它们谷歌):

internal static class NativeMethods
{
   internal const UInt32 IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = 0x00560000;

   [StructLayout(LayoutKind.Sequential)]
   internal class DISK_EXTENT
   {
      public UInt32 DiskNumber;
      public Int64  StartingOffset;
      public Int64  ExtentLength;
   }

   [StructLayout(LayoutKind.Sequential)]
   internal class VOLUME_DISK_EXTENTS
   {
      public UInt32      NumberOfDiskExtents;
      public DISK_EXTENT Extents;
   }

   [DllImport("kernel32", SetLastError = true)]
   [return: MarshalAs(UnmanagedType.Bool)]                             
   internal static extern bool DeviceIoControl(SafeFileHandle hDevice,
                                               UInt32         ioControlCode,
                                               IntPtr         inBuffer,
                                               UInt32         inBufferSize,
                                               IntPtr         outBuffer,
                                               UInt32         outBufferSize,
                                               out UInt32     bytesReturned,
                                               IntPtr         overlapped);
}
class Program
{
    static void Main(string[] args)
    {
       // Open the volume handle using CreateFile()
       SafeFileHandle sfh = ...

       // Prepare to obtain disk extents.
       // NOTE: This code assumes you only have one disk!
       NativeMethods.VOLUME_DISK_EXTENTS vde           = new NativeMethods.VOLUME_DISK_EXTENTS();
       UInt32                            outBufferSize = (UInt32)Marshal.SizeOf(vde);
       IntPtr                            outBuffer     = Marshal.AllocHGlobal((int)outBufferSize);
       UInt32                            bytesReturned = 0;
       if (NativeMethods.DeviceIoControl(sfh,
                                         NativeMethods.IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
                                         IntPtr.Zero,
                                         0,
                                         outBuffer,
                                         outBufferSize,
                                         out bytesReturned,
                                         IntPtr.Zero))
       {
          // The call succeeded, so marshal the data back to a
          // form usable from managed code.
          Marshal.PtrToStructure(outBuffer, vde);

          // Do something with vde.Extents here...e.g.
          Console.WriteLine("DiskNumber: {0}\nStartingOffset: {1}\nExtentLength: {2}",
                            vde.Extents.DiskNumber,
                            vde.Extents.StartingOffset,
                            vde.Extents.ExtentLength);
       }
       Marshal.FreeHGlobal(outBuffer);
    }
}

当然,这假设您只需要获取有关单个磁盘的信息。如果您需要DeviceIoControl函数来填充VOLUME_DISK_EXTENTS结构中包含多个磁盘的信息,则必须加倍努力。

问题是,在运行时确切地知道有多少磁盘。 DeviceIoControl将返回ERROR_MORE_DATA,通知您表格中还有更多信息,您需要使用更大的缓冲区再次调用。你会按照与上面相同的方式做到这一点,还有许多额外的复杂功能。您需要使用类似Marshal.ReAllocHGlobal的内容来扩展缓冲区的大小以容纳其他DISK_EXTENT结构。在第一次VOLUME_DISK_EXTENTS.NumberOfDiskExtents未成功通话后,DeviceIoControl会员将返回所需的号码。 This C# code显示了类似的实现。

编写这样令人讨厌的代码所花费的时间是我在很大程度上放弃了用C#开发Windows应用程序的原因。它创造了不可避免的悖论,其中C ++将更清晰,更优雅,更容易编写,并且更不容易出错。 (你确定我没有在上面的代码中泄漏任何句柄吗?我不是。)