Reputation: 4862
I am trying to get the FILE_ID_BOTH_DIR_INFO with the GetFileInformationByHandle
function. All my values are set to zero after calling.
I get the win32 error message:
ERROR_BAD_LENGTH
The program issued a command but the command length is incorrect.
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 structure
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
In 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;
}
Upvotes: 0
Views: 739
Reputation: 613063
The character set for ByValTStr is determined by the CharSet argument of the StructLayout attribute. Since you've not specified that, the default of 8 bit ANSI is used. If you specify the CharSet as CharSet.Unicode then that should deal with the problem. Add this attribute to your struct:
[StructLayout(LayoutKind.Sequential,
CharSet=CharSet.Unicode)]
For the purposes of debugging write a C++ program that outputs the size of the struct. Make sure that your C# program matches that value.
Upvotes: 2