1

我需要获取磁盘几何信息,但出现问题并且 DeviceIoControl 返回错误。任何想法如何解决它?或其他使用 C# 和 kernel32 的例子表示赞赏。

 [DllImport("kernel32.dll")]
            public static extern IntPtr CreateFile(
            string lpFileName, int dwDesiredAccess, int dwShareMode,
            IntPtr lpSecurityAttributes, int dwCreationDisposition,
            int dwFlagsAndAttributes, IntPtr hTemplateFile);

            private const int FILE_SHARE_READ = 1;
            private const int OPEN_ALWAYS = 4;
            private const int INVALID_HANDLE_VALUE = -1;

            [DllImport("kernel32.dll", ExactSpelling = true)]
            internal static extern bool DeviceIoControl(
            IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize,
            IntPtr lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr lpOverlapped);

            private const int IOCTL_DISK_GET_MEDIA_TYPES = 0x00070c00;

            static void Main(string[] args)
            {
                IntPtr hflp = CreateFile(@""\\.\C:", 0, FILE_SHARE_READ, IntPtr.Zero, OPEN_ALWAYS, 0, IntPtr.Zero);
                if ((int)hflp == INVALID_HANDLE_VALUE)
                { Console.WriteLine("CreateFile failed"); return; }

                Type ts = typeof(DISK_GEOMETRY);
                int ss = Marshal.SizeOf(ts);
                int ssa = ss * 20;
                IntPtr mptr = Marshal.AllocHGlobal(ssa);
                int byret = 0;
                bool ok = DeviceIoControl(hflp, IOCTL_DISK_GET_MEDIA_TYPES, IntPtr.Zero, 0,
                mptr, ssa, ref byret, IntPtr.Zero);
                if (!ok)
                { Console.WriteLine("DeviceIoControl failed"); return; }
                int count = byret / ss;
                int run = (int)mptr;
                DISK_GEOMETRY gem;
                for (int i = 0; i < count; i++)
                {
                    gem = (DISK_GEOMETRY)Marshal.PtrToStructure((IntPtr)run, ts);
                    Console.WriteLine("MediaType={0} SectorsPerTrack={1}", gem.MediaType, gem.SectorsPerTrack);
                    run += ss;
                }
                Marshal.FreeHGlobal(mptr);
            }

PS我已经阅读了有关此的msdn帮助。

4

1 回答 1

2

IOCTL_DISK_GET_MEDIA_TYPES似乎是遗留的,不再受支持。至少在我的操作系统(Win7 x64)上是这样。尝试调用DeviceIoControl导致IOCTL_DISK_GET_MEDIA_TYPES错误代码 1 ERROR_INVALID_FUNCTION,.

我相信您将需要使用它IOCTL_STORAGE_GET_MEDIA_TYPES_EX

在这种情况下,我的建议是首先尝试从 C++ 调用 API 函数。这样您就不必为 p/invoke 而苦恼,并且您知道所有的结构和函数原型都是正确的。一旦你弄清楚如何调用特定的 API 函数,然后转换为 p/invoke。

顺便说一句,您应该对 p/invokes 更加小心。小心使用uint来匹配DWORD,并确保你使用SetLastError=true,以便你可以使用Marshal.GetLastWin32Error().

像这样的东西:

[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr CreateFile(
    string lpFileName, uint dwDesiredAccess, uint dwShareMode,
    IntPtr lpSecurityAttributes, uint dwCreationDisposition,
    uint dwFlagsAndAttributes, IntPtr hTemplateFile);

[DllImport("kernel32.dll", SetLastError=true)]
internal static extern bool DeviceIoControl(
    IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer,
    uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, 
    ref uint lpBytesReturned, IntPtr lpOverlapped);
于 2012-03-03T20:32:32.220 回答