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 8928413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:25:52+00:00 2026-06-15T08:25:52+00:00

I have an unmanaged C++ Windows console app that works fine. I want it

  • 0

I have an unmanaged C++ Windows console app that works fine. I want it in C#. I have done DllImport statements for the necessary Kernel32.dll symbols:

[StructLayout(LayoutKind.Sequential)]
internal struct DiskGeometry
{
    public long Cylinders;
    public int MediaType;
    public int TracksPerCylinder;
    public int SectorsPerTrack;
    public int BytesPerSector;
}

internal static class NativeMethods
{
    internal const uint FileAccessGenericRead = 0x80000000;
    internal const uint FileShareWrite = 0x2;
    internal const uint FileShareRead = 0x1;
    internal const uint CreationDispositionOpenExisting = 0x3;
    internal const uint IoCtlDiskGetDriveGeometry = 0x70000;

    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern SafeFileHandle CreateFile(
        string fileName,
        uint fileAccess,
        uint fileShare,
        IntPtr securityAttributes,
        uint creationDisposition,
        uint flags,
        IntPtr template);

    [DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)]
    public static extern int DeviceIoControl(
        SafeFileHandle device,
        uint controlCode,
        IntPtr inBuffer,
        uint inBufferSize,
        IntPtr outBuffer,
        uint outBufferSize,
        ref uint bytesReturned,
        IntPtr overlapped);
}

I then have the following application code:

    public static void Main()
    {
        SafeFileHandle diskHandle = NativeMethods.CreateFile(
            "\\\\.\\PhysicalDrive0",
            NativeMethods.FileAccessGenericRead,
            NativeMethods.FileShareWrite | NativeMethods.FileShareRead,
            IntPtr.Zero,
            NativeMethods.CreationDispositionOpenExisting,
            0,
            IntPtr.Zero);
        if (diskHandle.IsInvalid)
        {
            Console.WriteLine("CreateFile failed with error: {0}", Marshal.GetLastWin32Error());
            return;
        }

        int geometrySize = Marshal.SizeOf(typeof(DiskGeometry));
        Console.WriteLine("geometry size = {0}", geometrySize);

        IntPtr geometryBlob = Marshal.AllocHGlobal(geometrySize);

        uint numBytesRead = 0;
        if (0 == NativeMethods.DeviceIoControl(
            diskHandle,
            NativeMethods.IoCtlDiskGetDriveGeometry,
            IntPtr.Zero,
            0,
            geometryBlob,
            (uint)geometrySize,
            ref numBytesRead,
            IntPtr.Zero))
        {
            Console.WriteLine("DeviceIoControl failed with error: {0}", Marshal.GetLastWin32Error());
            return;
        }

        Console.WriteLine("Bytes read = {0}", numBytesRead);

        DiskGeometry geometry = (DiskGeometry)Marshal.PtrToStructure(geometryBlob, typeof(DiskGeometry));
        Marshal.FreeHGlobal(geometryBlob);

        long bytesPerCylinder = (long)geometry.TracksPerCylinder * (long)geometry.SectorsPerTrack * (long)geometry.BytesPerSector;
        long totalSize = geometry.Cylinders * bytesPerCylinder;

        Console.WriteLine("Media Type:           {0}", geometry.MediaType);
        Console.WriteLine("Cylinders:            {0}", geometry.Cylinders);
        Console.WriteLine("Tracks per Cylinder:  {0}", geometry.TracksPerCylinder);
        Console.WriteLine("Sectors per Track:    {0}", geometry.SectorsPerTrack);
        Console.WriteLine("Bytes per Sector:     {0}", geometry.BytesPerSector);
        Console.WriteLine("Bytes per Cylinder:   {0}", bytesPerCylinder);
        Console.WriteLine("Total disk space:     {0}", totalSize);
    }

My C# app prints “Bytes read = 0” and the geometry member values are garbage. I am certainly no expert on DllImport and marshaling. Please help me understand what I am doing wrong. If I change the fifth parameter to DeviceIoControl to be “ref DiskGeometry” and just pass in one created right before the call (instead of IntPtr and alloc), all printed geometry member values are 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-15T08:25:53+00:00Added an answer on June 15, 2026 at 8:25 am

    You have mistype, try this:

    internal const uint IoCtlDiskGetDriveGeometry = 0x70000;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my problem: I have an unmanaged dll that I made.I'm calling one of
I have a simple application that loads an unmanaged dll and passes a few
I have a Windows Forms app that itself launches different threads to do different
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
I have an assembly that I've built on Windows I want to run the
We have a CF.NET 3.5 app that seems to work fine for about 4
I'm writing a CESetup.dll for a Windows Mobile app. It must be unmanaged, which
I have an unmanaged struct I'd like to marshal to c sharp that looks
I have an unmanaged dll with a class MyClass in it. Now is there
I have two unmanaged pointers in the form of IntPtr and want to copy

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.