为什么我的C#/ pinvoke DeviceIoControl调用返回0字节读取垃圾数据?

时间:2012-12-04 21:25:42

标签: c# deviceiocontrol

我有一个非托管的C ++ Windows控制台应用程序,运行正常。我希望它在C#中。我已经为必要的Kernel32.dll符号做了DllImport语句:

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

然后我有以下应用程序代码:

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

我的C#app打印“Bytes read = 0”,几何成员值是垃圾。我当然不是DllImport和marshaling的专家。请帮我理解我做错了什么。如果我将第五个参数更改为DeviceIoControl为“ref DiskGeometry”并在调用之前只传入一个创建的(而不是IntPtr和alloc),则所有打印的几何成员值都为0.

2 个答案:

答案 0 :(得分:1)

你错了,试试这个:

internal const uint IoCtlDiskGetDriveGeometry = 0x70000;

答案 1 :(得分:0)

尝试使用其中一个签名进行DllImport:http://pinvoke.net/default.aspx/kernel32.DeviceIoControl