P /调用NtQueryVolumeInformationFile函数返回0xC0000003错误

C#导入代码:

[DllImport("ntdll.dll",ExactSpelling = true,SetLastError = true)]
public static extern long NtQueryVolumeInformationFile(IntPtr FileHandle,out IO_STATUS_BLOCK IoStatusBlock,IntPtr FsInformation,uint Length,FSINFOCLASS FsInformationClass);

FSINFOCLASS枚举:

public enum FSINFOCLASS
{
    FileFsVolumeInformation,FileFsLabelInformation,FileFsSizeInformation,FileFsDeviceInformation,FileFsAttributeInformation,FileFsControlInformation,FileFsFullSizeInformation,FileFsObjectIdInformation,FileFsDriverPathInformation,FileFsVolumeflagsInformation,FileFsSectorSizeInformation,FileFsDataCopyInformation,FileFsMetadataSizeInformation,FileFsFullSizeInformationEx,FileFsMaximumInformation
}

它的调用方式:

var handlePtr = Win32.CreateFile(@"\\.\c:",Win32.GENERIC_READ,Win32.FILE_SHARE_READ | Win32.FILE_SHARE_WRITE | Win32.FILE_SHARE_DELETE,Win32.OPEN_EXISTING,0);

if (handlePtr == IntPtr.Zero - 1)
    return 0;

var iosb = new Win32.IO_STATUS_BLOCK();
var buffer = Marshal.AllocHGlobal(273);

var result = Win32.NtQueryVolumeInformationFile(handlePtr,out iosb,buffer,273,Win32.FSINFOCLASS.FileFsVolumeInformation);
 // result=0xC0000003,should be zero

代码以管理权限运行。

错误代码的描述为:STATUS_INVALID_INFO_CLASS

但是据我所知,信息类的枚举是正确的。

我想念什么?

xuming_11 回答:P /调用NtQueryVolumeInformationFile函数返回0xC0000003错误

由于Jimi的评论,我将值1分配给枚举成员FileFsVolumeInformation,并修复了该错误。

但是,它需要更多修复才能完全正常运行。互操作导入的完整工作代码如下:

 [DllImport("ntdll.dll",ExactSpelling = true,SetLastError = true)]
        public static extern long NtQueryVolumeInformationFile(IntPtr FileHandle,out IO_STATUS_BLOCK IoStatusBlock,IntPtr FsInformation,uint Length,FSINFOCLASS FsInformationClass);
 [StructLayout(LayoutKind.Sequential,Pack = 0)]
        public struct FILE_FS_VOLUME_INFORMATION
        {
            public long VolumeCreationTime;
            public uint VolumeSerialNumber;
            public uint VolumeLabelLength;
            public bool SupportsObjects;
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 1)]
            public string VolumeLabel;
        }
   public enum FSINFOCLASS
        {
            FileFsVolumeInformation = 1,FileFsLabelInformation,FileFsSizeInformation,FileFsDeviceInformation,FileFsAttributeInformation,FileFsControlInformation,FileFsFullSizeInformation,FileFsObjectIdInformation,FileFsDriverPathInformation,FileFsVolumeFlagsInformation,FileFsSectorSizeInformation,FileFsDataCopyInformation,FileFsMetadataSizeInformation,FileFsFullSizeInformationEx,FileFsMaximumInformation
        }
[StructLayout(LayoutKind.Sequential,Pack = 0)]
        public struct IO_STATUS_BLOCK
        {
            public uint status;
            public IntPtr information;
        }
 public uint GetVolumeSerialNumber(string volumePath)
        {
            var handlePtr = Win32.CreateFile($@"\\.\{volumePath.TrimEnd('\\')}",Win32.GENERIC_READ,Win32.FILE_SHARE_READ | Win32.FILE_SHARE_WRITE | Win32.FILE_SHARE_DELETE,Win32.OPEN_EXISTING,0);

            if (handlePtr == IntPtr.Zero - 1)
                return 0;

            var iosb = new Win32.IO_STATUS_BLOCK();
            var fsInfo = new Win32.FILE_FS_VOLUME_INFORMATION();
            var bufferSize = Marshal.SizeOf(fsInfo) + 32; //32 =maximum ntfs volume label length
            var buffer = Marshal.AllocHGlobal(bufferSize);

            var result = Win32.NtQueryVolumeInformationFile(handlePtr,out iosb,buffer,(uint)bufferSize,Win32.FSINFOCLASS.FileFsVolumeInformation);
            if (result != 0)
            {
                Marshal.FreeHGlobal(buffer);
                Win32.CloseHandle(handlePtr);
                return 0;
            }

            fsInfo = Marshal.PtrToStructure<Win32.FILE_FS_VOLUME_INFORMATION>(buffer);
            Marshal.FreeHGlobal(buffer);
            return fsInfo.VolumeSerialNumber;
        }
本文链接:https://www.f2er.com/3167063.html

大家都在问