Skip to content

Commit

Permalink
Changed the prototype of FindHidDevices to reduce complexity
Browse files Browse the repository at this point in the history
There's no point in using a `ref` variable when the function can return that information directly. This was a weird design decision.
  • Loading branch information
spiliot committed Apr 2, 2017
1 parent dcf220d commit 39f71a5
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions USB/Classes/DeviceDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace UsbHid.USB.Classes
{
public static class DeviceDiscovery
{
public static bool FindHidDevices(ref string[] listOfDevicePathNames, ref int numberOfDevicesFound)
public static int FindHidDevices(ref string[] listOfDevicePathNames)
{
var bufferSize = 0;
var detailDataBuffer = IntPtr.Zero;
Expand Down Expand Up @@ -63,13 +63,13 @@ public static bool FindHidDevices(ref string[] listOfDevicePathNames, ref int nu
// Get the String containing the devicePathName.
listOfDevicePathNames[listIndex] = Marshal.PtrToStringAuto(pDevicePathName);

listIndex += 1;
listIndex++;
}
}
catch (Exception)
{
// Something went badly wrong...
return false;
return 0;
}
finally
{
Expand All @@ -78,26 +78,22 @@ public static bool FindHidDevices(ref string[] listOfDevicePathNames, ref int nu
SetupApi.SetupDiDestroyDeviceInfoList(deviceInfoSet);
}

if (listIndex == 0) return false;

numberOfDevicesFound = listIndex;
return true;
return listIndex;
}

public static bool FindTargetDevice(ref DeviceInformationStructure deviceInformation)
{
var listOfDevicePathNames = new String[128]; // 128 is the maximum number of USB devices allowed on a single host
var numberOfDevicesFound = 0;

try
{
// Reset the device detection flag
deviceInformation.IsDeviceAttached = false;

// Get all the devices with the correct HID GUID
var deviceFoundByGuid = FindHidDevices(ref listOfDevicePathNames, ref numberOfDevicesFound);
int numberOfDevicesFound = FindHidDevices(ref listOfDevicePathNames);

if (!deviceFoundByGuid) return false;
if (numberOfDevicesFound == 0) return false;

for (int listIndex = 0; listIndex <= numberOfDevicesFound; listIndex++)
{
Expand Down

0 comments on commit 39f71a5

Please sign in to comment.