Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:49:53+00:00 2026-05-30T19:49:53+00:00

I need to obtain disk geometry info, but something wrong and DeviceIoControl returning false.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:49:54+00:00Added an answer on May 30, 2026 at 7:49 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to obtain a Social Security Number in my app, but I don't
I need to obtain all the posts that have the same tag. But I
I need to obtain the date of yesterday with NSDate object. Any idea? EDIT
For build logging, I need to obtain the current stream/baseline with cleartool , but
I need to obtain a queryset of a foreignkey but I haven't been able
I need to obtain the server time but I only have javascript to play
I have RSS feed link, but I need to obtain date from it. Then
I need to obtain all the divs with a given id but the jquery
I need to obtain UDP datagram from Asynchronous Socket Server but an exception occurred
I'd need to obtain all the objects created by a class. Is there any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.