[Refactor] Use LibraryImport instead of DllImport.

This commit is contained in:
2024-05-01 05:36:13 +01:00
parent 01116106d7
commit c2e6e12b37
23 changed files with 326 additions and 258 deletions

View File

@@ -27,6 +27,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat> <SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors> <Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports> <DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer> <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup> </PropertyGroup>

View File

@@ -52,7 +52,7 @@ namespace Aaru.Checksums;
/// <inheritdoc /> /// <inheritdoc />
/// <summary>Implements the Adler-32 algorithm</summary> /// <summary>Implements the Adler-32 algorithm</summary>
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")] [SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
public sealed class Adler32Context : IChecksum public sealed partial class Adler32Context : IChecksum
{ {
internal const ushort ADLER_MODULE = 65521; internal const ushort ADLER_MODULE = 65521;
internal const uint NMAX = 5552; internal const uint NMAX = 5552;
@@ -130,17 +130,17 @@ public sealed class Adler32Context : IChecksum
#endregion #endregion
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr adler32_init(); private static partial IntPtr adler32_init();
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int adler32_update(IntPtr ctx, byte[] data, uint len); private static partial int adler32_update(IntPtr ctx, byte[] data, uint len);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int adler32_final(IntPtr ctx, ref uint checksum); private static partial int adler32_final(IntPtr ctx, ref uint checksum);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void adler32_free(IntPtr ctx); private static partial void adler32_free(IntPtr ctx);
static void Step(ref ushort preSum1, ref ushort preSum2, byte[] data, uint len, bool useNative, static void Step(ref ushort preSum1, ref ushort preSum2, byte[] data, uint len, bool useNative,
IntPtr nativeContext) IntPtr nativeContext)

View File

@@ -41,7 +41,7 @@ namespace Aaru.Checksums;
/// <inheritdoc /> /// <inheritdoc />
/// <summary>Implements a CRC16 algorithm</summary> /// <summary>Implements a CRC16 algorithm</summary>
public class Crc16Context : IChecksum public partial class Crc16Context : IChecksum
{ {
readonly ushort _finalSeed; readonly ushort _finalSeed;
readonly bool _inverse; readonly bool _inverse;
@@ -198,29 +198,29 @@ public class Crc16Context : IChecksum
#endregion #endregion
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc16_init(); private static partial IntPtr crc16_init();
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_update(IntPtr ctx, byte[] data, uint len); private static partial int crc16_update(IntPtr ctx, byte[] data, uint len);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_final(IntPtr ctx, ref ushort crc); private static partial int crc16_final(IntPtr ctx, ref ushort crc);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc16_free(IntPtr ctx); private static partial void crc16_free(IntPtr ctx);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc16_ccitt_init(); private static partial IntPtr crc16_ccitt_init();
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_ccitt_update(IntPtr ctx, byte[] data, uint len); private static partial int crc16_ccitt_update(IntPtr ctx, byte[] data, uint len);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc16_ccitt_final(IntPtr ctx, ref ushort crc); private static partial int crc16_ccitt_final(IntPtr ctx, ref ushort crc);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc16_ccitt_free(IntPtr ctx); private static partial void crc16_ccitt_free(IntPtr ctx);
static void Step(ref ushort previousCrc, ushort[][] table, byte[] data, uint len) static void Step(ref ushort previousCrc, ushort[][] table, byte[] data, uint len)
{ {

View File

@@ -49,7 +49,7 @@ namespace Aaru.Checksums;
[SuppressMessage("ReSharper", "MemberCanBeInternal")] [SuppressMessage("ReSharper", "MemberCanBeInternal")]
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")] [SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public sealed class Crc32Context : IChecksum public sealed partial class Crc32Context : IChecksum
{ {
const uint CRC32_ISO_POLY = 0xEDB88320; const uint CRC32_ISO_POLY = 0xEDB88320;
const uint CRC32_ISO_SEED = 0xFFFFFFFF; const uint CRC32_ISO_SEED = 0xFFFFFFFF;
@@ -426,17 +426,17 @@ public sealed class Crc32Context : IChecksum
#endregion #endregion
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc32_init(); private static partial IntPtr crc32_init();
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc32_update(IntPtr ctx, byte[] data, uint len); private static partial int crc32_update(IntPtr ctx, byte[] data, uint len);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc32_final(IntPtr ctx, ref uint crc); private static partial int crc32_final(IntPtr ctx, ref uint crc);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc32_free(IntPtr ctx); private static partial void crc32_free(IntPtr ctx);
static uint[][] GenerateTable(uint polynomial) static uint[][] GenerateTable(uint polynomial)
{ {

View File

@@ -48,7 +48,7 @@ namespace Aaru.Checksums;
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")] [SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "MemberCanBeInternal")] [SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class Crc64Context : IChecksum public sealed partial class Crc64Context : IChecksum
{ {
/// <summary>ECMA CRC64 polynomial</summary> /// <summary>ECMA CRC64 polynomial</summary>
const ulong CRC64_ECMA_POLY = 0xC96C5795D7870F42; const ulong CRC64_ECMA_POLY = 0xC96C5795D7870F42;
@@ -371,17 +371,17 @@ public sealed class Crc64Context : IChecksum
#endregion #endregion
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr crc64_init(); private static partial IntPtr crc64_init();
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc64_update(IntPtr ctx, byte[] data, uint len); private static partial int crc64_update(IntPtr ctx, byte[] data, uint len);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int crc64_final(IntPtr ctx, ref ulong crc); private static partial int crc64_final(IntPtr ctx, ref ulong crc);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void crc64_free(IntPtr ctx); private static partial void crc64_free(IntPtr ctx);
static ulong[][] GenerateTable(ulong polynomial) static ulong[][] GenerateTable(ulong polynomial)
{ {

View File

@@ -50,7 +50,7 @@ namespace Aaru.Checksums;
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")] [SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "UnusedMember.Global")] [SuppressMessage("ReSharper", "UnusedMember.Global")]
public sealed class Fletcher32Context : IChecksum public sealed partial class Fletcher32Context : IChecksum
{ {
internal const ushort FLETCHER_MODULE = 0xFFFF; internal const ushort FLETCHER_MODULE = 0xFFFF;
internal const uint NMAX = 5552; internal const uint NMAX = 5552;
@@ -128,17 +128,17 @@ public sealed class Fletcher32Context : IChecksum
#endregion #endregion
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr fletcher32_init(); private static partial IntPtr fletcher32_init();
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher32_update(IntPtr ctx, byte[] data, uint len); private static partial int fletcher32_update(IntPtr ctx, byte[] data, uint len);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher32_final(IntPtr ctx, ref uint crc); private static partial int fletcher32_final(IntPtr ctx, ref uint crc);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void fletcher32_free(IntPtr ctx); private static partial void fletcher32_free(IntPtr ctx);
static void Step(ref ushort previousSum1, ref ushort previousSum2, byte[] data, uint len, bool useNative, static void Step(ref ushort previousSum1, ref ushort previousSum2, byte[] data, uint len, bool useNative,
IntPtr nativeContext) IntPtr nativeContext)
@@ -418,7 +418,7 @@ public sealed class Fletcher32Context : IChecksum
[SuppressMessage("ReSharper", "UnusedMember.Global")] [SuppressMessage("ReSharper", "UnusedMember.Global")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")] [SuppressMessage("ReSharper", "UnusedMethodReturnValue.Global")]
public sealed class Fletcher16Context : IChecksum public sealed partial class Fletcher16Context : IChecksum
{ {
const byte FLETCHER_MODULE = 0xFF; const byte FLETCHER_MODULE = 0xFF;
const byte NMAX = 22; const byte NMAX = 22;
@@ -497,17 +497,17 @@ public sealed class Fletcher16Context : IChecksum
#endregion #endregion
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr fletcher16_init(); private static partial IntPtr fletcher16_init();
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher16_update(IntPtr ctx, byte[] data, uint len); private static partial int fletcher16_update(IntPtr ctx, byte[] data, uint len);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int fletcher16_final(IntPtr ctx, ref ushort checksum); private static partial int fletcher16_final(IntPtr ctx, ref ushort checksum);
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void fletcher16_free(IntPtr ctx); private static partial void fletcher16_free(IntPtr ctx);
static void Step(ref byte previousSum1, ref byte previousSum2, byte[] data, uint len, bool useNative, static void Step(ref byte previousSum1, ref byte previousSum2, byte[] data, uint len, bool useNative,
IntPtr nativeContext) IntPtr nativeContext)

View File

@@ -35,7 +35,7 @@ using System.Runtime.InteropServices;
namespace Aaru.Checksums; namespace Aaru.Checksums;
/// <summary>Handles native implementations of compression algorithms</summary> /// <summary>Handles native implementations of compression algorithms</summary>
public static class Native public static partial class Native
{ {
static bool _checked; static bool _checked;
static bool _supported; static bool _supported;
@@ -76,6 +76,6 @@ public static class Native
} }
} }
[DllImport("libAaru.Checksums.Native", SetLastError = true)] [LibraryImport("libAaru.Checksums.Native", SetLastError = true)]
static extern ulong get_acn_version(); private static partial ulong get_acn_version();
} }

View File

@@ -27,6 +27,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat> <SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors> <Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports> <DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer> <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">

View File

@@ -40,12 +40,13 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Security.Principal; using System.Security.Principal;
namespace Aaru.CommonTypes.Interop; namespace Aaru.CommonTypes.Interop;
/// <summary>Detects the underlying execution framework and operating system</summary> /// <summary>Detects the underlying execution framework and operating system</summary>
public static class DetectOS public static partial class DetectOS
{ {
/// <summary>Are we running under Mono?</summary> /// <summary>Are we running under Mono?</summary>
public static readonly bool IsMono = public static readonly bool IsMono =
@@ -98,11 +99,15 @@ public static class DetectOS
} }
} }
[DllImport("libc", SetLastError = true)] [LibraryImport("libc", SetLastError = true)]
static extern int uname(out UtsName name); private static partial int uname(out UtsName name);
[DllImport("libc", SetLastError = true, EntryPoint = "sysctlbyname", CharSet = CharSet.Ansi)] [LibraryImport("libc",
static extern int OSX_sysctlbyname(string name, IntPtr oldp, IntPtr oldlenp, IntPtr newp, uint newlen); EntryPoint = "sysctlbyname",
SetLastError = true,
StringMarshalling = StringMarshalling.Custom,
StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
private static partial int OSX_sysctlbyname(string name, IntPtr oldp, IntPtr oldlenp, IntPtr newp, uint newlen);
/// <summary>Gets the real platform ID, not the incomplete .NET framework one</summary> /// <summary>Gets the real platform ID, not the incomplete .NET framework one</summary>
/// <returns>Platform ID</returns> /// <returns>Platform ID</returns>

View File

@@ -44,7 +44,7 @@ namespace Aaru.Compression;
/// <summary>Implements the Apple version of RLE</summary> /// <summary>Implements the Apple version of RLE</summary>
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
public static class ADC public static partial class ADC
{ {
const int PLAIN = 1; const int PLAIN = 1;
const int TWO_BYTE = 2; const int TWO_BYTE = 2;
@@ -53,8 +53,8 @@ public static class ADC
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true; public static bool IsSupported => true;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_adc_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize); private static partial int AARU_adc_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
static int GetChunkType(byte byt) => (byt & 0x80) == 0x80 static int GetChunkType(byte byt) => (byt & 0x80) == 0x80

View File

@@ -36,15 +36,16 @@ using System.Runtime.InteropServices;
namespace Aaru.Compression; namespace Aaru.Compression;
/// <summary>Implements the Apple version of RLE</summary> /// <summary>Implements the Apple version of RLE</summary>
public static class AppleRle public static partial class AppleRle
{ {
const uint DART_CHUNK = 20960; const uint DART_CHUNK = 20960;
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true; public static bool IsSupported => true;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_apple_rle_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize); private static partial int AARU_apple_rle_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer,
int srcSize);
/// <summary>Decodes a buffer compressed with Apple RLE</summary> /// <summary>Decodes a buffer compressed with Apple RLE</summary>
/// <param name="source">Encoded buffer</param> /// <param name="source">Encoded buffer</param>

View File

@@ -33,17 +33,18 @@ using Ionic.BZip2;
namespace Aaru.Compression; namespace Aaru.Compression;
/// <summary>Implements the BZIP2 compression algorithm</summary> /// <summary>Implements the BZIP2 compression algorithm</summary>
public class BZip2 public partial class BZip2
{ {
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true; public static bool IsSupported => true;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_bzip2_decode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize); private static partial int AARU_bzip2_decode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer,
uint srcSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_bzip2_encode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer, uint srcSize, private static partial int AARU_bzip2_encode_buffer(byte[] dstBuffer, ref uint dstSize, byte[] srcBuffer,
int blockSize100K); uint srcSize, int blockSize100K);
/// <summary>Decodes a buffer compressed with BZIP2</summary> /// <summary>Decodes a buffer compressed with BZIP2</summary>
/// <param name="source">Encoded buffer</param> /// <param name="source">Encoded buffer</param>

View File

@@ -36,23 +36,24 @@ namespace Aaru.Compression;
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
/// <summary>Implements the FLAC lossless audio compression algorithm</summary> /// <summary>Implements the FLAC lossless audio compression algorithm</summary>
public class FLAC public partial class FLAC
{ {
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true; public static bool IsSupported => true;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_flac_decode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, private static partial nuint AARU_flac_decode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize); nuint srcSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_flac_encode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, private static partial nuint AARU_flac_encode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize, uint blocksize, int doMidSideStereo, nuint srcSize, uint blocksize, int doMidSideStereo,
int looseMidSideStereo, string apodization, uint maxLpcOrder, int looseMidSideStereo, string apodization,
uint qlpCoeffPrecision, int doQlpCoeffPrecSearch, uint maxLpcOrder, uint qlpCoeffPrecision,
int doExhaustiveModelSearch, uint minResidualPartitionOrder, int doQlpCoeffPrecSearch, int doExhaustiveModelSearch,
uint maxResidualPartitionOrder, string applicationID, uint minResidualPartitionOrder,
uint applicationIDLen); uint maxResidualPartitionOrder, string applicationID,
uint applicationIDLen);
/// <summary>Decodes a buffer compressed with FLAC</summary> /// <summary>Decodes a buffer compressed with FLAC</summary>
/// <param name="source">Encoded buffer</param> /// <param name="source">Encoded buffer</param>

View File

@@ -32,18 +32,18 @@ namespace Aaru.Compression;
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
/// <summary>Implements the LZFSE compression algorithm</summary> /// <summary>Implements the LZFSE compression algorithm</summary>
public class LZFSE public partial class LZFSE
{ {
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => Native.IsSupported; public static bool IsSupported => Native.IsSupported;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_lzfse_decode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize, private static partial nuint AARU_lzfse_decode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
byte[] scratchBuffer); nuint srcSize, byte[] scratchBuffer);
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_lzfse_encode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize, private static partial nuint AARU_lzfse_encode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
byte[] scratchBuffer); nuint srcSize, byte[] scratchBuffer);
/// <summary>Decodes a buffer compressed with LZFSE</summary> /// <summary>Decodes a buffer compressed with LZFSE</summary>
/// <param name="source">Encoded buffer</param> /// <param name="source">Encoded buffer</param>

View File

@@ -32,17 +32,17 @@ namespace Aaru.Compression;
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
/// <summary>Implements the LZIP compression algorithm</summary> /// <summary>Implements the LZIP compression algorithm</summary>
public class LZIP public partial class LZIP
{ {
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => Native.IsSupported; public static bool IsSupported => Native.IsSupported;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_lzip_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize); private static partial int AARU_lzip_decode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_lzip_encode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize, private static partial int AARU_lzip_encode_buffer(byte[] dstBuffer, int dstSize, byte[] srcBuffer, int srcSize,
int dictionarySize, int matchLenLimit); int dictionarySize, int matchLenLimit);
/// <summary>Decodes a buffer compressed with LZIP</summary> /// <summary>Decodes a buffer compressed with LZIP</summary>
/// <param name="source">Encoded buffer</param> /// <param name="source">Encoded buffer</param>

View File

@@ -34,19 +34,20 @@ using SharpCompress.Compressors.LZMA;
namespace Aaru.Compression; namespace Aaru.Compression;
/// <summary>Implements the LZMA compression algorithm</summary> /// <summary>Implements the LZMA compression algorithm</summary>
public class LZMA public partial class LZMA
{ {
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => true; public static bool IsSupported => true;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_lzma_decode_buffer(byte[] dstBuffer, ref nuint dstSize, byte[] srcBuffer, ref nuint srcSize, private static partial int AARU_lzma_decode_buffer(byte[] dstBuffer, ref nuint dstSize, byte[] srcBuffer,
byte[] props, nuint propsSize); ref nuint srcSize, byte[] props, nuint propsSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern int AARU_lzma_encode_buffer(byte[] dstBuffer, ref nuint dstSize, byte[] srcBuffer, nuint srcSize, private static partial int AARU_lzma_encode_buffer(byte[] dstBuffer, ref nuint dstSize, byte[] srcBuffer,
byte[] outProps, ref nuint outPropsSize, int level, uint dictSize, int lc, nuint srcSize, byte[] outProps, ref nuint outPropsSize,
int lp, int pb, int fb, int numThreads); int level, uint dictSize, int lc, int lp, int pb, int fb,
int numThreads);
/// <summary>Decodes a buffer compressed with LZMA</summary> /// <summary>Decodes a buffer compressed with LZMA</summary>
/// <param name="source">Encoded buffer</param> /// <param name="source">Encoded buffer</param>

View File

@@ -35,7 +35,7 @@ using System.Runtime.InteropServices;
namespace Aaru.Compression; namespace Aaru.Compression;
/// <summary>Handles native implementations of compression algorithms</summary> /// <summary>Handles native implementations of compression algorithms</summary>
public static class Native public static partial class Native
{ {
static bool _checked; static bool _checked;
static bool _supported; static bool _supported;
@@ -76,6 +76,6 @@ public static class Native
} }
} }
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern ulong AARU_get_acn_version(); private static partial ulong AARU_get_acn_version();
} }

View File

@@ -32,17 +32,18 @@ namespace Aaru.Compression;
// ReSharper disable once InconsistentNaming // ReSharper disable once InconsistentNaming
/// <summary>Implements the zstandard compression algorithm</summary> /// <summary>Implements the zstandard compression algorithm</summary>
public class ZSTD public partial class ZSTD
{ {
/// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary> /// <summary>Set to <c>true</c> if this algorithm is supported, <c>false</c> otherwise.</summary>
public static bool IsSupported => Native.IsSupported; public static bool IsSupported => Native.IsSupported;
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_zstd_decode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize); private static partial nuint AARU_zstd_decode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
nuint srcSize);
[DllImport("libAaru.Compression.Native", SetLastError = true)] [LibraryImport("libAaru.Compression.Native", SetLastError = true)]
static extern nuint AARU_zstd_encode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize, private static partial nuint AARU_zstd_encode_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer,
int compressionLevel); nuint srcSize, int compressionLevel);
/// <summary>Decodes a buffer compressed with ZSTD</summary> /// <summary>Decodes a buffer compressed with ZSTD</summary>
/// <param name="source">Encoded buffer</param> /// <param name="source">Encoded buffer</param>

View File

@@ -27,6 +27,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat> <SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors> <Authors>Natalia Portillo &lt;claunia@claunia.com&gt;</Authors>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports> <DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer> <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">

View File

@@ -32,47 +32,64 @@
// ****************************************************************************/ // ****************************************************************************/
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
namespace Aaru.Devices.Linux; namespace Aaru.Devices.Linux;
static class Extern static partial class Extern
{ {
[DllImport("libc", CharSet = CharSet.Ansi, SetLastError = true)] [LibraryImport("libc",
internal static extern int open(string pathname, [MarshalAs(UnmanagedType.U4)] FileFlags flags); SetLastError = true,
StringMarshalling = StringMarshalling.Custom,
StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
internal static partial int open(string pathname, [MarshalAs(UnmanagedType.U4)] FileFlags flags);
[DllImport("libc")] [LibraryImport("libc")]
internal static extern int close(int fd); internal static partial int close(int fd);
[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] [LibraryImport("libc", EntryPoint = "ioctl", SetLastError = true)]
internal static extern int ioctlSg(int fd, LinuxIoctl request, ref SgIoHdrT value); internal static partial int ioctlSg(int fd, LinuxIoctl request, ref SgIoHdrT value);
[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] [LibraryImport("libc", EntryPoint = "ioctl", SetLastError = true)]
internal static extern int ioctlMmc(int fd, LinuxIoctl request, ref MmcIocCmd value); internal static partial int ioctlMmc(int fd, LinuxIoctl request, ref MmcIocCmd value);
[DllImport("libc", CharSet = CharSet.Ansi, SetLastError = true)] [LibraryImport("libc",
internal static extern int readlink(string path, nint buf, int bufsize); SetLastError = true,
StringMarshalling = StringMarshalling.Custom,
StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
internal static partial int readlink(string path, nint buf, int bufsize);
[DllImport("libc", CharSet = CharSet.Ansi, EntryPoint = "readlink", SetLastError = true)] [LibraryImport("libc",
internal static extern long readlink64(string path, nint buf, long bufsize); EntryPoint = "readlink",
SetLastError = true,
StringMarshalling = StringMarshalling.Custom,
StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
internal static partial long readlink64(string path, nint buf, long bufsize);
[DllImport("libudev", CharSet = CharSet.Ansi, SetLastError = true)] [LibraryImport("libudev", SetLastError = true)]
internal static extern nint udev_new(); internal static partial nint udev_new();
[DllImport("libudev", CharSet = CharSet.Ansi, SetLastError = true)] [LibraryImport("libudev",
internal static extern nint udev_device_new_from_subsystem_sysname(nint udev, string subsystem, string sysname); SetLastError = true,
StringMarshalling = StringMarshalling.Custom,
StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
internal static partial nint udev_device_new_from_subsystem_sysname(nint udev, string subsystem, string sysname);
[DllImport("libudev", CharSet = CharSet.Ansi, SetLastError = true)] [LibraryImport("libudev",
internal static extern string udev_device_get_property_value(nint udevDevice, string key); SetLastError = true,
StringMarshalling = StringMarshalling.Custom,
StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
internal static partial string udev_device_get_property_value(nint udevDevice, string key);
[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] [LibraryImport("libc", EntryPoint = "ioctl", SetLastError = true)]
internal static extern int ioctlMmcMulti(int fd, LinuxIoctl request, nint value); internal static partial int ioctlMmcMulti(int fd, LinuxIoctl request, nint value);
[DllImport("libc", SetLastError = true)] [LibraryImport("libc", SetLastError = true)]
internal static extern long lseek(int fd, long offset, SeekWhence whence); internal static partial long lseek(int fd, long offset, SeekWhence whence);
[DllImport("libc", SetLastError = true)] [LibraryImport("libc", SetLastError = true)]
internal static extern int read(int fd, byte[] buf, int count); internal static partial int read(int fd, byte[] buf, int count);
[DllImport("libc", EntryPoint = "read", SetLastError = true)] [LibraryImport("libc", EntryPoint = "read", SetLastError = true)]
internal static extern long read64(int fd, byte[] buf, long count); internal static partial long read64(int fd, byte[] buf, long count);
} }

View File

@@ -39,85 +39,101 @@ using Microsoft.Win32.SafeHandles;
namespace Aaru.Devices.Windows; namespace Aaru.Devices.Windows;
[SuppressMessage("ReSharper", "UnusedMember.Global")] [SuppressMessage("ReSharper", "UnusedMember.Global")]
static class Extern static partial class Extern
{ {
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] [LibraryImport("kernel32.dll",
internal static extern SafeFileHandle CreateFile([MarshalAs(UnmanagedType.LPTStr)] string filename, EntryPoint = "CreateFileW",
[MarshalAs(UnmanagedType.U4)] FileAccess access, SetLastError = true,
[MarshalAs(UnmanagedType.U4)] FileShare share, StringMarshalling = StringMarshalling.Utf16)]
nint securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero internal static partial SafeFileHandle CreateFile([MarshalAs(UnmanagedType.LPTStr)] string filename,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, [MarshalAs(UnmanagedType.U4)] FileAccess access,
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes, [MarshalAs(UnmanagedType.U4)] FileShare share,
nint templateFile); nint securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
nint templateFile);
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControlScsi(SafeFileHandle hDevice, WindowsIoctl ioControlCode, [return: MarshalAs(UnmanagedType.Bool)]
ref ScsiPassThroughDirectAndSenseBuffer inBuffer, internal static partial bool DeviceIoControlScsi(SafeFileHandle hDevice, WindowsIoctl ioControlCode,
uint nInBufferSize, ref ScsiPassThroughDirectAndSenseBuffer inBuffer,
ref ScsiPassThroughDirectAndSenseBuffer outBuffer, uint nInBufferSize,
uint nOutBufferSize, ref uint pBytesReturned, nint overlapped); ref ScsiPassThroughDirectAndSenseBuffer outBuffer,
uint nOutBufferSize, ref uint pBytesReturned, nint overlapped);
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControlAta(SafeFileHandle hDevice, WindowsIoctl ioControlCode, [return: MarshalAs(UnmanagedType.Bool)]
ref AtaPassThroughDirect inBuffer, uint nInBufferSize, internal static partial bool DeviceIoControlAta(SafeFileHandle hDevice, WindowsIoctl ioControlCode,
ref AtaPassThroughDirect outBuffer, uint nOutBufferSize, ref AtaPassThroughDirect inBuffer, uint nInBufferSize,
ref uint pBytesReturned, nint overlapped); ref AtaPassThroughDirect outBuffer, uint nOutBufferSize,
ref uint pBytesReturned, nint overlapped);
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControlStorageQuery(SafeFileHandle hDevice, WindowsIoctl ioControlCode, [return: MarshalAs(UnmanagedType.Bool)]
ref StoragePropertyQuery inBuffer, uint nInBufferSize, internal static partial bool DeviceIoControlStorageQuery(SafeFileHandle hDevice, WindowsIoctl ioControlCode,
nint outBuffer, uint nOutBufferSize, ref StoragePropertyQuery inBuffer, uint nInBufferSize,
ref uint pBytesReturned, nint overlapped); nint outBuffer, uint nOutBufferSize,
ref uint pBytesReturned, nint overlapped);
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControlIde(SafeFileHandle hDevice, WindowsIoctl ioControlCode, [return: MarshalAs(UnmanagedType.Bool)]
ref IdePassThroughDirect inBuffer, uint nInBufferSize, internal static partial bool DeviceIoControlIde(SafeFileHandle hDevice, WindowsIoctl ioControlCode,
ref IdePassThroughDirect outBuffer, uint nOutBufferSize, ref IdePassThroughDirect inBuffer, uint nInBufferSize,
ref uint pBytesReturned, nint overlapped); ref IdePassThroughDirect outBuffer, uint nOutBufferSize,
ref uint pBytesReturned, nint overlapped);
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControlGetDeviceNumber(SafeFileHandle hDevice, WindowsIoctl ioControlCode, [return: MarshalAs(UnmanagedType.Bool)]
nint inBuffer, uint nInBufferSize, internal static partial bool DeviceIoControlGetDeviceNumber(SafeFileHandle hDevice, WindowsIoctl ioControlCode,
ref StorageDeviceNumber outBuffer, uint nOutBufferSize, nint inBuffer, uint nInBufferSize,
ref uint pBytesReturned, nint overlapped); ref StorageDeviceNumber outBuffer, uint nOutBufferSize,
ref uint pBytesReturned, nint overlapped);
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControl(SafeFileHandle hDevice, WindowsIoctl ioControlCode, nint inBuffer, [return: MarshalAs(UnmanagedType.Bool)]
uint nInBufferSize, ref SffdiskQueryDeviceProtocolData outBuffer, internal static partial bool DeviceIoControl(SafeFileHandle hDevice, WindowsIoctl ioControlCode, nint inBuffer,
uint nOutBufferSize, out uint pBytesReturned, nint overlapped); uint nInBufferSize, ref SffdiskQueryDeviceProtocolData outBuffer,
uint nOutBufferSize, out uint pBytesReturned, nint overlapped);
[DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern bool DeviceIoControl(SafeFileHandle hDevice, WindowsIoctl ioControlCode, byte[] inBuffer, [return: MarshalAs(UnmanagedType.Bool)]
uint nInBufferSize, byte[] outBuffer, uint nOutBufferSize, internal static partial bool DeviceIoControl(SafeFileHandle hDevice, WindowsIoctl ioControlCode, byte[] inBuffer,
out uint pBytesReturned, nint overlapped); uint nInBufferSize, byte[] outBuffer, uint nOutBufferSize,
out uint pBytesReturned, nint overlapped);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetClassDevsW")]
internal static extern SafeFileHandle SetupDiGetClassDevs(ref Guid classGuid, nint enumerator, nint hwndParent, internal static partial SafeFileHandle SetupDiGetClassDevs(ref Guid classGuid, nint enumerator, nint hwndParent,
DeviceGetClassFlags flags); DeviceGetClassFlags flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] [LibraryImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiEnumDeviceInterfaces(SafeFileHandle hDevInfo, nint devInfo, [return: MarshalAs(UnmanagedType.Bool)]
ref Guid interfaceClassGuid, uint memberIndex, public static partial bool SetupDiEnumDeviceInterfaces(SafeFileHandle hDevInfo, nint devInfo,
ref DeviceInterfaceData deviceInterfaceData); ref Guid interfaceClassGuid, uint memberIndex,
ref DeviceInterfaceData deviceInterfaceData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceInterfaceDetailW", SetLastError = true)]
public static extern bool SetupDiGetDeviceInterfaceDetail(SafeFileHandle hDevInfo, [return: MarshalAs(UnmanagedType.Bool)]
ref DeviceInterfaceData deviceInterfaceData, public static partial bool SetupDiGetDeviceInterfaceDetail(SafeFileHandle hDevInfo,
nint deviceInterfaceDetailData, ref DeviceInterfaceData deviceInterfaceData,
uint deviceInterfaceDetailDataSize, ref uint requiredSize, nint deviceInterfaceDetailData,
nint deviceInfoData); uint deviceInterfaceDetailDataSize,
ref uint requiredSize, nint deviceInfoData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] [LibraryImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(SafeFileHandle hDevInfo); [return: MarshalAs(UnmanagedType.Bool)]
public static partial bool SetupDiDestroyDeviceInfoList(SafeFileHandle hDevInfo);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", SetLastError = true)]
internal static extern bool CloseHandle(SafeFileHandle hDevice); [return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool CloseHandle(SafeFileHandle hDevice);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("Kernel32.dll", SetLastError = true)]
public static extern bool SetFilePointerEx(SafeFileHandle hFile, long liDistanceToMove, out long lpNewFilePointer, [return: MarshalAs(UnmanagedType.Bool)]
MoveMethod dwMoveMethod); public static partial bool SetFilePointerEx(SafeFileHandle hFile, long liDistanceToMove, out long lpNewFilePointer,
MoveMethod dwMoveMethod);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadFile(SafeFileHandle hFile, byte[] lpBuffer, uint nNumberOfBytesToRead, [return: MarshalAs(UnmanagedType.Bool)]
out uint lpNumberOfBytesRead, nint lpOverlapped); public static partial bool ReadFile(SafeFileHandle hFile, byte[] lpBuffer, uint nNumberOfBytesToRead,
out uint lpNumberOfBytesRead, nint lpOverlapped);
} }

View File

@@ -1170,54 +1170,76 @@ static partial class Usb
// ********************** API Definitions ************************ // ********************** API Definitions ************************
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetClassDevsW")]
static extern IntPtr SetupDiGetClassDevs( // 1st form using a ClassGUID private static partial IntPtr SetupDiGetClassDevs( // 1st form using a ClassGUID
ref Guid classGuid, int enumerator, IntPtr hwndParent, int flags); ref Guid classGuid, int enumerator, IntPtr hwndParent, int flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] // 2nd form uses an Enumerator [LibraryImport("setupapi.dll",
static extern IntPtr SetupDiGetClassDevs(int classGuid, string enumerator, IntPtr hwndParent, int flags); EntryPoint = "SetupDiGetClassDevsW",
StringMarshalling = StringMarshalling.Utf16)] // 2nd form uses an Enumerator
private static partial IntPtr SetupDiGetClassDevs(int classGuid, string enumerator,
IntPtr hwndParent, int flags);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, IntPtr deviceInfoData, [return: MarshalAs(UnmanagedType.Bool)]
ref Guid interfaceClassGuid, int memberIndex, private static partial bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, IntPtr deviceInfoData,
ref SpDeviceInterfaceData deviceInterfaceData); ref Guid interfaceClassGuid, int memberIndex,
ref SpDeviceInterfaceData
deviceInterfaceData);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceInterfaceDetailW", SetLastError = true)]
static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, [return: MarshalAs(UnmanagedType.Bool)]
ref SpDeviceInterfaceData deviceInterfaceData, private static partial bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet,
ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, ref SpDeviceInterfaceData
int deviceInterfaceDetailDataSize, ref int requiredSize, deviceInterfaceData,
ref SpDevinfoData deviceInfoData); ref SpDeviceInterfaceDetailData
deviceInterfaceDetailData,
int deviceInterfaceDetailDataSize,
ref int requiredSize,
ref SpDevinfoData deviceInfoData);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll",
static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, EntryPoint = "SetupDiGetDeviceRegistryPropertyW",
int iProperty, ref int propertyRegDataType, SetLastError = true)]
IntPtr propertyBuffer, int propertyBufferSize, [return: MarshalAs(UnmanagedType.Bool)]
ref int requiredSize); private static partial bool SetupDiGetDeviceRegistryProperty(
IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, int iProperty,
ref int propertyRegDataType, IntPtr propertyBuffer, int propertyBufferSize,
ref int requiredSize);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex, ref SpDevinfoData deviceInfoData); [return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex,
ref SpDevinfoData deviceInfoData);
[DllImport("setupapi.dll", SetLastError = true)] [LibraryImport("setupapi.dll", SetLastError = true)]
static extern bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet); [return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceInstanceIdW", SetLastError = true)]
static extern bool SetupDiGetDeviceInstanceId(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, [return: MarshalAs(UnmanagedType.Bool)]
StringBuilder deviceInstanceId, int deviceInstanceIdSize, private static partial bool SetupDiGetDeviceInstanceId(
out int requiredSize); IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, StringBuilder deviceInstanceId,
int deviceInstanceIdSize, out int requiredSize);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("kernel32.dll", SetLastError = true)]
static extern bool DeviceIoControl(IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize, [return: MarshalAs(UnmanagedType.Bool)]
IntPtr lpOutBuffer, int nOutBufferSize, out int lpBytesReturned, private static partial bool DeviceIoControl(IntPtr hDevice, int dwIoControlCode, IntPtr lpInBuffer,
IntPtr lpOverlapped); int nInBufferSize, IntPtr lpOutBuffer,
int nOutBufferSize, out int lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("kernel32.dll",
static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, EntryPoint = "CreateFileW",
IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, SetLastError = true,
IntPtr hTemplateFile); StringMarshalling = StringMarshalling.Utf16)]
private static partial IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode,
IntPtr lpSecurityAttributes, int dwCreationDisposition,
int dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [LibraryImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hObject); [return: MarshalAs(UnmanagedType.Bool)]
private static partial bool CloseHandle(IntPtr hObject);
#endregion #endregion
} }

View File

@@ -164,11 +164,11 @@ static partial class Usb
} }
} }
[DllImport("setupapi.dll")] [LibraryImport("setupapi.dll")]
static extern int CM_Get_Parent(out uint pdnDevInst, uint dnDevInst, int ulFlags); private static partial int CM_Get_Parent(out uint pdnDevInst, uint dnDevInst, int ulFlags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] [LibraryImport("setupapi.dll", EntryPoint = "CM_Get_Device_IDW")]
static extern int CM_Get_Device_ID(uint dnDevInst, IntPtr buffer, int bufferLen, int ulFlags); private static partial int CM_Get_Device_ID(uint dnDevInst, IntPtr buffer, int bufferLen, int ulFlags);
/// <summary>Find a device based upon a Drive Letter</summary> /// <summary>Find a device based upon a Drive Letter</summary>
/// <param name="driveLetter">Drive letter</param> /// <param name="driveLetter">Drive letter</param>