Tag: deviceiocontrol

使用C#在USB上创建多个分区

我试图使用DeviceIOControl在USB中创建多个partiions。 它始终只创建一个分区。 这是我的源代码 [DllImport(“kernel32.dll”, SetLastError = true)] static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); [DllImport(“kernel32”)] static extern int CloseHandle(IntPtr handle); [DllImport(“kernel32”)] private static extern int DeviceIoControl (IntPtr deviceHandle, uint ioControlCode, IntPtr inBuffer, int inBufferSize, IntPtr outBuffer, int outBufferSize, ref int bytesReturned, IntPtr overlapped); public const […]

Pinvoke DeviceIoControl参数

我正在使用DeviceIoControl C#项目。 我已经为我的签名咨询了相关的Pinvoke.net页面 : [DllImport(“Kernel32.dll”, SetLastError = false, CharSet = CharSet.Auto)] public static extern bool DeviceIoControl( SafeFileHandle hDevice, EIOControlCode IoControlCode, [MarshalAs(UnmanagedType.AsAny)] [In] object InBuffer, uint nInBufferSize, [MarshalAs(UnmanagedType.AsAny)] [Out] object OutBuffer, uint nOutBufferSize, out uint pBytesReturned, [In] IntPtr Overlapped ); 我之前从未见过object和[MarshalAs( UnmanagedType.AsAny )] ,但MSDN文档听起来很有希望: 一种动态类型,它在运行时确定对象的类型,并将对象编组为该类型。 该成员仅对平台调用方法有效。 我的问题是:使用此签名的“最佳”和/或“正确”方式是什么? 例如, IOCTL_STORAGE_QUERY_PROPERTY期望InBuffer是STORAGE_PROPERTY_QUERY结构。 看起来我应该能够定义该结构,创建一个new实例,并将其传递给我的Pinvoke签名: var query = new STORAGE_PROPERTY_QUERY { […]

物理磁盘大小不正确(IoCtlDiskGetDriveGeometry)

我使用下面的代码来获取物理磁盘大小 ,但返回的大小不正确。 我用其他工具检查了尺寸。 以下代码报告 总磁盘空间: 8.249.955.840字节 它应该是 总磁盘空间: 8.254.390.272字节 如何检索实际/正确的物理磁盘大小? 在USB驱动器和普通硬盘上测试。 代码很长,这里将它分开来显示。 结构: [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 { [DllImport(“Kernel32.dll”, SetLastError=true, CharSet=CharSet.Auto)] public static extern SafeFileHandle CreateFile( string fileName, uint fileAccess, uint fileShare, IntPtr […]