Skip to content
Merged
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
8 changes: 4 additions & 4 deletions Examples/TestApplication/TestApplication.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\_Imports\Test.targets" />
<Import Project="..\..\_Imports\Test.targets"/>

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<OutputType>Exe</OutputType>
<TargetFrameworks>netstandard2.0;net7.0-windows;net8.0-windows</TargetFrameworks>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\RawDiskLib\RawDiskLib.csproj" />
<ProjectReference Include="..\..\RawDiskLib\RawDiskLib.csproj"/>
</ItemGroup>

</Project>
138 changes: 69 additions & 69 deletions RawDiskLib.sln
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31105.61
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawDiskLib", "RawDiskLib\RawDiskLib.csproj", "{C2D3D361-FD53-4928-8467-72AAD5C63D24}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{C5A63324-038D-442A-A4E3-A73260C5A19C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication", "Examples\TestApplication\TestApplication.csproj", "{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D10F43F9-0CDD-4918-8FC8-8FCAF2DE31B3}"
ProjectSection(SolutionItems) = preProject
appveyor.yml = appveyor.yml
Directory.Build.props = Directory.Build.props
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Imports", "_Imports", "{85DFF48C-8580-486C-9ADF-47946BFBD1E1}"
ProjectSection(SolutionItems) = preProject
_Imports\Local.targets = _Imports\Local.targets
_Imports\Test.targets = _Imports\Test.targets
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x64.ActiveCfg = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x64.Build.0 = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x86.ActiveCfg = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x86.Build.0 = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|Any CPU.Build.0 = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x64.ActiveCfg = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x64.Build.0 = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x86.ActiveCfg = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x86.Build.0 = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x64.ActiveCfg = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x64.Build.0 = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x86.ActiveCfg = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x86.Build.0 = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|Any CPU.Build.0 = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x64.ActiveCfg = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x64.Build.0 = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x86.ActiveCfg = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0} = {C5A63324-038D-442A-A4E3-A73260C5A19C}
{85DFF48C-8580-486C-9ADF-47946BFBD1E1} = {D10F43F9-0CDD-4918-8FC8-8FCAF2DE31B3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {10FC3F59-4D76-411E-B2E8-89A0D096B85F}
EndGlobalSection
EndGlobal
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31105.61
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawDiskLib", "RawDiskLib\RawDiskLib.csproj", "{C2D3D361-FD53-4928-8467-72AAD5C63D24}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{C5A63324-038D-442A-A4E3-A73260C5A19C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication", "Examples\TestApplication\TestApplication.csproj", "{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D10F43F9-0CDD-4918-8FC8-8FCAF2DE31B3}"
ProjectSection(SolutionItems) = preProject
appveyor.yml = appveyor.yml
Directory.Build.props = Directory.Build.props
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Imports", "_Imports", "{85DFF48C-8580-486C-9ADF-47946BFBD1E1}"
ProjectSection(SolutionItems) = preProject
_Imports\Local.targets = _Imports\Local.targets
_Imports\Test.targets = _Imports\Test.targets
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x64.ActiveCfg = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x64.Build.0 = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x86.ActiveCfg = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Debug|x86.Build.0 = Debug|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|Any CPU.Build.0 = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x64.ActiveCfg = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x64.Build.0 = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x86.ActiveCfg = Release|Any CPU
{C2D3D361-FD53-4928-8467-72AAD5C63D24}.Release|x86.Build.0 = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x64.ActiveCfg = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x64.Build.0 = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x86.ActiveCfg = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Debug|x86.Build.0 = Debug|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|Any CPU.Build.0 = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x64.ActiveCfg = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x64.Build.0 = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x86.ActiveCfg = Release|Any CPU
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0D4905FB-6E29-4ADF-90B7-C560B962F3F0} = {C5A63324-038D-442A-A4E3-A73260C5A19C}
{85DFF48C-8580-486C-9ADF-47946BFBD1E1} = {D10F43F9-0CDD-4918-8FC8-8FCAF2DE31B3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {10FC3F59-4D76-411E-B2E8-89A0D096B85F}
EndGlobalSection
EndGlobal
14 changes: 7 additions & 7 deletions RawDiskLib/DiskNumberType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace RawDiskLib
{
public enum DiskNumberType
{
PhysicalDisk,
Volume,
}
namespace RawDiskLib
{
public enum DiskNumberType
{
PhysicalDisk,
Volume,
}
}
22 changes: 21 additions & 1 deletion RawDiskLib/Helpers/PlatformShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,27 @@

namespace RawDiskLib.Helpers
{
internal static class PlatformShim
internal static partial class PlatformShim
{
#if NET7_0_OR_GREATER
[LibraryImport("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
private static partial SafeFileHandle CreateFile(
string lpFileName,
FileAccess dwDesiredAccess,
FileShare dwShareMode,
IntPtr lpSecurityAttributes,
FileMode dwCreationDisposition,
FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);

[LibraryImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool GetDiskFreeSpace(string lpRootPathName,
out uint lpSectorsPerCluster,
out uint lpBytesPerSector,
out uint lpNumberOfFreeClusters,
out uint lpTotalNumberOfClusters);
#else
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern SafeFileHandle CreateFile(
string lpFileName,
Expand All @@ -24,6 +43,7 @@ internal static extern bool GetDiskFreeSpace(string lpRootPathName,
out uint lpBytesPerSector,
out uint lpNumberOfFreeClusters,
out uint lpTotalNumberOfClusters);
#endif

public static SafeFileHandle CreateDeviceHandle(string path, FileAccess access)
{
Expand Down
111 changes: 58 additions & 53 deletions RawDiskLib/Helpers/Win32Helper.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace RawDiskLib.Helpers
{
internal static class Win32Helper
{
private const int ERROR_INSUFFICIENT_BUFFER = 122;
private const int ERROR_SUCCSS = 0;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern int QueryDosDevice(string lpDeviceName, byte[] lpTargetPath, int ucchMax);

public static string[] GetAllDevices()
{
int returnSize;

byte[] bytes = new byte[16000];
do
{
returnSize = QueryDosDevice(null, bytes, bytes.Length);
int error = Marshal.GetLastWin32Error();

if (returnSize > 0 || error == ERROR_SUCCSS)
break;

if (error != ERROR_INSUFFICIENT_BUFFER)
throw new Exception("Unable to query DOS devices");

bytes = new byte[bytes.Length * 2];
} while (true);

// Parse as list of null-seperated ANSI strings
List<string> res = new List<string>();
StringBuilder sb = new StringBuilder(200);

for (int i = 0; i < returnSize; i++)
{
if (bytes[i] != 0)
sb.Append((char)bytes[i]);
else if (bytes[i] == 0 && sb.Length > 0)
{
res.Add(sb.ToString());
sb.Clear();
}
}

return res.ToArray();
}
}
}
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace RawDiskLib.Helpers
{
internal static partial class Win32Helper
{
private const int ERROR_INSUFFICIENT_BUFFER = 122;
private const int ERROR_SUCCSS = 0;

#if NET7_0_OR_GREATER
[LibraryImport("kernel32.dll", EntryPoint = "QueryDosDeviceW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
private static partial int QueryDosDevice(string lpDeviceName, char[] lpTargetPath, int ucchMax);
#else
[DllImport("kernel32.dll", EntryPoint = "QueryDosDeviceW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int QueryDosDevice(string lpDeviceName, char[] lpTargetPath, int ucchMax);
#endif

public static string[] GetAllDevices()
{
int returnSize;

char[] chars = new char[16000];
do
{
returnSize = QueryDosDevice(null, chars, chars.Length);
int error = Marshal.GetLastWin32Error();

if (returnSize > 0 || error == ERROR_SUCCSS)
break;

if (error != ERROR_INSUFFICIENT_BUFFER)
throw new Exception("Unable to query DOS devices");

chars = new char[chars.Length * 2];
} while (true);

// Parse as list of null-seperated UTF-16 strings
List<string> res = new List<string>();
StringBuilder sb = new StringBuilder(200);

for (int i = 0; i < returnSize; i++)
{
if (chars[i] != '\0')
sb.Append(chars[i]);
else if (sb.Length > 0)
{
res.Add(sb.ToString());
sb.Clear();
}
}

return res.ToArray();
}
}
}
8 changes: 2 additions & 6 deletions RawDiskLib/RawDiskLib.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net7.0-windows;net8.0-windows</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Description>Library to read and write to raw disks in Windows, something that can be tricky from time to time</Description>
<PackageTags>Raw-disk;Disk</PackageTags>
</PropertyGroup>
Expand All @@ -10,9 +11,4 @@
<PackageReference Include="DeviceIOControlLib" Version="0.1.6" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="System.IO.FileSystem.DriveInfo" Version="4.3.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.0" />
</ItemGroup>

</Project>
Loading