UsbWrapper "Unable to create a handle for the device"

I am trying to create a Windows Store app in C# using the Pololu usb sdk. The ConnectToDevice(), TrySetTarget, and TrySetSpeed are the same as in the sample applications, and I have added references to the 2.0.5.0 version of System namespace. The code compiles and I confirmed that the device is found and added to a DeviceItemList. However, I get the error below when I try to instantiate a new Usc using the DeviceItemList.

I can connect using the MaestroEasyExample and the Maestro Control Center, so I’m fairly certain this is a software bug.

Exception thrown: 'System.Exception' in UsbWrapper.dll
System.Exception: There was an error connecting to the device. ---> System.ComponentModel.Win32Exception: Unabled to create a handle for the device.
   at Pololu.WinusbHelper.Winusb.listConnect(IntPtr listHandle, Byte index, Guid deviceInterfaceGuid)
   at Pololu.WinusbHelper.Winusb.connect(Guid deviceInterfaceGuid, Int32 deviceInstance)
   at Pololu.UsbWrapper.UsbDevice..ctor(DeviceListItem deviceListItem)
   --- End of inner exception stack trace ---
   at Pololu.UsbWrapper.UsbDevice..ctor(DeviceListItem deviceListItem)
   at Pololu.Usc.Usc..ctor(DeviceListItem deviceListItem)
   at EyeMannequin.MainPage.TrySetSpeed(Byte channel, UInt16 target)
The program '[23792] EyeMannequin.exe' has exited with code -1 (0xffffffff).

Hello.

There is no TrySetSpeed function in any of the Maestro examples in the Pololu USB SDK. Where did you find that code?

From your description and error message, it sounds like there is something wrong with your code, since you are able to run the (unmodified) MaestroEasyExample and use Maestro Control Center without any issues. Can you post your main code here?

- Amanda

Sorry, I forgot that I wrote TrySetSpeed by copying the TrySetTarget and using the Usc setSpeed function instead of setTarget.

using Pololu.Usc;
using Pololu.UsbWrapper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;


namespace EyeMannequin
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();
            TrySetSpeed(1, 1000);
        }

        /// <summary>
        /// Attempts to set the target (width of pulses sent) of a channel.
        /// </summary>
        /// <param name="channel">Channel number from 0 to 23.</param>
        /// <param name="target">
        ///   Target, in units of quarter microseconds.  For typical servos,
        ///   6000 is neutral and the acceptable range is 4000-8000.
        /// </param>
        //void TrySetTarget(Byte channel, UInt16 target)
        //{
        //    try
        //    {
        //        using (DeviceListItem device = ConnectToDevice())  // Find a device and temporarily connect.
        //        {
        //            device.setTarget(channel, target);
        //        }
        //    }
        //    catch (Exception exception)  // Handle exceptions by displaying them to the user.
        //    {
        //        Debug.WriteLine(exception);
        //    }
        //}

        void TrySetSpeed(Byte channel, UInt16 target)
        {
            try
            {
                using (DeviceListItem device = ConnectToDevice())  // Find a device and temporarily connect.
                {
                    device.setSpeed(channel, target);
                }
             }
            catch (Exception exception)  // Handle exceptions by displaying them to the user.
            {
                Debug.WriteLine(exception);
            }
        }

        /// <summary>
        /// Connects to a Maestro using native USB and returns the Usc object
        /// representing that connection.  When you are done with the
        /// connection, you should close it using the Dispose() method so that
        /// other processes or functions can connect to the device later.  The
        /// "using" statement can do this automatically for you.
        /// </summary>
        Usc ConnectToDevice()
        {
            // Get a list of all connected devices of this type.
            List<DeviceListItem> connectedDevices = Usc.getConnectedDevices();

            foreach (DeviceListItem dli in connectedDevices)
            {
                // If you have multiple devices connected and want to select a particular
                // device by serial number, you could simply add a line like this:
                //   if (dli.serialNumber != "00012345"){ continue; }
                Debug.WriteLine(dli.serialNumber);
                Usc device = new Usc(dli); // Connect to the device.
                return device;             // Return the device.
            }
            throw new Exception("Could not find device.  Make sure it is plugged in to USB " +
                "and check your Device Manager (Windows) or run lsusb (Linux).");
        }
    }
}

The ConnectToDevice function returns a Usc object, not DeviceListItem. You should change DeviceListItem to Usc for the device variable in your TrySetSpeed function (and in the TrySetTarget function if you plan on using it in your program).

- Amanda

Yes, that is an error; thanks. The exception is thrown on the line Usc device = new Usc(dli);, before the Usc item is returned though.

At this point, to help narrow down the source of the problem, I suggest making a new program that is the same as the MaestroEasyExample code, and then gradually changing the code to be more like your code. At some point, you should encounter a small step that causes the code to stop working. Once you get there, please post what you found, and I can help you further.

- Amanda