Question

I need to obtain disk geometry info, but something wrong and DeviceIoControl returning false. Any ideas how to fix it? Or Other examples using C# and kernel32 appreciated.

 [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);
            }

P.S I've already read msdn help on this.

Was it helpful?

Solution

IOCTL_DISK_GET_MEDIA_TYPES appears to be legacy and no longer supported. At least that's the case on my OS (Win7 x64). Attempting to call DeviceIoControl with IOCTL_DISK_GET_MEDIA_TYPES results in error code 1, ERROR_INVALID_FUNCTION.

I believe that you will need to use IOCTL_STORAGE_GET_MEDIA_TYPES_EX instead.

My advice in this situation is to attempt to call the API functions from C++ first. That way you don't have to struggle with p/invoke and you know that all the structures and function prototypes are correct. Once you have worked out how to call the particular API function then translate into p/invoke.

As an aside, you should be a little more careful about your p/invokes. Take care to use uint to match DWORD, and make sure you use SetLastError=true so that you can query the error code with Marshal.GetLastWin32Error().

Something like this:

[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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top