diff --git a/src/HidLibrary/Extensions.cs b/src/HidLibrary/Extensions.cs index 2c692c0..0c64a58 100644 --- a/src/HidLibrary/Extensions.cs +++ b/src/HidLibrary/Extensions.cs @@ -1,15 +1,23 @@ -using System.Text; +using System; +using System.ComponentModel; +using System.Text; namespace HidLibrary { + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This class will be removed in a future version.")] public static class Extensions { + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method will be removed in a future version.")] public static string ToUTF8String(this byte[] buffer) { var value = Encoding.UTF8.GetString(buffer); return value.Remove(value.IndexOf((char)0)); } + [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method will be removed in a future version.")] public static string ToUTF16String(this byte[] buffer) { var value = Encoding.Unicode.GetString(buffer); diff --git a/src/HidLibrary/HidDevices.cs b/src/HidLibrary/HidDevices.cs index 7067bc1..b3ea39f 100644 --- a/src/HidLibrary/HidDevices.cs +++ b/src/HidLibrary/HidDevices.cs @@ -98,7 +98,7 @@ private static string GetDevicePath(IntPtr deviceInfoSet, NativeMethods.SP_DEVIC var bufferSize = 0; var interfaceDetail = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA { Size = IntPtr.Size == 4 ? 4 + Marshal.SystemDefaultCharSize : 8 }; - NativeMethods.SetupDiGetDeviceInterfaceDetailBuffer(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero); + NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero); return NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, ref interfaceDetail, bufferSize, ref bufferSize, IntPtr.Zero) ? interfaceDetail.DevicePath : null; @@ -115,43 +115,56 @@ private static Guid HidClassGuid private static string GetDeviceDescription(IntPtr deviceInfoSet, ref NativeMethods.SP_DEVINFO_DATA devinfoData) { - var descriptionBuffer = new byte[1024]; - - var requiredSize = 0; - var type = 0; + unsafe + { + const int charCount = 1024; + var descriptionBuffer = stackalloc char[charCount]; - NativeMethods.SetupDiGetDeviceRegistryProperty(deviceInfoSet, - ref devinfoData, - NativeMethods.SPDRP_DEVICEDESC, - ref type, - descriptionBuffer, - descriptionBuffer.Length, - ref requiredSize); + var requiredSize = 0; + var type = 0; + + if (NativeMethods.SetupDiGetDeviceRegistryProperty(deviceInfoSet, + ref devinfoData, + NativeMethods.SPDRP_DEVICEDESC, + ref type, + descriptionBuffer, + propertyBufferSize: charCount * sizeof(char), + ref requiredSize)) + { + return new string(descriptionBuffer); + } - return descriptionBuffer.ToUTF8String(); + return null; + } } private static string GetBusReportedDeviceDescription(IntPtr deviceInfoSet, ref NativeMethods.SP_DEVINFO_DATA devinfoData) { - var descriptionBuffer = new byte[1024]; - - if (Environment.OSVersion.Version.Major > 5) + unsafe { - uint propertyType = 0; - var requiredSize = 0; + const int charCount = 1024; + var descriptionBuffer = stackalloc char[charCount]; - var _continue = NativeMethods.SetupDiGetDeviceProperty(deviceInfoSet, - ref devinfoData, - ref NativeMethods.DEVPKEY_Device_BusReportedDeviceDesc, - ref propertyType, - descriptionBuffer, - descriptionBuffer.Length, - ref requiredSize, - 0); + if (Environment.OSVersion.Version.Major > 5) + { + uint propertyType = 0; + var requiredSize = 0; + + if (NativeMethods.SetupDiGetDeviceProperty(deviceInfoSet, + ref devinfoData, + ref NativeMethods.DEVPKEY_Device_BusReportedDeviceDesc, + ref propertyType, + descriptionBuffer, + propertyBufferSize: charCount * sizeof(char), + ref requiredSize, + 0)) + { + return new string(descriptionBuffer); + } + } - if (_continue) return descriptionBuffer.ToUTF16String(); + return null; } - return null; } } } diff --git a/src/HidLibrary/HidLibrary.csproj b/src/HidLibrary/HidLibrary.csproj index 8c8b1c2..e94d167 100644 --- a/src/HidLibrary/HidLibrary.csproj +++ b/src/HidLibrary/HidLibrary.csproj @@ -2,6 +2,7 @@ net20;net35;net40;net45;netstandard2 + true 3.2.49 Ultraviolet Catastrophe diff --git a/src/HidLibrary/NativeMethods.cs b/src/HidLibrary/NativeMethods.cs index fefb500..9c59b0c 100644 --- a/src/HidLibrary/NativeMethods.cs +++ b/src/HidLibrary/NativeMethods.cs @@ -1,6 +1,8 @@ using System; using System.Runtime.InteropServices; +[module: DefaultCharSet(CharSet.Unicode)] + namespace HidLibrary { internal static class NativeMethods @@ -27,16 +29,16 @@ internal struct SECURITY_ATTRIBUTES public bool bInheritHandle; } - [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] static internal extern bool CancelIoEx(IntPtr hFile, IntPtr lpOverlapped); - [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] + [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] static internal extern bool CloseHandle(IntPtr hObject); - [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] + [DllImport("kernel32.dll")] static internal extern IntPtr CreateEvent(ref SECURITY_ATTRIBUTES securityAttributes, int bManualReset, int bInitialState, string lpName); - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + [DllImport("kernel32.dll", SetLastError = true)] static internal extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, ref SECURITY_ATTRIBUTES lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); [DllImport("kernel32.dll", SetLastError = true)] @@ -106,7 +108,7 @@ internal struct SP_DEVINFO_DATA internal IntPtr Reserved; } - [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] + [StructLayout(LayoutKind.Sequential)] internal struct SP_DEVICE_INTERFACE_DETAIL_DATA { internal int Size; @@ -124,11 +126,11 @@ internal struct DEVPROPKEY internal static DEVPROPKEY DEVPKEY_Device_BusReportedDeviceDesc = new DEVPROPKEY { fmtid = new Guid(0x540b947e, 0x8b40, 0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2), pid = 4 }; - [DllImport("setupapi.dll", EntryPoint = "SetupDiGetDeviceRegistryProperty")] - public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, int propertyVal, ref int propertyRegDataType, byte[] propertyBuffer, int propertyBufferSize, ref int requiredSize); + [DllImport("setupapi.dll")] + public static extern unsafe bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, int propertyVal, ref int propertyRegDataType, void* propertyBuffer, int propertyBufferSize, ref int requiredSize); - [DllImport("setupapi.dll", EntryPoint = "SetupDiGetDevicePropertyW", SetLastError = true)] - public static extern bool SetupDiGetDeviceProperty(IntPtr deviceInfo, ref SP_DEVINFO_DATA deviceInfoData, ref DEVPROPKEY propkey, ref uint propertyDataType, byte[] propertyBuffer, int propertyBufferSize, ref int requiredSize, uint flags); + [DllImport("setupapi.dll", SetLastError = true)] + public static extern unsafe bool SetupDiGetDeviceProperty(IntPtr deviceInfo, ref SP_DEVINFO_DATA deviceInfoData, ref DEVPROPKEY propkey, ref uint propertyDataType, void* propertyBuffer, int propertyBufferSize, ref int requiredSize, uint flags); [DllImport("setupapi.dll")] static internal extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, int memberIndex, ref SP_DEVINFO_DATA deviceInfoData); @@ -139,13 +141,13 @@ internal struct DEVPROPKEY [DllImport("setupapi.dll")] static internal extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, ref Guid interfaceClassGuid, int memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData); - [DllImport("setupapi.dll", CharSet = CharSet.Unicode)] + [DllImport("setupapi.dll")] static internal extern IntPtr SetupDiGetClassDevs(ref System.Guid classGuid, string enumerator, IntPtr hwndParent, int flags); - [DllImport("setupapi.dll", CharSet = CharSet.Auto, EntryPoint = "SetupDiGetDeviceInterfaceDetail")] - static internal extern bool SetupDiGetDeviceInterfaceDetailBuffer(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, IntPtr deviceInfoData); + [DllImport("setupapi.dll")] + static internal extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, IntPtr deviceInfoData); - [DllImport("setupapi.dll", CharSet = CharSet.Auto)] + [DllImport("setupapi.dll")] static internal extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, IntPtr deviceInfoData); [StructLayout(LayoutKind.Sequential)] @@ -206,13 +208,13 @@ internal struct HIDP_CAPS [DllImport("hid.dll")] static internal extern int HidP_GetCaps(IntPtr preparsedData, ref HIDP_CAPS capabilities); - [DllImport("hid.dll", CharSet = CharSet.Unicode)] + [DllImport("hid.dll")] internal static extern bool HidD_GetProductString(IntPtr hidDeviceObject, ref byte lpReportBuffer, int ReportBufferLength); - [DllImport("hid.dll", CharSet = CharSet.Unicode)] + [DllImport("hid.dll")] internal static extern bool HidD_GetManufacturerString(IntPtr hidDeviceObject, ref byte lpReportBuffer, int ReportBufferLength); - [DllImport("hid.dll", CharSet = CharSet.Unicode)] + [DllImport("hid.dll")] internal static extern bool HidD_GetSerialNumberString(IntPtr hidDeviceObject, ref byte lpReportBuffer, int reportBufferLength); } }