Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Unicode functions consistently and remove helper extension methods that were made public #119

Merged
merged 10 commits into from
Sep 24, 2020
10 changes: 9 additions & 1 deletion src/HidLibrary/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
69 changes: 41 additions & 28 deletions src/HidLibrary/HidDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
}
1 change: 1 addition & 0 deletions src/HidLibrary/HidLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net20;net35;net40;net45;netstandard2</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<Version>3.2.49</Version>
<Company>Ultraviolet Catastrophe</Company>
Expand Down
34 changes: 18 additions & 16 deletions src/HidLibrary/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Runtime.InteropServices;

[module: DefaultCharSet(CharSet.Unicode)]

namespace HidLibrary
{
internal static class NativeMethods
Expand All @@ -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)]
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)]
Expand Down Expand Up @@ -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);
}
}