Pinvoke命令长度不正确

时间:2013-03-22 07:41:29

标签: c# c++ pinvoke

我正在尝试使用GetFileInformationByHandle函数获取 FILE_ID_BOTH_DIR_INFO 。调用后我的所有值都设置为零。
我收到win32错误消息:
ERROR_BAD_LENGTH 程序发出命令,但命令长度不正确。

IntPtr handle = CreateFile(dir, EFileAccess.GenericRead, EFileShare.Read, IntPtr.Zero, ECreationDisposition.OpenExisting, EFileAttributes.BackupSemantics | EFileAttributes.Normal, IntPtr.Zero);
FILE_ID_BOTH_DIR_INFO fileStruct = new FILE_ID_BOTH_DIR_INFO();
GetFileInformationByHandleEx(handle, FILE_INFO_BY_HANDLE_CLASS.FileIdBothDirectoryInfo, out fileStruct, (uint)Marshal.SizeOf(fileStruct));

FILE_ID_BOTH_DIR_INFO结构

    typedef struct _FILE_ID_BOTH_DIR_INFO {
    DWORD         NextEntryOffset;
    DWORD         FileIndex;
    LARGE_INTEGER CreationTime;
    LARGE_INTEGER LastAccessTime;
    LARGE_INTEGER LastWriteTime;
    LARGE_INTEGER ChangeTime;
    LARGE_INTEGER EndOfFile;
    LARGE_INTEGER AllocationSize;
    DWORD         FileAttributes;
    DWORD         FileNameLength;
    DWORD         EaSize;
    CCHAR         ShortNameLength;
    WCHAR         ShortName[12];
    LARGE_INTEGER FileId;
    WCHAR         FileName[1];
    } FILE_ID_BOTH_DIR_INFO, *PFILE_ID_BOTH_DIR_INFO

在C#中

    public struct FILE_ID_BOTH_DIR_INFO
    {
        uint NextEntryOffset;
        uint FileIndex;
        LARGE_INTEGER CreationTime;
        LARGE_INTEGER LastAccessTime;
        LARGE_INTEGER LastWriteTime;
        LARGE_INTEGER ChangeTime;
        LARGE_INTEGER EndOfFile;
        LARGE_INTEGER AllocationSize;
        uint FileAttributes;
        uint FileNameLength;
        uint EaSize;
        char ShortNameLength;
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 12)]
        string ShortName;
        LARGE_INTEGER FileId;
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 1)]
        string FileName;
    }

    [StructLayout(LayoutKind.Explicit, Size = 8)]
    struct LARGE_INTEGER
    {
        [FieldOffset(0)]
        public Int64 QuadPart;
        [FieldOffset(0)]
        public UInt32 LowPart;
        [FieldOffset(4)]
        public Int32 HighPart;
    }

1 个答案:

答案 0 :(得分:2)

ByValTStr的字符集由StructLayout属性的CharSet参数确定。由于您未指定,因此使用默认的8位ANSI。如果您将CharSet指定为CharSet.Unicode,则应该处理该问题。将此属性添加到结构中:

[StructLayout(LayoutKind.Sequential, 
    CharSet=CharSet.Unicode)]

出于调试目的,编写一个输出结构大小的C ++程序。确保您的C#程序与该值匹配。