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

  • SEARCH
  • Home
  • 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 8870525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:45:17+00:00 2026-06-14T17:45:17+00:00

I’m having some problems with DeviceIOControl . I’m trying to read the disk geometry

  • 0

I’m having some problems with DeviceIOControl. I’m trying to read the disk geometry from a physical drive but the output buffer is never set.

Here is a sample of my code, a simple function that is supposed to get the disk geometry and return whether the disk is removable:

public static bool IsDeviceRemovable(int DriveNo) {
    string Filename=@"\\.\Physicaldrive"+DriveNo;

    SafeFileHandle drive=CreateFile(
        Filename,
        FileAccess_e.None,
        FileShare_e.Write|FileShare_e.Read,
        IntPtr.Zero,
        CreationDisposition_e.OpenExisting,
        FileAttributes_e.None,
        IntPtr.Zero
    );

    if(drive.IsInvalid) {
        throw new IOException("Unable to access drive. Win32 Error Code: "+Marshal.GetLastWin32Error());
    }

    uint bytesReturned=0;
    DISK_GEOMETRY dg=new DISK_GEOMETRY();

    if(!DeviceIoControl(
        drive,
        IOControlCode_e.DiskGetDriveGeometry,
        IntPtr.Zero,
        0,
        dg,
        (uint)Marshal.SizeOf(dg),
        ref bytesReturned,
        IntPtr.Zero
        )) {
        throw new Exception("Unable to get properties from device. Win32 Error Code: "+Marshal.GetLastWin32Error());
    }

    drive.Close();
    return dg.MediaType==MEDIA_TYPE.RemovableMedia;
}

And some data structures:

[DllImport("Kernel32.dll", SetLastError=false, CharSet=CharSet.Auto)]
private static extern bool DeviceIoControl(
    SafeFileHandle hDevice,
    IOControlCode_e IoControlCode,
    [MarshalAs(UnmanagedType.AsAny)]
    [In] object InBuffer,
    uint nInBufferSize,
    [MarshalAs(UnmanagedType.AsAny)]
    [Out] object OutBuffer,
    uint nOutBufferSize,
    ref uint pBytesReturned,
    IntPtr Overlapped
    );

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
private static extern SafeFileHandle CreateFile(
    string lpFileName,
    FileAccess_e dwDesiredAccess,
    FileShare_e dwShareMode,
    IntPtr lpSecurityAttributes,
    CreationDisposition_e dwCreationDisposition,
    FileAttributes_e dwFlagsAndAttributes,
    IntPtr hTemplateFile
    );

private enum MEDIA_TYPE {
    Unknown=0x00,
    F5_1Pt2_512=0x01,
    F3_1Pt44_512=0x02,
    F3_2Pt88_512=0x03,
    F3_20Pt8_512=0x04,
    F3_720_512=0x05,
    F5_360_512=0x06,
    F5_320_512=0x07,
    F5_320_1024=0x08,
    F5_180_512=0x09,
    F5_160_512=0x0a,
    RemovableMedia=0x0b,
    FixedMedia=0x0c,
    F3_120M_512=0x0d,
    F3_640_512=0x0e,
    F5_640_512=0x0f,
    F5_720_512=0x10,
    F3_1Pt2_512=0x11,
    F3_1Pt23_1024=0x12,
    F5_1Pt23_1024=0x13,
    F3_128Mb_512=0x14,
    F3_230Mb_512=0x15,
    F8_256_128=0x16,
    F3_200Mb_512=0x17,
    F3_240M_512=0x18,
    F3_32M_512=0x19
}

[StructLayout(LayoutKind.Sequential)]
private struct DISK_GEOMETRY {
    public Int64 Cylinders;
    public MEDIA_TYPE MediaType;
    public Int32 TracksPerCylinder;
    public Int32 SectorsPerTrack;
    public Int32 BytesPerSector;
}

The enums used I got from

  • pinvoke.net: createfile (kernel32)
  • pinvoke.net: deviceiocontrol (kernel32)

The bytesReturned variable gets set (to 24 in this case) but the dg variable is all zeroes.

I have tried several other functions of DeviceIoControl with the same result: bytesReturned is set but the output buffer is all zeroes.

My testing environment

  • OS: Windows 7 Pro 64
  • IDE: Visual Studio 2010
  • Target Framework: .Net 4.0
  • 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-06-14T17:45:19+00:00Added an answer on June 14, 2026 at 5:45 pm

    The DISK_GEOMETRY declaration is the problem. It is being marshaled as an object but it was declared as a struct. That means it will be boxed, only the boxed copy will be updated by the pinvoke call. You are reading the original value and thus get all zeros.

    The simple fix is to declare it as a class instead of a struct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from

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.