From 0af5bc0919c5bedbe856968e32ae13d91e36555c Mon Sep 17 00:00:00 2001 From: Michael Bisbjerg Date: Thu, 9 Apr 2026 12:16:24 +0200 Subject: [PATCH 1/4] Normalize eol --- RawDiskLib.sln | 138 +++++++++++++++--------------- RawDiskLib/DiskNumberType.cs | 14 +-- RawDiskLib/Helpers/Win32Helper.cs | 106 +++++++++++------------ 3 files changed, 129 insertions(+), 129 deletions(-) diff --git a/RawDiskLib.sln b/RawDiskLib.sln index d90728a..d6f64de 100644 --- a/RawDiskLib.sln +++ b/RawDiskLib.sln @@ -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 diff --git a/RawDiskLib/DiskNumberType.cs b/RawDiskLib/DiskNumberType.cs index d912fa5..f06a408 100644 --- a/RawDiskLib/DiskNumberType.cs +++ b/RawDiskLib/DiskNumberType.cs @@ -1,8 +1,8 @@ -namespace RawDiskLib -{ - public enum DiskNumberType - { - PhysicalDisk, - Volume, - } +namespace RawDiskLib +{ + public enum DiskNumberType + { + PhysicalDisk, + Volume, + } } \ No newline at end of file diff --git a/RawDiskLib/Helpers/Win32Helper.cs b/RawDiskLib/Helpers/Win32Helper.cs index 965bad6..4e353c3 100644 --- a/RawDiskLib/Helpers/Win32Helper.cs +++ b/RawDiskLib/Helpers/Win32Helper.cs @@ -1,53 +1,53 @@ -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 res = new List(); - 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 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 res = new List(); + 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(); + } + } +} From f872ae952cc27518e4cfbfd9056057c4f88ef8de Mon Sep 17 00:00:00 2001 From: Michael Bisbjerg Date: Wed, 8 Apr 2026 12:16:01 +0200 Subject: [PATCH 2/4] Target net7-windows to use LibraryImport --- RawDiskLib/Helpers/PlatformShim.cs | 22 +++++++++++++++++++++- RawDiskLib/Helpers/Win32Helper.cs | 9 +++++++-- RawDiskLib/RawDiskLib.csproj | 8 ++------ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/RawDiskLib/Helpers/PlatformShim.cs b/RawDiskLib/Helpers/PlatformShim.cs index 543e46b..45c3bd7 100644 --- a/RawDiskLib/Helpers/PlatformShim.cs +++ b/RawDiskLib/Helpers/PlatformShim.cs @@ -6,8 +6,27 @@ namespace RawDiskLib.Helpers { - internal static class PlatformShim + internal static partial class PlatformShim { +#if NET7_0_OR_GREATER + [LibraryImport("kernel32.dll", 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", 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, @@ -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) { diff --git a/RawDiskLib/Helpers/Win32Helper.cs b/RawDiskLib/Helpers/Win32Helper.cs index 4e353c3..5f51289 100644 --- a/RawDiskLib/Helpers/Win32Helper.cs +++ b/RawDiskLib/Helpers/Win32Helper.cs @@ -1,17 +1,22 @@ -using System; +using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace RawDiskLib.Helpers { - internal static class Win32Helper + 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", SetLastError = true)] + private static partial int QueryDosDevice([MarshalAs(UnmanagedType.LPStr)] string lpDeviceName, byte[] lpTargetPath, int ucchMax); +#else [DllImport("kernel32.dll", SetLastError = true)] private static extern int QueryDosDevice(string lpDeviceName, byte[] lpTargetPath, int ucchMax); +#endif public static string[] GetAllDevices() { diff --git a/RawDiskLib/RawDiskLib.csproj b/RawDiskLib/RawDiskLib.csproj index 018a467..46bcfee 100644 --- a/RawDiskLib/RawDiskLib.csproj +++ b/RawDiskLib/RawDiskLib.csproj @@ -1,7 +1,8 @@  - netstandard1.3;netstandard2.0 + netstandard2.0;net7.0-windows;net8.0-windows + true Library to read and write to raw disks in Windows, something that can be tricky from time to time Raw-disk;Disk @@ -10,9 +11,4 @@ - - - - - From e03191412b2b6fbc1d83a08c10610aceef1d0e68 Mon Sep 17 00:00:00 2001 From: Michael Bisbjerg Date: Thu, 9 Apr 2026 12:18:56 +0200 Subject: [PATCH 3/4] Update testapplication to allow for testing --- Examples/TestApplication/TestApplication.csproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Examples/TestApplication/TestApplication.csproj b/Examples/TestApplication/TestApplication.csproj index 5a6c710..e806853 100644 --- a/Examples/TestApplication/TestApplication.csproj +++ b/Examples/TestApplication/TestApplication.csproj @@ -1,14 +1,14 @@  - + - $(DefaultTargetFramework) - Exe + netstandard2.0;net7.0-windows;net8.0-windows + Exe - + From 7ac6ecf643383ba000026497d7eec44b6dd0c726 Mon Sep 17 00:00:00 2001 From: Michael Bisbjerg Date: Thu, 9 Apr 2026 12:20:49 +0200 Subject: [PATCH 4/4] Use W variants of native methods --- RawDiskLib/Helpers/PlatformShim.cs | 4 ++-- RawDiskLib/Helpers/Win32Helper.cs | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/RawDiskLib/Helpers/PlatformShim.cs b/RawDiskLib/Helpers/PlatformShim.cs index 45c3bd7..9413dd2 100644 --- a/RawDiskLib/Helpers/PlatformShim.cs +++ b/RawDiskLib/Helpers/PlatformShim.cs @@ -9,7 +9,7 @@ namespace RawDiskLib.Helpers internal static partial class PlatformShim { #if NET7_0_OR_GREATER - [LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] + [LibraryImport("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] private static partial SafeFileHandle CreateFile( string lpFileName, FileAccess dwDesiredAccess, @@ -19,7 +19,7 @@ private static partial SafeFileHandle CreateFile( FileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile); - [LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] + [LibraryImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] [return: MarshalAs(UnmanagedType.Bool)] internal static partial bool GetDiskFreeSpace(string lpRootPathName, out uint lpSectorsPerCluster, diff --git a/RawDiskLib/Helpers/Win32Helper.cs b/RawDiskLib/Helpers/Win32Helper.cs index 5f51289..50981ee 100644 --- a/RawDiskLib/Helpers/Win32Helper.cs +++ b/RawDiskLib/Helpers/Win32Helper.cs @@ -11,21 +11,21 @@ internal static partial class Win32Helper private const int ERROR_SUCCSS = 0; #if NET7_0_OR_GREATER - [LibraryImport("kernel32.dll", SetLastError = true)] - private static partial int QueryDosDevice([MarshalAs(UnmanagedType.LPStr)] string lpDeviceName, byte[] lpTargetPath, int ucchMax); + [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", SetLastError = true)] - private static extern int QueryDosDevice(string lpDeviceName, byte[] lpTargetPath, int ucchMax); + [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; - byte[] bytes = new byte[16000]; + char[] chars = new char[16000]; do { - returnSize = QueryDosDevice(null, bytes, bytes.Length); + returnSize = QueryDosDevice(null, chars, chars.Length); int error = Marshal.GetLastWin32Error(); if (returnSize > 0 || error == ERROR_SUCCSS) @@ -34,18 +34,18 @@ public static string[] GetAllDevices() if (error != ERROR_INSUFFICIENT_BUFFER) throw new Exception("Unable to query DOS devices"); - bytes = new byte[bytes.Length * 2]; + chars = new char[chars.Length * 2]; } while (true); - // Parse as list of null-seperated ANSI strings + // Parse as list of null-seperated UTF-16 strings List res = new List(); 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) + if (chars[i] != '\0') + sb.Append(chars[i]); + else if (sb.Length > 0) { res.Add(sb.ToString()); sb.Clear();