Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions DeviceIOControlLib/DeviceIOControlLib.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net8.0-windows;net9.0-windows;net10.0-windows</TargetFrameworks>
<Description>Library to interact with Windows' DeviceIoControl method</Description>
<PackageTags>Win32 DeviceIoControl</PackageTags>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Win32.Primitives" Version="4.3.0" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="System.IO.FileSystem" Version="4.3.0" />
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
<PackageReference Include="System.Runtime" Version="4.3.0" />
<PackageReference Include="System.Runtime.Extensions" Version="4.3.0" />
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using DeviceIOControlLib.Objects.Enums;

Expand All @@ -10,7 +11,5 @@ internal struct DRIVE_LAYOUT_INFORMATION_EX_INTERNAL
public PartitionStyle PartitionStyle;
public int PartitionCount;
public DRIVE_LAYOUT_INFORMATION_UNION DriveLayoutInformaiton;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 128)]
public PARTITION_INFORMATION_EX[] PartitionEntry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ internal struct DRIVE_LAYOUT_INFORMATION_INTERNAL
{
public int PartitionCount;
public uint Signature;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 128)]
public PARTITION_INFORMATION[] PartitionEntry;
}
}
5 changes: 2 additions & 3 deletions DeviceIOControlLib/Objects/Disk/GETVERSIONINPARAMS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
namespace DeviceIOControlLib.Objects.Disk
{
[StructLayout(LayoutKind.Sequential)]
public struct GETVERSIONINPARAMS
public unsafe struct GETVERSIONINPARAMS
{
public byte bVersion;
public byte bRevision;
public byte bReserved;
public byte bIDEDeviceMap;
public ulong fCapabilities;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public ulong[] dwReserved;
public fixed ulong dwReserved[4];
}
}
5 changes: 2 additions & 3 deletions DeviceIOControlLib/Objects/Disk/PARTITION_INFORMATION_GPT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DeviceIOControlLib.Objects.Disk
{
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct PARTITION_INFORMATION_GPT
public unsafe struct PARTITION_INFORMATION_GPT
{
[FieldOffset(0)]
public Guid PartitionType;
Expand All @@ -15,7 +15,6 @@ public struct PARTITION_INFORMATION_GPT
[MarshalAs(UnmanagedType.U8)]
public EFIPartitionAttributes Attributes;
[FieldOffset(40)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
public string Name;
public fixed char Name[36];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DeviceIOControlLib.Objects.Storage
{
[StructLayout(LayoutKind.Sequential)]
public struct STORAGE_DEVICE_DESCRIPTOR
public unsafe struct STORAGE_DEVICE_DESCRIPTOR
{
public uint Version;
public uint Size;
Expand All @@ -20,6 +20,6 @@ public struct STORAGE_DEVICE_DESCRIPTOR
public STORAGE_BUS_TYPE BusType;
public uint RawPropertiesLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x16)]
public byte[] RawDeviceProperties;
public fixed byte RawDeviceProperties[0x16];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DeviceIOControlLib.Objects.Storage
{
[StructLayout(LayoutKind.Sequential)]
public struct STORAGE_DEVICE_DESCRIPTOR_PARSED
public unsafe struct STORAGE_DEVICE_DESCRIPTOR_PARSED
{
public uint Version;
public uint Size;
Expand All @@ -20,7 +20,7 @@ public struct STORAGE_DEVICE_DESCRIPTOR_PARSED
public STORAGE_BUS_TYPE BusType;
public uint RawPropertiesLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x16)]
public byte[] RawDeviceProperties;
public fixed byte RawDeviceProperties[0x16];
public string SerialNumber;
public string VendorId;
public string ProductId;
Expand Down
4 changes: 2 additions & 2 deletions DeviceIOControlLib/Objects/Storage/STORAGE_PROPERTY_QUERY.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace DeviceIOControlLib.Objects.Storage
{
[StructLayout(LayoutKind.Sequential)]
public struct STORAGE_PROPERTY_QUERY
public unsafe struct STORAGE_PROPERTY_QUERY
{
public STORAGE_PROPERTY_ID PropertyId;
public STORAGE_QUERY_TYPE QueryType;
public byte[] AdditionalParameters;
public byte* AdditionalParameters;
}
}
21 changes: 16 additions & 5 deletions DeviceIOControlLib/Objects/Usn/USN_RECORD_V2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace DeviceIOControlLib.Objects.Usn
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct USN_RECORD_V2 : IUSN_RECORD
public unsafe struct USN_RECORD_V2 : IUSN_RECORD
{
private uint _recordLength;
private ushort _majorVersion;
Expand All @@ -19,8 +19,7 @@ public struct USN_RECORD_V2 : IUSN_RECORD
private FileAttributes _fileAttributes;
private ushort _fileNameLength;
private ushort _fileNameOffset;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
private string _fileName;
private fixed char _fileName[1];

public uint RecordLength
{
Expand Down Expand Up @@ -89,8 +88,20 @@ public ushort FileNameOffset
}
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
get
{
fixed(char* fileNamePtr = _fileName)
{
return new string(fileNamePtr, 0, 1);
}
}
set
{
fixed(char * fileNamePtr = _fileName)
{
fileNamePtr[0] = value[0];
}
}
}
}
}
71 changes: 58 additions & 13 deletions DeviceIOControlLib/Objects/Usn/USN_RECORD_V3.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Runtime.InteropServices;
using DeviceIOControlLib.Objects.Enums;

namespace DeviceIOControlLib.Objects.Usn
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct USN_RECORD_V3 : IUSN_RECORD
public unsafe struct USN_RECORD_V3 : IUSN_RECORD
{
internal uint _recordLength;
internal ushort _majorVersion;
internal ushort _minorVersion;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] _fileReferenceNumber;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] _parentFileReferenceNumber;
public fixed byte _fileReferenceNumber[16];
public fixed byte _parentFileReferenceNumber[16];
public long _usn;
public ulong _timeStamp;
public UsnJournalReasonMask _reason;
Expand All @@ -21,8 +20,7 @@ public struct USN_RECORD_V3 : IUSN_RECORD
public FileAttributes _fileAttributes;
public ushort _fileNameLength;
public ushort _fileNameOffset;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public string _fileName;
public fixed char _fileName[1];

public uint RecordLength
{
Expand All @@ -41,13 +39,48 @@ public ushort MinorVersion
}
public byte[] FileReferenceNumber
{
get { return _fileReferenceNumber; }
set { _fileReferenceNumber = value; }
get
{
byte[] fileReferenceNumber = new byte[16];
fixed (byte* fileReferenceNumberDst = fileReferenceNumber)
fixed (byte* fileReferenceNumberSrc = _fileReferenceNumber)
{
Buffer.MemoryCopy(fileReferenceNumberSrc, fileReferenceNumberDst, 16, 16);
}

return fileReferenceNumber;
}
set
{
fixed (byte* fileReferenceNumberDst = _fileReferenceNumber)
fixed (byte* fileReferenceNumberSrc = value)
{
Buffer.MemoryCopy(fileReferenceNumberSrc, fileReferenceNumberDst, 16, 16);
}
}
}

public byte[] ParentFileReferenceNumber
{
get { return _parentFileReferenceNumber; }
set { _parentFileReferenceNumber = value; }
get
{
byte[] parentFileReferenceNumber = new byte[16];
fixed (byte* parentFileReferenceNumberDst = parentFileReferenceNumber)
fixed (byte* parentFileReferenceNumberSrc = _parentFileReferenceNumber)
{
Buffer.MemoryCopy(parentFileReferenceNumberSrc, parentFileReferenceNumberDst, 16, 16);
}

return parentFileReferenceNumber;
}
set
{
fixed (byte* parentFileReferenceNumberDst = _parentFileReferenceNumber)
fixed (byte* parentFileReferenceNumberSrc = value)
{
Buffer.MemoryCopy(parentFileReferenceNumberSrc, parentFileReferenceNumberDst, 16, 16);
}
}
}
public USN Usn
{
Expand Down Expand Up @@ -91,8 +124,20 @@ public ushort FileNameOffset
}
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
get
{
fixed (char* fileNamePtr = _fileName)
{
return new string(fileNamePtr, 0, 1);
}
}
set
{
fixed (char* fileNamePtr = _fileName)
{
fileNamePtr[0] = value[0];
}
}
}
}
}
16 changes: 4 additions & 12 deletions DeviceIOControlLib/Utilities/MarshalHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ namespace DeviceIOControlLib.Utilities
{
internal static class MarshalHelper
{
public static T ToStructure<T>(this IntPtr ptr)
public static unsafe T ToStructure<T>(this IntPtr ptr) where T : unmanaged
{
#if NETCORE
return Marshal.PtrToStructure<T>(ptr);
#else
return (T)Marshal.PtrToStructure(ptr, typeof(T));
#endif
return *(T*)ptr.ToPointer();
}

public static uint SizeOf<T>()
public static unsafe uint SizeOf<T>() where T : unmanaged
{
#if NETCORE
return (uint)Marshal.SizeOf<T>();
#else
return (uint)Marshal.SizeOf(typeof(T));
#endif
return (uint)sizeof(T);
}
}
}
2 changes: 1 addition & 1 deletion DeviceIOControlLib/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static string GetWin32ErrorMessage(int errorCode)
return new Win32Exception(errorCode).Message;
}

public static T ByteArrayToStruct<T>(byte[] data, int index) where T : struct
public static T ByteArrayToStruct<T>(byte[] data, int index) where T : unmanaged
{
using (UnmanagedMemory mem = new UnmanagedMemory(data.Length - index))
{
Expand Down
Loading