From 87613c03b72bfafebc80086e4f4f0889878c18da Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 1 May 2024 16:32:02 +0100 Subject: [PATCH] [Refactor] Rollback to `DllImport` calls that need custom marshallers for now. --- Aaru.CommonTypes/Interop/DetectOS.cs | 4 +-- Aaru.Compression/FLAC.cs | 2 +- Aaru.Devices/Linux/Enums.cs | 2 +- Aaru.Devices/Linux/Extern.cs | 6 ++-- Aaru.Devices/Windows/Extern.cs | 41 ++++++++++++---------------- Aaru.Devices/Windows/Usb.cs | 27 ++++++++---------- 6 files changed, 37 insertions(+), 45 deletions(-) diff --git a/Aaru.CommonTypes/Interop/DetectOS.cs b/Aaru.CommonTypes/Interop/DetectOS.cs index fe5eacf4e..516ffae2a 100644 --- a/Aaru.CommonTypes/Interop/DetectOS.cs +++ b/Aaru.CommonTypes/Interop/DetectOS.cs @@ -99,8 +99,8 @@ public static partial class DetectOS } } - [LibraryImport("libc", SetLastError = true)] - private static partial int uname(out UtsName name); + [DllImport("libc", SetLastError = true)] + static extern int uname(out UtsName name); [LibraryImport("libc", EntryPoint = "sysctlbyname", diff --git a/Aaru.Compression/FLAC.cs b/Aaru.Compression/FLAC.cs index 5b4a04ac9..c5762797e 100644 --- a/Aaru.Compression/FLAC.cs +++ b/Aaru.Compression/FLAC.cs @@ -45,7 +45,7 @@ public partial class FLAC private static partial nuint AARU_flac_decode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize); - [LibraryImport("libAaru.Compression.Native", SetLastError = true)] + [LibraryImport("libAaru.Compression.Native", SetLastError = true, StringMarshalling = StringMarshalling.Utf8)] private static partial nuint AARU_flac_encode_redbook_buffer(byte[] dstBuffer, nuint dstSize, byte[] srcBuffer, nuint srcSize, uint blocksize, int doMidSideStereo, int looseMidSideStereo, string apodization, diff --git a/Aaru.Devices/Linux/Enums.cs b/Aaru.Devices/Linux/Enums.cs index e9b80b08d..10525a3fb 100644 --- a/Aaru.Devices/Linux/Enums.cs +++ b/Aaru.Devices/Linux/Enums.cs @@ -38,7 +38,7 @@ namespace Aaru.Devices.Linux; [Flags] [SuppressMessage("ReSharper", "UnusedMember.Global")] -enum FileFlags +enum FileFlags : uint { /// O_RDONLY Readonly = 0x0, diff --git a/Aaru.Devices/Linux/Extern.cs b/Aaru.Devices/Linux/Extern.cs index 1d3a8f290..392b310ab 100644 --- a/Aaru.Devices/Linux/Extern.cs +++ b/Aaru.Devices/Linux/Extern.cs @@ -42,7 +42,7 @@ static partial class Extern SetLastError = true, StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(AnsiStringMarshaller))] - internal static partial int open(string pathname, [MarshalAs(UnmanagedType.U4)] FileFlags flags); + internal static partial int open(string pathname, FileFlags flags); [LibraryImport("libc")] internal static partial int close(int fd); @@ -50,8 +50,8 @@ static partial class Extern [LibraryImport("libc", EntryPoint = "ioctl", SetLastError = true)] internal static partial int ioctlSg(int fd, LinuxIoctl request, ref SgIoHdrT value); - [LibraryImport("libc", EntryPoint = "ioctl", SetLastError = true)] - internal static partial int ioctlMmc(int fd, LinuxIoctl request, ref MmcIocCmd value); + [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)] + internal static extern int ioctlMmc(int fd, LinuxIoctl request, ref MmcIocCmd value); [LibraryImport("libc", SetLastError = true, diff --git a/Aaru.Devices/Windows/Extern.cs b/Aaru.Devices/Windows/Extern.cs index 5cd071b55..0e6913ced 100644 --- a/Aaru.Devices/Windows/Extern.cs +++ b/Aaru.Devices/Windows/Extern.cs @@ -46,20 +46,17 @@ static partial class Extern SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] internal static partial SafeFileHandle CreateFile([MarshalAs(UnmanagedType.LPTStr)] string filename, - [MarshalAs(UnmanagedType.U4)] FileAccess access, - [MarshalAs(UnmanagedType.U4)] FileShare share, + FileAccess access, FileShare share, nint securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero - [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, - [MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes, + FileMode creationDisposition, FileAttributes flagsAndAttributes, nint templateFile); - [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static partial bool DeviceIoControlScsi(SafeFileHandle hDevice, WindowsIoctl ioControlCode, - ref ScsiPassThroughDirectAndSenseBuffer inBuffer, - uint nInBufferSize, - ref ScsiPassThroughDirectAndSenseBuffer outBuffer, - uint nOutBufferSize, ref uint pBytesReturned, nint overlapped); + [DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] + internal static extern bool DeviceIoControlScsi(SafeFileHandle hDevice, WindowsIoctl ioControlCode, + ref ScsiPassThroughDirectAndSenseBuffer inBuffer, + uint nInBufferSize, + ref ScsiPassThroughDirectAndSenseBuffer outBuffer, + uint nOutBufferSize, ref uint pBytesReturned, nint overlapped); [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] @@ -68,19 +65,17 @@ static partial class Extern ref AtaPassThroughDirect outBuffer, uint nOutBufferSize, ref uint pBytesReturned, nint overlapped); - [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static partial bool DeviceIoControlStorageQuery(SafeFileHandle hDevice, WindowsIoctl ioControlCode, - ref StoragePropertyQuery inBuffer, uint nInBufferSize, - nint outBuffer, uint nOutBufferSize, - ref uint pBytesReturned, nint overlapped); + [DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] + internal static extern bool DeviceIoControlStorageQuery(SafeFileHandle hDevice, WindowsIoctl ioControlCode, + ref StoragePropertyQuery inBuffer, uint nInBufferSize, + nint outBuffer, uint nOutBufferSize, + ref uint pBytesReturned, nint overlapped); - [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static partial bool DeviceIoControlIde(SafeFileHandle hDevice, WindowsIoctl ioControlCode, - ref IdePassThroughDirect inBuffer, uint nInBufferSize, - ref IdePassThroughDirect outBuffer, uint nOutBufferSize, - ref uint pBytesReturned, nint overlapped); + [DllImport("Kernel32.dll", SetLastError = true, EntryPoint = "DeviceIoControl", CharSet = CharSet.Auto)] + internal static extern bool DeviceIoControlIde(SafeFileHandle hDevice, WindowsIoctl ioControlCode, + ref IdePassThroughDirect inBuffer, uint nInBufferSize, + ref IdePassThroughDirect outBuffer, uint nOutBufferSize, + ref uint pBytesReturned, nint overlapped); [LibraryImport("Kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] diff --git a/Aaru.Devices/Windows/Usb.cs b/Aaru.Devices/Windows/Usb.cs index 4a2d2e16a..bd1f83ff7 100644 --- a/Aaru.Devices/Windows/Usb.cs +++ b/Aaru.Devices/Windows/Usb.cs @@ -1187,16 +1187,14 @@ static partial class Usb ref SpDeviceInterfaceData deviceInterfaceData); - [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceInterfaceDetailW", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, - ref SpDeviceInterfaceData - deviceInterfaceData, - ref SpDeviceInterfaceDetailData - deviceInterfaceDetailData, - int deviceInterfaceDetailDataSize, - ref int requiredSize, - ref SpDevinfoData deviceInfoData); + [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] + static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, + ref SpDeviceInterfaceData deviceInterfaceData, + ref SpDeviceInterfaceDetailData + deviceInterfaceDetailData, + int deviceInterfaceDetailDataSize, + ref int requiredSize, + ref SpDevinfoData deviceInfoData); [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceRegistryPropertyW", @@ -1216,11 +1214,10 @@ static partial class Usb [return: MarshalAs(UnmanagedType.Bool)] private static partial bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet); - [LibraryImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceInstanceIdW", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool SetupDiGetDeviceInstanceId( - IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, StringBuilder deviceInstanceId, - int deviceInstanceIdSize, out int requiredSize); + [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)] + static extern bool SetupDiGetDeviceInstanceId(IntPtr deviceInfoSet, ref SpDevinfoData deviceInfoData, + StringBuilder deviceInstanceId, + int deviceInstanceIdSize, out int requiredSize); [LibraryImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)]